/** * 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()); } }
/** * 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()); }
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 == ' ' ? ' ' : $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]; } } }
/** * 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>'; }
/** * 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()); }
/** * 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; }
/** * 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(); } }
/** * 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(); }