Esempio n. 1
0
 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]));
             }
         }
     }
 }
Esempio n. 2
0
 /**
  * Add validation so that if you change any fields that are considered frozen,
  * it would add an error.
  * In case of associations, if the count of the items has been changed,
  * also add an error.
  *
  * @param  Jam_Model      $model
  * @param  Jam_Event_Data $data
  */
 public function model_after_check(Jam_Model $model, Jam_Event_Data $data)
 {
     if ($model->loaded() and $model->is_frozen() and !$model->is_just_frozen()) {
         foreach ($this->_associations as $name) {
             if ($model->meta()->association($name) instanceof Jam_Association_Collection and count($model->{$name}) !== count($model->{$name}->original())) {
                 $model->errors()->add($name, 'frozen');
             }
         }
         foreach ($this->_fields as $name) {
             if ($model->changed($name)) {
                 $model->errors()->add($name, 'frozen');
             }
         }
     }
 }
 /**
  * Before the model is deleted, and the depenedent option is set, remove the dependent models
  * @param  Jam_Model $model 
  */
 public function model_before_delete(Jam_Model $model)
 {
     if ($model->loaded()) {
         switch ($this->children_dependent) {
             case Jam_Association::DELETE:
                 foreach ($model->{$this->name} as $item) {
                     $item->delete();
                 }
                 break;
             case Jam_Association::ERASE:
                 $this->erase_query($model)->execute($model->meta()->db());
                 break;
         }
         $this->erase_branches_query($model)->execute($model->meta()->db());
     }
 }
Esempio n. 4
0
 public function model_after_delete(Jam_Model $model)
 {
     if ($model->loaded() and $this->join_table_dependent) {
         $this->erase_query($model)->execute(Jam::meta($this->model)->db());
     }
 }
Esempio n. 5
0
 /**
  * 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), '/') . '/';
 }
Esempio n. 6
0
 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());
     }
 }