예제 #1
0
파일: file.php 프로젝트: vitch/kohana-jelly
 /**
  * Deletes the actual uploaded file.
  *
  * @param   Jelly_Model  $model
  * @return  void
  */
 public function delete($model)
 {
     $file = $model->get($this->name, FALSE);
     if ($file != $this->default) {
         $path = $this->path . $file;
         if (file_exists($path)) {
             unlink($path);
         }
     }
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
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);
     }
 }
예제 #4
0
 /**
  * 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;
 }
예제 #5
0
 /**
  * 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;
 }
예제 #6
0
 /**
  * Deletes the actual uploaded image(s).
  *
  * @param   Jelly_Model  $model
  * @return  void
  */
 public function delete($model)
 {
     $file = $model->get($this->name, FALSE);
     if ($file != $this->default) {
         $path = $this->path . $file;
         if (file_exists($path)) {
             unlink($path);
         }
         foreach ($this->thumbnails as $thumbnail) {
             $thumbnail_path = realpath($thumbnail['path']) . DIRECTORY_SEPARATOR;
             if (file_exists($thumbnail_path . $file)) {
                 unlink($thumbnail_path . $file);
             }
         }
     }
 }