View source
<?php
function eck_get_property_types($property_type = '', $reset = FALSE) {
global $language;
static $property_types;
$langcode = $language->language;
if ($reset) {
$property_types = NULL;
cache_clear_all('property_types:', 'cache_eck', TRUE);
}
if (!$property_types) {
if ($cached = cache_get("property_types:{$langcode}", 'cache_eck')) {
$property_types = $cached->data;
}
else {
$property_types = array();
foreach (module_implements('eck_property_types') as $module) {
$module_property_types = (array) module_invoke($module, 'eck_property_types');
foreach ($module_property_types as $name => $property_type_info) {
$property_type_info += array(
'type' => $name,
'label' => t("@name", array(
"@name" => $name,
)),
'settings' => array(),
'class' => FALSE,
'file' => FALSE,
'file type' => 'inc',
'description' => '',
);
$property_types[$name] = $property_type_info;
$property_types[$name]['module'] = $module;
}
}
drupal_alter('eck_property_types', $property_types);
cache_set("property_types:{$langcode}", $property_types, 'cache_eck');
}
}
if (!$property_type) {
return $property_types;
}
if (isset($property_types[$property_type])) {
return $property_types[$property_type];
}
}
function eck_get_property_type_class($property_type_name = '') {
$property_classes =& drupal_static(__FUNCTION__, array());
$load_types = NULL;
if ($property_type_name && empty($property_classes[$property_type_name])) {
if ($property_type = eck_get_property_types($property_type_name)) {
$load_types = array(
$property_type_name => $property_type,
);
}
}
elseif (!$property_type_name) {
$load_types = array_diff_key(eck_get_property_types(), $property_classes);
}
if ($load_types) {
foreach ($load_types as $property_type => $property_type_info) {
if ($class = $property_type_info['class']) {
$class_loaded = class_exists($class);
if (!$class_loaded) {
if (function_exists('drupal_get_path') && $property_type_info['file']) {
$file = DRUPAL_ROOT . '/' . drupal_get_path('module', $property_type_info['module']) . '/' . $property_type_info['file'];
if (is_file($file)) {
require_once $file;
$class_loaded = class_exists($class);
}
}
}
$property_classes[$property_type] = $class_loaded ? $class : FALSE;
}
}
}
if (!$property_type_name) {
return $property_classes;
}
if (isset($property_classes[$property_type_name])) {
return $property_classes[$property_type_name];
}
}
function eck_get_property_type($property_type) {
return eck_get_property_types($property_type);
}
function eck_get_property_type_schema($property_type_name) {
if ($class = eck_get_property_type_class($property_type_name)) {
return $class::schema();
}
}
interface PropertyTypeInterface {
public static function schema();
public static function validate($value);
}
class TextPropertyType implements PropertyTypeInterface {
public static function schema() {
$schema = array(
'description' => 'Text',
'type' => 'varchar',
'not null' => TRUE,
'default' => '',
'length' => 255,
);
return $schema;
}
public static function validate($value) {
return TRUE;
}
}
class FixedSizeTextPropertyType implements PropertyTypeInterface {
public static function schema() {
$schema = array(
'description' => 'All of the values of this property are text of the same size',
'type' => 'char',
'not null' => TRUE,
'default' => '',
'length' => 255,
);
return $schema;
}
public static function validate($value) {
return TRUE;
}
}
class LongTextPropertyType implements PropertyTypeInterface {
public static function schema() {
$schema = array(
'description' => 'Long Text (longer than 8000 characters)',
'type' => 'text',
'length' => 255,
'not null' => TRUE,
'length' => 255,
);
return $schema;
}
public static function validate($value) {
return TRUE;
}
}
class IntegerPropertyType implements PropertyTypeInterface {
public static function schema() {
$schema = array(
'description' => "Integer",
'type' => 'int',
'size' => 'normal',
'not null' => TRUE,
'default' => 0,
'unsigned' => FALSE,
);
return $schema;
}
public static function validate($value) {
$number = filter_var($value, FILTER_VALIDATE_INT);
return $number !== FALSE;
}
}
class DecimalPropertyType implements PropertyTypeInterface {
public static function schema() {
$schema = array(
'description' => 'Decimal/Float/Double',
'type' => 'float',
'not null' => TRUE,
'default' => 0,
'unsigned' => FALSE,
);
return $schema;
}
public static function validate($value) {
$number = filter_var($value, FILTER_VALIDATE_FLOAT);
return $number !== FALSE;
}
}
class BlobPropertyType implements PropertyTypeInterface {
public static function schema() {
$schema = array(
'description' => 'Decimal/Float/Double',
'type' => 'blob',
'size' => 'normal',
'not null' => TRUE,
);
return $schema;
}
public static function validate($value) {
return TRUE;
}
}
class DateTimePropertyType implements PropertyTypeInterface {
public static function schema() {
$schema = array(
'description' => 'Date/Time',
'type' => 'datetime',
'not null' => TRUE,
'default' => 0,
);
return $schema;
}
public static function validate($value) {
$number = filter_var($value, FILTER_VALIDATE_FLOAT);
return $number !== FALSE;
}
}
class LanguagePropertyType extends TextPropertyType {
public static function schema() {
$schema = parent::schema();
$schema['default'] = LANGUAGE_NONE;
$schema['description'] = "Language";
$schema['length'] = 12;
return $schema;
}
public static function validate($value) {
if (parent::validate($value)) {
return TRUE;
}
return FALSE;
}
}
class UUIDPropertyType extends FixedSizeTextPropertyType {
public static function schemaConditions() {
return array(
'locked' => 'length',
);
}
public static function schema() {
$schema = parent::schema();
$schema['description'] = 'Universally Unique Identifier';
$schema['length'] = 16;
return $schema;
}
public static function validate($value) {
return TRUE;
}
}
class PositiveIntegerPropertyType extends IntegerPropertyType {
public static function schema() {
$schema = parent::schema();
$schema['description'] = "Positive Integer";
$schema['unsigned'] = TRUE;
return $schema;
}
public static function validate($value) {
if (parent::validate($value)) {
$int = intval($value);
if ($int >= 0) {
return TRUE;
}
}
return FALSE;
}
}