public function displayPage($page = null) { if (is_null($page)) { $page = $this->pages(); $page = isset($page[0]) ? $page[0] : ''; switch ($page) { case '': $pageName = Cana::config()->defaults->page; break; default: $pageName = implode('/', $this->pages()); break; } } else { $pageName = $page; } if (getenv('DEBUG')) { error_log('>> DISPLAYING PAGE: ' . $pageName); } try { parent::displayPage($pageName == 'error' ? 'home' : $pageName); } catch (Exception $e) { $this->exception($e); } return $this; }
public function objectMap($a, $b = null) { // create a new object if not caching if (Cana::config()->cache->object === false) { $obj = new $a($b); } else { if (is_string($a)) { $t = new $a(); } // NOCACHE: if the first param is an object, and you gave us the id, use the id you gave us if (is_object($a) && (is_string($b) || is_int($b))) { $obj = $this->_objectMap[get_class($a)][$b] = $a; // CACHED: if the first param is an object, the second is an id, and we have it cached } elseif (is_object($a) && (is_string($b) || is_int($b)) && $this->_objectMap[get_class($a)][$a->{$b}]) { $obj = $this->_objectMap[get_class($a)][$a->{$b}]; // CACHED: if the first param is an object, and we have it cached } elseif (is_object($a) && method_exists($a, 'idVar') && $this->_objectMap[get_class($a)][$a->{$a->idVar()}]) { $obj = $this->_objectMap[get_class($a)][$a->{$a->idVar()}]; // NOCACHE: if the first param is an object with no other info, store it. these come from Cana_Table typicaly } elseif (is_object($a) && method_exists($a, 'idVar')) { $obj = $this->_objectMap[get_class($a)][$a->{$a->idVar()}] = $a; // CACHED: if the first param is the type of object, and the second one is the id } elseif (is_string($a) && (is_string($b) || is_int($b)) && $this->_objectMap[$a][$b]) { $obj = $this->_objectMap[$a][$b]; // CACHED: if the first param is the type of object, and the second one is the object and we didnt know that we already had it } elseif (is_string($a) && is_object($b) && method_exists($t, 'idVar') && $this->_objectMap[$a][$b->{$t->idVar()}]) { $obj = $this->_objectMap[$a][$b->{$t->idVar()}]; // NOCACHE: we dont have it, so make it and store it } elseif ($a) { $obj = new $a($b); if (!$this->_objectMap[get_class($obj)][$obj->{$obj->idVar()}]) { $this->_objectMap[get_class($obj)][$obj->{$obj->idVar()}] = $obj; } // NOCACHE: you didnt give us anything to work with } else { $obj = new Cana_Model(); } } // return an object of some type $t = null; return $obj; }
public static function gitVersion() { $v = shell_exec('cd ' . Cana::config()->dirs->root . ' && git rev-parse HEAD'); return trim($v); }
/** * Load the object with properties * * Passing in an object will populate $this with the current vars of that object * as public properties. Passing in an int id will load the object with the * table and key associated with the object. * * @param $id object|int */ public function load($id = null) { if (is_object($id)) { $node = $id; } elseif (is_array($id)) { $node = (object) $id; } else { if ($id) { if ($this->_jsonParsing) { $json = @json_decode($id); if (is_object($json)) { $node = $json; $id = $node->{$this->idVar()}; } } if (!$node) { $query = 'SELECT * FROM `' . $this->table() . '` WHERE `' . $this->idVar() . '`=:id LIMIT 1'; $node = $this->db()->get($query, ['id' => $id])->get(0); } if (!$node) { $node = new Cana_Model(); } if (!isset($this->_noId)) { $node->id = $id; } } else { // fill the object with blank properties based on the fields of that table foreach ($this->fields() as $field) { $this->{$field->field} = $this->{$field->field} ? $this->{$field->field} : ''; } } } if (isset($node)) { if (isset($node->id)) { $this->id = $node->id; } foreach (get_object_vars($node) as $var => $value) { $this->{$var} = $value; } if (!isset($this->id) && $this->idVar()) { $id_var = $this->idVar(); } if (!isset($this->id) && isset($node->{$id_var})) { $this->id = $node->{$id_var}; } } foreach ($this->fields() as $field) { switch ($field->type) { case 'int': $this->{$field->field} = (int) $this->{$field->field}; break; case 'boolean': $this->{$field->field} = $this->{$field->field} ? true : false; break; } } if (Cana::config()->cache->object !== false) { Cana::factory($this); } return $this; }
public static function save($object, $options = array()) { $objectType = get_class($object); Cana::config()->cache->object = false; $current = new $objectType($object->{$object->idVar()}); Cana::config()->cache->object = true; $oldVals = array(); $newVals = array(); foreach ($current->properties() as $var => $val) { if (isset($object->{$var})) { if ($object->{$var} != $current->{$var}) { $oldVals[$var] = $current->{$var}; $newVals[$var] = $object->{$var}; } } } if (isset($options['custom'])) { foreach ($options['custom'] as $key => $customOption) { $oldVals[$key] = $customOption['old']; $newVals[$key] = $customOption['new']; } } $time = isset($options['timestamp']) ? $options['timestamp'] : date('Y-m-d H:i:s'); // set $set = Cana_Table::fromTable(isset($options['set']['table']) ? $options['set']['table'] : $object->table() . '_change_set', isset($options['set']['id']) ? $options['set']['id'] : $object->idVar() . '_change_set', $object->db()); $set->strip(); $set->timestamp = $time; if (isset($options['author_id'])) { $authorVar = $options['author_id']; } elseif (c::admin()->id_admin) { $authorVar = 'id_admin'; } elseif (c::user()->id_user) { $authorVar = 'id_user'; } elseif (isset($options['id_admin'])) { $authorVar = 'id_admin'; } if ($authorVar) { if (isset($options['author'])) { $author = $options['author']; } elseif (c::admin()->id_admin) { $author = c::admin()->id_admin; } elseif (c::user()->id_user) { $author = c::user()->id_user; // there is some case where the change is made by a cron task } elseif (isset($options['id_admin'])) { $author = $options['id_admin']; } if ($author) { $set->{$authorVar} = $author; } } $set->{$object->idVar()} = $object->{$object->idVar()}; // changes. only save set if theres at least one change if (count($oldVals)) { $set->save(); foreach ($oldVals as $field => $oldVal) { $change = Cana_Table::fromTable(isset($options['change']['table']) ? $options['change']['table'] : $object->table() . '_change', isset($options['change']['id']) ? $options['change']['id'] : $object->{$object->idVar()} . '_change', $object->db()); $change->strip(); $change->{$set->idVar()} = $set->{$set->idVar()}; $change->field = $field; $change->new_value = $newVals[$field]; $change->old_value = $oldVals[$field]; $change->save(); } } return $set; }