Example #1
0
 /**
  * Operations over fields.
  */
 public function setField($field_name, AbstractField $field)
 {
     $field->setName($field_name);
     $field->setModel(get_called_class());
     // TODO: Find a way to make a clean management of field.
     if ($field instanceof Many2OneField) {
         $field_name_local = substr($field_name, 0, -3);
         $f = clone $field;
         $f->setName($field_name_local);
         $this->_structure[$field_name_local] = $f;
     }
     $this->_structure[$field_name] = $field;
     return TRUE;
 }
Example #2
0
 public function applyGet($value)
 {
     $value = parent::applyGet($value);
     if ($value === NULL) {
         return NULL;
     }
     $value = $value == TRUE ? TRUE : FALSE;
     return $value;
 }
Example #3
0
 public function validate($object, $field_name)
 {
     if (!parent::validate($object, $field_name)) {
         return FALSE;
     }
     $value = $object->{$field_name};
     if (empty($value)) {
         return TRUE;
     }
     if (strlen($value) > $this->size) {
         $this->setError('wrong_value', 'Field "' . $this->getLabel() . '" (' . $field_name . ') content is too long ' . strlen($value) . ' characters, only ' . $this->size . ' allowed!');
         return FALSE;
     }
     return TRUE;
 }
Example #4
0
 public function validate($object, $field_name)
 {
     if (!parent::validate($object, $field_name)) {
         return FALSE;
     }
     $value = $object->{$field_name};
     if (empty($value)) {
         return TRUE;
     }
     if (empty($this->enumeration[$value])) {
         $this->setError('wrong_value', 'Field "' . $this->getLabel() . '" (' . $field_name . ') should contains one of this options: ' . join('/', array_keys($this->enumeration)));
         return FALSE;
     }
     return TRUE;
 }
Example #5
0
 public function handleField(AbstractField $field)
 {
     $name = $field->getName();
     $defaultValue = $field->getDefaultValue();
     $required = $field->isRequired();
     $default = '';
     if ($required) {
         $default = 'NOT NULL';
         if ($defaultValue) {
             if ($defaultValue instanceof RawSQL) {
                 $default .= ' DEFAULT ' . $defaultValue->get() . '';
             } else {
                 $default .= ' DEFAULT "' . $defaultValue . '"';
             }
         }
     } else {
         if ($defaultValue instanceof RawSQL) {
             $default = $defaultValue->get();
         } else {
             $default = 'DEFAULT NULL';
         }
     }
     if (in_array($name, $this->primary_keys)) {
         $default = 'NOT NULL';
     }
     switch ($field->getType()) {
         case 'primary':
             $type = 'INT(10) unsigned';
             $default = 'NOT NULL';
             if ($field->getAutoId()) {
                 $default .= ' AUTO_INCREMENT';
             }
             break;
         case 'text':
         case 'password':
         case 'email':
             $type = 'VARCHAR(' . $field->getSize() . ')';
             break;
         case 'enum':
             $type = 'VARCHAR(32)';
             break;
         case 'datetime':
             $type = 'TIMESTAMP';
             break;
         case 'date':
             $type = 'DATE';
             break;
         case 'time':
             $type = 'TIME';
             break;
         case 'textarea':
             $type = 'TEXT';
             break;
         case 'boolean':
             $type = 'TINYINT(1)';
             break;
         case 'int':
             $type = 'INT';
             break;
         case 'float':
             $type = 'DOUBLE';
             break;
         case 'many2one':
             $type = 'INT(10) unsigned';
             if (substr($name, -3) !== '_id') {
                 return FALSE;
             }
             break;
         default:
             return FALSE;
     }
     $this->fields[$name] = '`' . $name . '` ' . $type . ' ' . $default;
     return TRUE;
 }