setOriginalValues ($id); $this->setInitialPrimaryKey ($id->{$this->primary_key}); } else { $this->setInitialPrimaryKey ($id); } parent::__construct ($id); } function __isset ($name) { $relations = array ('has_one', 'belongs_to', 'has_many', 'has_and_belongs_to_many', 'load_with'); foreach ($relations as $key) { if (in_array ($name, $this->$key)) { return true; } } return parent::__isset ($name); } function getId () { return $this->{$this->primary_key}; } function setId ($id) { $this->{$this->primary_key} = $id; } function getTableName () { return $this->db->table_prefix () . $this->table_name; } function getRawTableName () { return $this->table_name; } function getDatabase () { return $this->db; } function compare ($new = null) { $changes = array (); foreach ($this->object as $field => $value) { if ($value != $new->object[$field]) { $human_field_name = $this->getHumanFieldName ($field); if (isset ($human_field_name)) { $change = array ( 'field' => $human_field_name, 'to' => $this->getHumanValue ($field, $new->$field), ); if ($this->isExist ()) { $change['from'] = $this->getHumanValue ($field, $value); } $changes[] = $change; } } } if ($changes) { $log = ORM::factory ('editorlog'); if ($this->isExist ()) { $log->log_action = 'Changed'; $log->log_object = $this->getJournalName (); } else { $log->log_action = 'Created'; $log->log_object = $new->getJournalName (); } $log->log_changes = $this->encodeChanges ($changes); $log->save (); } } protected function isReadOnlyMode () { return Database::instance()->isReadOnlyMode (); } function save () { if (! $this instanceof Search_Model) { if ($this->isReadOnlyMode ()) { return; } } if ($this->isJournaling ()) { $original_class = get_class ($this); $original_object = new $original_class ($this->id); $result = parent::save (); $original_object->compare ($this); return $result; } return parent::save (); } function delete ($id = NULL) { if ($this->isReadOnlyMode ()) { return; } if ($this->isJournaling ()) { $log = ORM::factory ('editorlog'); if ($this->isExist ()) { $log->log_action = 'Deleted'; $log->log_object = $this->getJournalName (); } $changes = array (); foreach ($this->object as $field => $value) { $human_field_name = $this->getHumanFieldName ($field); if (isset ($human_field_name)) { $changes[] = array ( 'field' => $human_field_name, 'from' => $this->getHumanValue ($field, $value), ); } } $log->log_changes = $this->encodeChanges ($changes); $log->save (); } return parent::delete ($id); } function query ($query) { if ($this->isReadOnlyMode ()) { if (preg_match ('/^\s*(INSERT|REPLACE|DELETE|UPDATE)\b/is', $query)) { return; } } $args = func_get_args (); $real_query = call_user_func_array (array ($this, 'constructQuery'), $args); return $this->db->query ($real_query); } function constructQuery ($query) { $args = func_get_args (); array_shift ($args); $params = array (); foreach (new RecursiveIteratorIterator (new RecursiveArrayIterator ($args)) as $item) { $params[] = $item; } $parts = preg_split ("/\s+\?/", $query); $result_sql = ""; $i = 0; foreach ($parts as $part) { $result_sql .= $part; if (count ($parts) == $i+1) { break; } if (array_key_exists ($i, $params)) { $result_sql .= ' ' . $this->db->escape ($params[$i]); } $i++; } return $result_sql; } function renew ($initial=false) { if (!$this->loaded) { $this->id = $this->getInitialPrimaryKey (); } $this->save (); } function dump () { echo get_class ($this); echo "\n"; echo ($this->loaded) ? 'loaded' : 'not loaded'; echo "\n"; echo '
'; print_r ($this->changed); echo '
'; } function setInitialPrimaryKey ($initial_primary_key) { $this->initial_primary_key = $initial_primary_key; } function getInitialPrimaryKey () { return $this->initial_primary_key; } function hasInitialPrimaryKey () { return isset ($this->initial_primary_key); } function isExist () { return !empty ($this->{$this->primary_key}); } function exists () { return !empty ($this->{$this->primary_key}); } function resetField ($fieldname) { $table_name = $this->getTableName (); $this->query ("UPDATE `$table_name` SET `$fieldname` = ''"); } protected function deleteHasMany () { foreach ($this->has_many as $table) { foreach ($this->$table as $rec) { $rec->delete (); } } } function page ($page, $paginator = null) { $page = (int)$page; $this->limit ($paginator->items_per_page, ($paginator->current_page - 1) * $paginator->items_per_page); return $this; } protected function fieldHasBeenChanged ($field_name) { return isset ($this->changed [$field_name]); } private function isJournaling () { if ($this instanceof Journaling) { if (Zend_Session::isStarted ()) { $namespace = new Zend_Session_Namespace ('application'); return true; } } } private function encodeChanges ($data) { return base64_encode (serialize ($data)); } function getHumanFieldName ($field_name) { if (isset ($this->human_field_name[$field_name])) { return $this->human_field_name[$field_name]; } } function getJournalName () { $result = $this->journal_name; if ($this->isExist ()) { if (isset ($this->journal_field_name)) { $journal_field_name = $this->journal_field_name; } else { $journal_field_name = 'name'; } $result .= ' "' . $this->{$journal_field_name} . '"'; } return $result; } function getHumanValue ($field_name, $value) { if (isset ($this->human_field_values)) { if (array_key_exists ($field_name, $this->human_field_values)) { if (isset ($this->human_field_values[$field_name][$value])) { return $this->human_field_values[$field_name][$value]; } } } return $value; } function getTablePrefix () { return Kohana::config ('database.default.table_prefix'); } function getAllPrimaryKeys () { $table = $this->getTableName (); $rows = $this->query ("SELECT id FROM `$table`"); $result = array (); foreach ($rows as $row) { $result [] = $row->id; } return $result; } function getOriginalFieldValue ($field_name) { if (! $this->hasOriginalValues ()) { if (! $this->exists ()) { return; } $old = clone $this; $old->reload (); $old_values = $old->as_array (); settype ($old_values, 'object'); $this->setOriginalValues ($old_values); } return $this->getOriginalValues ()->$field_name; } function setOriginalValues ($original_values) { $this->original_values = $original_values; } function getOriginalValues () { return $this->original_values; } function hasOriginalValues () { return isset ($this->original_values); } function random () { $count_all = $this->count_all (); if ($count_all > 1) { mt_srand (preg_replace ('/.*\./', '', (string) microtime(true))); $offset = mt_rand (1, $count_all); $iterator = $this->limit (1, $offset-1)->find_all (); if ($iterator->count () > 0) { foreach ($iterator as $record) { return $record; } } } } }