コード例 #1
0
ファイル: core.php プロジェクト: justus/kohana-formo
 /**
  * Load a model's fields into the form
  *
  * @access public
  * @param mixed Jelly_Model $model
  * @param mixed array $fields. (default: NULL)
  * @return form object
  */
 public function load(Jelly_Model $model, array $fields = NULL)
 {
     $this->model = $model;
     $this->make_fields($fields);
     foreach ($model->meta()->fields() as $column => $field) {
         if (in_array($column, $this->skip_fields)) {
             continue;
         }
         if ($this->fields and !in_array($column, $this->fields)) {
             continue;
         }
         // Create the array
         $options = (array) $field;
         // Fetch the validation key names from the config file
         $validation_keys = $this->config()->validation_keys;
         // Look for validation rules as defined by the config file
         foreach ($validation_keys as $key => $value) {
             // If they are using the assumed names, do nothing
             if ($key === $value) {
                 continue;
             }
             // Only grab the proper validation settings from jelly field definition
             $options[$key] = !empty($options[$value]) ? $options[$value] : array();
             // No need to carry duplicates for a rule
             unset($options[$value]);
         }
         // Determine the driver
         $options['driver'] = $this->determine_driver($options, get_class($field));
         // Add the value
         if ($field instanceof Jelly_Field_Relationship === FALSE) {
             // Add the value
             $options['value'] = $model->get($column) ? $model->get($column) : $options['default'];
         } elseif ($field instanceof Field_ManyToMany === FALSE and $field instanceof Field_HasMany === FALSE) {
             // grab the actual foreign model
             $foreign_model = $model->get($column)->execute();
             // Set the value
             $options['value'] = $foreign_model->id();
         } else {
             // Grab all the foreign options
             $all_options = Jelly::select($field->foreign['model'])->execute();
             // Create the array
             $options['options'] = array();
             $options['value'] = array();
             foreach ($all_options as $option) {
                 // Build the option
                 $options['options'][] = array('value' => $option->id(), 'alias' => $option->name());
                 if ($model->has($column, $option)) {
                     $options['value'][] = $option->id();
                 }
             }
         }
         // Add the field to its parent
         $this->form->add($column, $options);
         $field = $this->form->{$column};
     }
     return $this->form;
 }
コード例 #2
0
ファイル: core.php プロジェクト: salesignighter/kohana-formo
 /**
  * Load a model's fields into the form
  * 
  * @access public
  * @param mixed Jelly_Model $model
  * @param mixed array $fields. (default: NULL)
  * @return form object
  */
 public function load(Jelly_Model $model, array $fields = NULL)
 {
     $this->model = $model;
     $this->make_fields($fields);
     foreach ($model->meta()->fields() as $column => $field) {
         if (in_array($column, $this->skip_fields)) {
             continue;
         }
         if ($this->fields and !in_array($column, $this->fields)) {
             continue;
         }
         // Create the array
         $options = (array) $field;
         // Fetch the validation key names from the config file
         $validation_keys = $this->config()->validation_keys;
         // Look for validation rules as defined by the config file
         foreach ($validation_keys as $key => $value) {
             // If they are using the assumed names, do nothing
             if ($key === $value) {
                 continue;
             }
             // Only grab the proper validation settings from jelly field definition
             $options[$key] = !empty($options[$value]) ? $options[$value] : array();
             // No need to carry duplicates for a rule
             unset($options[$value]);
         }
         // Determine the driver
         $options['driver'] = $this->determine_driver($options, get_class($field));
         // Add the value
         if ($field instanceof Jelly_Field_Relationship === FALSE) {
             // Add the value
             $options['value'] = $model->get($column) ? $model->get($column) : $options['default'];
         } elseif ($field instanceof Field_ManyToMany === FALSE and $field instanceof Field_HasMany === FALSE) {
             // grab the actual foreign model
             $foreign_model = $model->get($column)->execute();
             // Set the value
             $options['value'] = $foreign_model->{$foreign_model->meta()->primary_key()};
         } else {
             // Grab the foreign records
             $foreign_models = $model->get($column)->execute();
             // Create the array
             $values = array();
             foreach ($foreign_models as $record) {
                 $values[$record->get($record->meta()->name_key())] = $record->get($record->meta()->primary_key());
             }
             $options['value'] = $values;
         }
         is_object($options['value']) and $options['value'] = (string) $options['value'];
         // Add the field to its parent
         $this->form->add($column, $options);
         $field = $this->form->{$column};
         $this->add_auto_rules($field);
     }
     return $this->form;
 }
