Esempio n. 1
0
 /**
  * Returns a Jelly model that, when load()ed will return a database
  * result of the models that this field has.
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $value
  * @param   boolean      $loaded
  * @return  Jelly
  */
 public function get($model, $value)
 {
     if ($model->changed($this->name)) {
         // Return a real object
         return Jelly::select($this->foreign['model'])->where(':primary_key', 'IN', $value);
     } else {
         return Jelly::select($this->foreign['model'])->where($this->foreign['column'], '=', $model->id());
     }
 }
Esempio n. 2
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;
 }
Esempio n. 3
0
 /**
  * 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);
         }
     }
 }
Esempio n. 4
0
 /**
  * Hashes the password only if it's changed.
  *
  * @param   string       $password
  * @param   Jelly_Model  $model
  * @return  string
  */
 public function hash($password, Jelly_Model $model)
 {
     // Do we need to hash the password?
     if (!empty($password) and $this->hash_with and $model->changed($this->name)) {
         // Hash the password
         return call_user_func($this->hash_with, $password);
     }
     // Return plain password if no hashing is done
     return $password;
 }
Esempio n. 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->{$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;
 }
Esempio n. 6
0
 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);
     }
 }
Esempio n. 7
0
 public function set($values, $value = NULL)
 {
     parent::set($values, $value);
     if (!$this->saved()) {
         $this->generate_link();
     }
     return $this;
 }
Esempio n. 8
0
 public function delete($key = NULL)
 {
     if (parent::delete($key)) {
         DB::delete('product_search')->where('product_id', '=', $this->id)->execute();
         return TRUE;
     }
     return FALSE;
 }
Esempio n. 9
0
 /**
  * Creates or updates the current exif data
  *
  * If $key is passed, the record will be assumed to exist
  * and an update will be executed, even if the model isn't loaded().
  *
  * @param   mixed  $key
  * @return  $this
  */
 public function save($key = null)
 {
     // If new EXIF data, try to read from image
     if (!$this->loaded() && !$key) {
         $this->read();
     }
     // If was new and no exif data was found it will not be saved
     parent::save($key);
 }
Esempio n. 10
0
 public function delete($key = NULL)
 {
     $path = $this->_meta->fields('file')->path;
     try {
         unlink($path . $this->file);
     } catch (Exception $e) {
         Kohana::$log->add('info', 'Requested remove of ":file". Failed.', array(':file' => $path . $this->file));
     }
     return parent::delete($key);
 }
Esempio n. 11
0
 public function save($key = NULL)
 {
     if ($this->product->unit->type == 'integer') {
         $this->quantity = (int) $this->quantity;
     }
     $status = $this->_original['status'];
     parent::save();
     if ($status != $this->status and $this->status == 'done') {
         $this->product->modify_quantity($this->quantity);
     }
 }
Esempio n. 12
0
 public function delete($key = NULL)
 {
     foreach ($this->images as $image) {
         $image->delete();
     }
     $this->reset_priority();
     $path = $this->_meta->fields('file')->path;
     try {
         unlink($path . $this->file);
     } catch (Exception $e) {
         Kohana::$log->add('info', 'Requested remove of ":file". Failed.', array(':file' => $path . $this->file));
     }
     return parent::delete($key);
 }
Esempio n. 13
0
 /**
  * Allows a model to be loaded by username or email address.
  *
  * @param   mixed  $id  id, username, email
  * @return  string
  */
 public function unique_key($id)
 {
     if (!empty($id) && is_string($id) && !ctype_digit($id)) {
         return valid::email($id) ? 'email' : 'username';
     }
     return parent::unique_key($id);
 }
Esempio n. 14
0
 /**
  * Gets a string representation of the value, formatted according to the
  * fields type.
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $value
  * @return String
  **/
 public function display($model, $value)
 {
     return '<span class="sort-on" rel="' . $model->id() . '">' . $value . '</span>';
 }
Esempio n. 15
0
 /**
  * Update user token
  *
  * @return  boolean
  */
 public function update()
 {
     $this->token = $this->create_token();
     return parent::save();
 }
