public function model_call_update_token(Jam_Model $model, Jam_Event_Data $data) { do { $model->{$this->_field} = $this->new_token(); } while (Jam::all($model->meta()->model())->where($this->_field, '=', $model->{$this->_field})->count_all() > 0); $data->return = $model; }
/** * Persist this collection in the database * @param Jam_Model $model */ public function model_after_save(Jam_Model $model) { if ($model->changed($this->name) and $collection = $model->{$this->name} and $collection->changed()) { $collection->save_changed(); $this->save($model, $collection); } }
public function add_items_query(Jam_Model $model, array $ids) { $query = DB::insert($this->join_table)->columns(array($this->term_key, $this->item_polymorphic_key, $this->item_key)); foreach ($ids as $id) { $query->values(array($model->id(), $this->foreign_model, $id)); } return $query; }
public function model_call_ansestors(Jam_Model $model, Jam_event_data $data) { $path_ids = $model->path_ids(); if ($path_ids) { $data->return = Jam::all($model)->where($model->meta()->primary_key(), 'IN', $path_ids); } else { $data->return = Jam_Query_Builder_Collection::factory($model)->load_fields(array()); } }
public function model_call_change_upload_server(Jam_Model $model, Jam_Event_Data $data, $name, $server) { if ($this->_name == $name) { if ($old_server = $model->{$this->_dynamic_server} and $old_server !== $server) { $model->{$this->_name}->move_to_server($server); $model->update_fields($this->_dynamic_server, $server); } } }
public function model_before_check(Jam_Model $model) { foreach ($this->_parents as $child => $parent) { $child_association = $model->meta()->association_insist($child); $parent_association = $model->meta()->association_insist($parent); if ($model->changed($child) and $model->{$child} and !$model->{$child}->parent and $model->{$parent}) { $model->{$child}->parent = $model->{$parent}; } } }
public function model_after_save(Jam_Model $model, Jam_Event_Data $data, $changed) { if ($value = Arr::get($changed, $this->name)) { if ($item = $model->{$this->name}) { $old_path = $model->children_path(); $model->update_fields('path', $item->children_path()); $new_path = $model->children_path(); Jam::update($model)->where('path', 'LIKE', $old_path . '%')->update_children($old_path, $new_path)->execute(); } } }
public function model_before_check(Jam_Model $model) { if (!$model->loaded() or $model->changed($this->_ip_field)) { $info = $this->geoip_record($model->{$this->_ip_field}); foreach ($this->_locations as $association => $info_name) { if (isset($info[$info_name])) { $model->{$association} = Jam::find_or_build('location', array('name' => $info[$info_name])); } } } }
/** * Checks if user has permission. * * @param mixed $permission Permission name string, perission Jam object, permission id * @return boolean */ public function model_call_has_permission(Jam_Model $user, Jam_Event_Data $data, $permission) { if ($permission instanceof Model_Permission) { $permission = $permission->name(); } elseif (is_numeric($permission)) { $permission = Jam::factory('permission', $permission)->name(); } if ($user->is_superadmin()) { $data->return = TRUE; $data->stop = TRUE; } else { $data->return = array_key_exists($permission, $user->permissions()); $data->stop = TRUE; } }
public function __construct($id = NULL) { parent::__construct($id); if (mt_rand(1, 100) === 1) { Jam::delete($this)->expired()->execute(); } }
/** * Persist this collection in the database * @param Jam_Model $model */ public function model_after_delete(Jam_Model $model) { $path = $model->children_path(); Jam::update($model)->where('path', 'LIKE', $path . '%')->update_children($path, '')->execute(); }
protected function assign_item(Jam_Model $item, $foreign_key, $polymorphic_key, $inverse_of) { $item->{$this->foreign_key} = $foreign_key; if ($this->is_polymorphic()) { $item->{$this->polymorphic_key} = $polymorphic_key; } if ($this->inverse_of) { $item->retrieved($this->inverse_of, $inverse_of); } if ($this->as) { $item->retrieved($this->as, $inverse_of); } }
/** * Get the localized path for a given model, so that there are less filename conflicts and files are easily located, * for example the default path is model/id so that Model_Image(1) images will be stored as images/1/file.jpg * * @param Jam_Model $model the model for the context * @return string */ protected function path(Jam_Model $model) { $converted_params = array(); preg_match_all('#\\:[a-zA-z]*#', $this->path, $params); foreach ($params[0] as $param) { switch ($param) { case ':column': $converted_params[$param] = $this->column; break; case ':model': $converted_params[$param] = $this->model; break; case ':name': $converted_params[$param] = $this->name; break; case ':id': $converted_params[$param] = $model->loaded() ? $model->id() : 'new'; break; case ':id_group': $converted_params[$param] = $model->loaded() ? ceil($model->id() / 10000) : 'new'; break; default: $converted_params[$param] = $model->{str_replace(':', '', $param)}; } } return rtrim(strtr($this->path, $converted_params), '/') . '/'; }
public function execute(Jam_Model $model) { foreach ($this->callbacks() as $method => $model_names) { $children = Jam_Behavior_Cascade::get_current_children($model->meta()->model(), $model_names); $models = Jam_Behavior_Cascade::collect_models($model, (array) $children); call_user_func($method, $model, $models); } }
private function _model_messages_dump(Jam_Model $model) { $messages = array(); foreach ($model->errors() as $attribute_name => $errors) { if ($model->meta()->association($attribute_name) instanceof Jam_Association_Collection) { foreach ($model->{$attribute_name} as $i => $item) { if (!$item->is_valid()) { $messages[] = UTF8::ucfirst(Inflector::humanize($attribute_name)) . ' (' . $i . '): ' . join(', ', $this->_model_messages_dump($item)); } } } elseif ($model->meta()->association($attribute_name) and $model->{$attribute_name}) { $messages[] = UTF8::ucfirst(Inflector::humanize($attribute_name)) . ': ' . join(', ', $this->_model_messages_dump($model->{$attribute_name})); } else { foreach ($errors as $error => $params) { $messages[] = Jam_Errors::message($model->meta()->errors_filename(), $attribute_name, $error, Arr::merge($params, array(':model' => $model->meta()->model(), ':attribute' => Jam_Errors::attribute_label($model->meta(), $attribute_name)))); } } } return $messages; }
/** * Generate a query to add models from the association (without deleting them), for specific ids * @param Jam_Model $model * @param array $ids * @return Database_Query */ public function add_items_query(Jam_Model $model, array $ids) { $query = DB::insert($this->branches_table)->columns(array($this->ansestor_key, $this->descendant_key, $this->depth_key)); foreach ($ids as $id) { foreach ($this->ansestors_query($model->id())->execute($model->meta()->db()) as $ansestor) { $query->values(array($ansestor[$this->ansestor_key], $id, $ansestor[$this->depth_key] + 1)); foreach ($this->descendants_query($id)->where($this->depth_key, '>', 0)->execute($model->meta()->db()) as $descendant) { $query->values(array($ansestor[$this->ansestor_key], $descendant[$this->descendant_key], $ansestor[$this->depth_key] + $descendant[$this->depth_key] + 1)); } } } return $query; }
/** * Check if the object has been frozen, but is not yet saved. * Go up the chain of parents until it reaches the root, whose * is_just_frozen flag is returned. * * @param Jam_Model $model * @param Jam_Event_Data $data * @return boolean */ public function model_call_is_just_frozen(Jam_Model $model, Jam_Event_Data $data) { if ($this->_parent) { $data->return = $model->get_insist($this->_parent)->is_just_frozen(); } else { $data->return = $model->is_just_frozen; } }
public function query_builder($type, Jam_Model $model) { $query = call_user_func("Jam::{$type}", $this->foreign_model)->where($this->foreign_key, '=', $model->id()); if ($this->is_polymorphic()) { $query->where($this->polymorphic_key, '=', $model->meta()->model()); } return $query; }
public function model_call_matches_slug_insist(Jam_Model $model, Jam_Event_Data $data, $slug) { if (!$model->matches_slug($slug)) { throw new Jam_Exception_Slugmismatch("Stale slug :slug for model :model ", $model, $slug); } $data->return = TRUE; }
public static function _slug(Jam_Model $model) { return $model->name(); }
protected static function render_field(Jam_Model $item, Jam_Field $field) { $value = $item->{$field->name}; if ($field instanceof Jam_Field_Integer) { return HTML::chars(number_format($value)); } elseif ($field instanceof Jam_Field_Float) { return HTML::chars(number_format($value, 2)); } elseif ($field instanceof Jam_Field_Boolean) { return $value ? '<i class="icon-ok"></i>' : ''; } elseif ($field instanceof Jam_Field_Serialized) { return Debug::vars($value); } elseif ($field instanceof Jam_Field_Timestamp) { if (!$value) { return '-'; } $time = is_numeric($value) ? $value : strtotime($value); return '<span title="' . date('j M Y H:i:s', $time) . '">' . Tart_Html::date_span($time) . '</span>'; } elseif ($field instanceof Jam_Field_Weblink) { return Text::limit_chars(HTML::chars($value), 30) . ' ' . HTML::anchor($value, '<i class="icon-share-alt"></i>'); } elseif ($field instanceof Jam_Field_Text) { return Text::widont(Text::limit_chars(HTML::chars($value), 40)); } elseif ($field instanceof Jam_Field_Upload) { return HTML::image($value->url(TRUE), array('class' => 'img-polaroid', 'alt' => $item->name())); } else { return HTML::chars($value); } }
public function model_after_delete(Jam_Model $model) { DB::delete($this->_branches_table)->where($this->_ansestor_key, '=', $model->id())->where($this->_descendant_key, '=', $model->id())->execute($model->meta()->db()); }
public function item_get(Jam_Model $model, Jam_Model $item) { if (!$item->loaded() and !$item->vocabulary_id and count($this->vocabulary_ids()) === 1) { $item->{$this->vocabulary_foreign_key} = current($this->vocabulary_ids()); } }
/** * Perform validation on the belonging model, if it was changed. * @param Jam_Model $model * @param Jam_Event_Data $data * @param array $changed */ public function model_after_check(Jam_Model $model, Jam_Event_Data $data, $changed) { if ($value = Arr::get($changed, $this->name) and Jam_Association::is_changed($value)) { if (!$model->{$this->name}->is_validating() and !$model->{$this->name}->check()) { $model->errors()->add($this->name, 'association', array(':errors' => $model->{$this->name}->errors())); } } }
private function _is_scope_changed(Jam_Model $model) { if (!$this->_scope) { return FALSE; } foreach ((array) $this->_scope as $scope_field) { if ($model->changed($scope_field)) { return TRUE; } } return FALSE; }
/** * $model->restore_delete() Perform this to "undelete" a model * * @param Jam_Model $model * @param Jam_Event_Data $data */ public function model_call_restore_delete(Jam_Model $model) { Jam::update($this->_model)->where_key($model->id())->deleted(Jam_Behavior_Paranoid::DELETED)->value($this->_field, FALSE)->execute(); }
public function set_query(Jam_Model $model, $ansestor_id) { $query = DB::insert($this->branches_table)->columns(array($this->ansestor_key, $this->descendant_key, $this->depth_key)); foreach ($this->ansestors_query($ansestor_id)->execute($model->meta()->db()) as $ansestor) { $query->values(array($ansestor[$this->ansestor_key], $model->id(), $ansestor[$this->depth_key] + 1)); } return $query; }