コード例 #3
0
ファイル: sorton.php プロジェクト: vitch/kohana-kadmium
 /**
  * If value is null (e.g. initial saving of model) then value
  * is set to the number of records in this column.
  *
  * @param   Jelly_Model  $model
  * @param   mixed  $value
  * @return  mixed
  */
 public function save($model, $value, $loaded)
 {
     if ($value == null) {
         $builder = Jelly::select($model->meta()->model());
         if (isset($this->category_key)) {
             // TODO: There must be a way to just get at the value without having to execute the
             // query and then get it back out?!??!
             $foreign = $model->get($this->category_key)->execute();
             $builder->where($this->category_key, '=', $foreign->get($foreign->meta()->primary_key()));
         }
         $value = $builder->count() + 1;
     }
     return $value;
 }
コード例 #4
0
ファイル: core.php プロジェクト: vitch/kohana-formo
 public static function load(Jelly_Model $model, Formo $form, array $fields = NULL)
 {
     $skip_fields = array();
     if ($fields and in_array('*', $fields)) {
         $skip_fields = $fields;
         $fields = NULL;
     }
     foreach ($model->meta()->fields() as $column => $field) {
         if (in_array($column, $skip_fields)) {
             continue;
         }
         if ($fields and !in_array($column, $fields)) {
             continue;
         }
         $options = (array) $field + array('value' => $model->get($column));
         // Fetch the validation key names from the config file
         $validation_keys = Kohana::config('formo_jelly')->validation_keys;
         // Add specific rules
         if ($options['unique'] === TRUE) {
             // If the field is set to unique, add the rule to check for other records
             $options[$validation_keys['rules']][':model::unique'] = array($column);
         }
         // Look for validation rules as defined by the config file
         foreach ($validation_keys as $key => $value) {
             // If they are using the assumed names, do nothing
             if ($key === $value) {
                 continue;
             }
             // Only grab the proper validation settings from jelly field definition
             $options[$key] = !empty($options[$value]) ? $options[$value] : array();
             // No need to carry duplicates for a rule
             unset($options[$value]);
         }
         // NOTE: This shouldn't really happen until pre_render
         /*
         			$options = array('value' => $model->get($column));
         			
         			$add_options = array('driver', 'rules', 'filters', 'triggers', 'post_filters');
         			
         			foreach ($add_options as $option)
         			{
         				if ( ! empty($field->$option))
         				{
         					$options[$option] = $field->$option;
         				}
         			}
         */
         $form->add($column, $options);
     }
 }
コード例 #5
0
ファイル: core.php プロジェクト: TdroL/kohana-jelly-torn
 public function __construct(Jelly_Model $model)
 {
     $this->model = $model;
     foreach ($model->meta()->fields() as $name => $jelly_field) {
         if (!preg_match('/Field_(?<name>.*)$/i', get_class($jelly_field), $matches)) {
             throw new Torn_Field_Exception('Unsupported field type ":field"', array(':field' => get_class($jelly_field)));
         }
         $field_name = Kohana::config('torn')->field_prefix . ucfirst($matches['name']);
         $class = new ReflectionClass($field_name);
         if (!$class->isSubclassOf('Torn_Field')) {
             throw new Torn_Field_Exception('Field ":field" must be a subclass of Torn_Field', array(':field' => get_class($field_name)));
         }
         $this->fields[$name] = $class->newInstance($jelly_field, $model, $this);
     }
 }
コード例 #6
0
ファイル: manytomany.php プロジェクト: piotrtheis/jelly
 /**
  * Returns either an array or unexecuted query to find
  * which columns the model is "in" in the join table.
  *
  * @param   Jelly_Model  $model
  * @param   boolean      $as_array
  * @return  mixed
  */
 protected function _in($model, $as_array = FALSE)
 {
     $result = Jelly::query($this->through['model'])->select_column($this->through['fields'][1], 'in')->where($this->through['fields'][0], '=', $model->id())->type(Database::SELECT);
     if ($as_array) {
         $result = $result->select($model->meta()->db())->as_array(NULL, 'in');
     }
     return $result;
 }
コード例 #7
0
ファイル: image.php プロジェクト: piotrtheis/jelly
 /**
  * Deletes the image and the thumbnails if automatic file deletion
  * is enabled.
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $key
  * @return  void
  */
 public function delete($model, $key)
 {
     if (!$this->delete_file) {
         // Stop here if automatic deletion is disabled
         return;
     }
     // Set the field name
     $field = $this->name;
     // Set file
     $file = $this->path . $model->{$field};
     if (is_file($file)) {
         // Delete file
         unlink($file);
     }
     // Set thumbnails
     $thumbnails = $model->meta()->field($field)->thumbnails;
     foreach ($thumbnails as $thumbnail) {
         // Set file name
         $file = $thumbnail['prefix'] . $model->{$field};
         if (isset($thumbnail['path'])) {
             // Add path to file name if set
             $file = $thumbnail['path'] . $file;
         } else {
             // Add the path of the original image
             $file = $this->path . $file;
         }
         if (is_file($file)) {
             // Delete file
             unlink($file);
         }
     }
     return;
 }