Esempio n. 16
0
 /**
  * Creates or updates the current image
  *
  * If $key is passed, the record will be assumed to exist
  * and an update will be executed, even if the model isn't loaded().
  *
  * @param   mixed  $key
  * @return  $this
  */
 public function save($key = null)
 {
     $new = !$this->loaded() && !$key;
     if ($new) {
         if (!$this->file || !Upload::not_empty($this->file)) {
             throw new Kohana_Exception(__('No image'));
         } else {
             if (!Upload::size($this->file, Kohana::config('image.filesize'))) {
                 throw new Kohana_Exception(__('Image too big (limit :size)', array(':size' => Kohana::config('image.filesize'))));
             } else {
                 if (!Upload::type($this->file, Kohana::config('image.filetypes'))) {
                     throw new Kohana_Exception(__('Invalid image type (use :types)', array(':types' => implode(', ', Kohana::config('image.filetypes')))));
                 }
             }
         }
     }
     parent::save($key);
     // Some magic on created images only
     if ($new) {
         // Make sure we have the new target directory
         $new_path = Kohana::config('image.path') . URL::id($this->id);
         if (!is_dir($new_path)) {
             mkdir($new_path, 0777, true);
             chmod($new_path, 0777);
         }
         if (is_writable($new_path)) {
             $new_path = rtrim($new_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
         } else {
             throw new Kohana_Exception(get_class($this) . ' can not write to directory');
         }
         // New file name with some random postfix for hard to guess filenames
         !$this->postfix and $this->postfix = Text::random('alnum', 8);
         $new_file = $this->id . '_' . $this->postfix . Kohana::config('image.postfix_original') . '.jpg';
         // Rename and move to correct directory using image id
         $old_path = Kohana::config('image.upload_path');
         $old_file = $this->file;
         if (!rename($old_path . $old_file, $new_path . $new_file)) {
             throw new Kohana_Exception(get_class($this) . ' could not move uploaded image');
         }
         $this->file = $new_file;
         // Start creating images
         $this->_generate_images($new_path . $new_file);
         parent::save();
     }
     return $this;
 }
Esempio n. 17
0
 /**
  * Return model id for routing/URLs
  *
  * @static
  * @param   Jelly_Model  $model
  * @return  string
  */
 public static function model_id(Jelly_Model $model)
 {
     return URL::title($model->id() . ' ' . $model->name());
 }
Esempio n. 18
0
 /**
  * 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;
 }
Esempio n. 19
0
 /**
  * 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;
 }
Esempio n. 20
0
 /**
  * Implementation of Jelly_Field_Supports_Has.
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $models
  * @return  boolean
  */
 public function has($model, $models)
 {
     return (bool) Jelly::query($this->foreign['model'])->where($this->foreign['model'] . '.' . $this->foreign['field'], '=', $model->id())->where($this->foreign['model'] . '.' . ':primary_key', 'IN', $this->_ids($models))->count();
 }
Esempio n. 21
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);
             }
         }
     }
 }
Esempio n. 22
0
 public function __construct()
 {
     parent::__construct();
     $this->_db = Database::instance('unit_testing');
 }
Esempio n. 23
0
 /**
  * Implementation of Jelly_Field_Supports_Save.
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $value
  * @param   boolean      $loaded
  * @return  void
  */
 public function save($model, $value, $loaded)
 {
     // Don't do anything on INSERTs when there is nothing in the value
     if (!$loaded and empty($value)) {
         return;
     }
     // Empty relations to the default value
     Jelly::query($this->foreign['model'])->where($this->foreign['model'] . '.' . $this->foreign['field'], '=', $model->id())->set(array($this->foreign['field'] => $this->foreign_default))->update();
     // Set the new relations
     if (!empty($value)) {
         // Update the ones in our list
         Jelly::query($this->foreign['model'])->where($this->foreign['model'] . '.' . ':primary_key', '=', $value)->set(array($this->foreign['field'] => $model->id()))->update();
     }
 }
Esempio n. 24
0
 /**
  * Logic to deal with uploading the image file and generating thumbnails according to
  * what has been specified in the $thumbnails array.
  *
  * @param   Validation   $validation
  * @param   Jelly_Model  $model
  * @param   Jelly_Field  $field
  * @return  bool
  */
 public function _upload(Validation $validation, $model, $field)
 {
     // Get the file from the validation object
     $file = $validation[$field];
     //no need upload if post helper isset
     if (!empty($_POST[self::$post_file_helper_prefix . $this->name])) {
         $this->_filename = $_POST[self::$post_file_helper_prefix . $this->name];
         return TRUE;
     }
     // Sanitize the filename
     $file['name'] = preg_replace('/[^a-z0-9-\\.]/', '-', strtolower($file['name']));
     // Strip multiple dashes
     $file['name'] = preg_replace('/-{2,}/', '-', $file['name']);
     // Upload a file?
     if (($filename = Upload::save($file, NULL, $this->path)) !== FALSE) {
         // Standardise slashes
         $filename = str_replace('\\', '/', $filename);
         // Chop off the original path
         // $value = str_replace($this->path, '', $filename);
         $value = str_replace(ltrim($_SERVER['DOCUMENT_ROOT'], '/'), '', $filename);
         // Ensure we have no leading slash
         if (is_string($value)) {
             $value = trim($value, '/');
         }
         // Garbage collect
         $this->_delete_old_file($model->original($this->name), $this->path);
         // Set the saved filename
         $this->_filename = '/' . $value;
         $_POST[self::$post_file_helper_prefix . $this->name] = $this->_filename;
     }
     return TRUE;
 }
Esempio n. 25
0
 protected function generate_field(Jelly_Model $model, &$fields, $field_id, $field, array $validation_errors = array(), $attrs = array(), $field_prefix = 'field-')
 {
     $field_id_attr = $field_prefix . $field->name;
     if (!$this->include_field($field, $model->id() == 0)) {
         return;
     }
     $id_attribs = array('attributes' => array('id' => $field_id_attr) + $attrs, 'name' => $field_id_attr);
     if ($field->prevent_edit) {
         $label = Form::label($field_id_attr, $field->label);
         $field_str = $field->display($model, $model->get($field_id));
         $fields[$label] = '<div class="non-editable">' . ($field_str == '' || $field_str == ' ' ? '&nbsp;' : $field_str) . '</div>';
     } else {
         $field_output = $model->input($field->name, $id_attribs);
         if ($field instanceof Field_HasManyUniquely) {
             $label = View::factory('jelly/field/hasmanyuniquely/header', array('label' => $field->label, 'is_sortable' => isset($field->sort_on) && $field->sort_on)) . '';
         } else {
             if ($field instanceof Field_BelongsTo && $field->edit_inline) {
                 $label = '<!-- ' . $field_id . ' -->';
                 $sub_model = $model->{$field_id};
                 $sub_meta = Jelly::meta($sub_model);
                 $field_output = View::factory('kadmium/fieldset_subedit', array('field_id' => $field_id, 'label' => $field->label, 'fields' => $this->generate_fields($sub_model, $sub_meta, 'field-' . $field_id . '-', $validation_errors)));
             } else {
                 $label = Form::label($field_id_attr, $field->label);
             }
         }
         $fields[$label] = $field_output;
         if (isset($validation_errors[$field_id])) {
             array_push($field->css_class, 'error');
             $fields[$label]->errors = $validation_errors[$field_id];
         }
     }
 }
Esempio n. 26
0
 public function update()
 {
     // Create a new token each time the token is saved
     $this->token = $this->create_token();
     return parent::save();
 }
Esempio n. 27
0
 public static function set(Jelly_Model $model, Formo $form, array $data = NULL)
 {
     $model->set($form->as_array('_value'));
 }
Esempio n. 28
0
 /**
  * Returns a pre-built Jelly model ready to be loaded
  *
  * @param   Jelly_Model  $model
  * @param   mixed        $value
  * @param   boolean      $loaded
  * @return  void
  */
 public function get($model, $value)
 {
     // If the value hasn't changed, we need to pull from the database
     if ($model->changed($this->name)) {
         return Jelly::select($this->foreign['model'])->where($this->foreign['column'], 'IN', $value);
     }
     $join_col1 = $this->through['model'] . '.' . $this->through['columns'][1];
     $join_col2 = $this->foreign['model'] . '.' . $this->foreign['column'];
     $where_col = $this->through['model'] . '.' . $this->through['columns'][0];
     return Jelly::select($this->foreign['model'])->join($this->through['model'])->on($join_col1, '=', $join_col2)->where($where_col, '=', $model->id());
 }
Esempio n. 29
0
 /**
  *
  * @access public
  * @param $column - Which field to get.
  * @return mixed
  */
 public function __get($column)
 {
     switch ($column) {
         case 'parent':
             return $this->parent();
         case 'parents':
             return $this->parents();
         case 'children':
             return $this->children();
         case 'first_child':
             return $this->children(FALSE, 'ASC', 1);
         case 'last_child':
             return $this->children(FALSE, 'DESC', 1);
         case 'siblings':
             return $this->siblings();
         case 'root':
             return $this->root();
         case 'leaves':
             return $this->leaves();
         case 'descendants':
             return $this->descendants();
             /*case 'left_column':
                   return $this->meta()->left_column;
               case 'right_column':
                   return $this->meta()->right_column;
               case 'level_column':
                   return $this->meta()->level_column;
               case 'scope_column':
                   return $this->meta()->scope_column; */
         /*case 'left_column':
               return $this->meta()->left_column;
           case 'right_column':
               return $this->meta()->right_column;
           case 'level_column':
               return $this->meta()->level_column;
           case 'scope_column':
               return $this->meta()->scope_column; */
         case 'db':
             return $this->meta()->db();
         case 'table':
             return $this->meta()->table();
         default:
             return parent::__get($column);
     }
 }