From 702cf14bb3cd4c5ae17b6b5e774ded2e3776731d Mon Sep 17 00:00:00 2001 From: dogmatic Date: Wed, 13 Jan 2010 21:35:42 +0200 Subject: [PATCH] compleded routing setup from the database. [#100] --- config/routes.php | 63 +- extensions/libs/models/behaviors/ordered.php | 548 +++++++++--------- infinitas/management/models/route.php | 148 +++++ .../management/views/routes/admin_add.ctp | 12 +- .../management/views/routes/admin_edit.ctp | 60 +- .../management/views/routes/admin_index.ctp | 27 +- 6 files changed, 494 insertions(+), 364 deletions(-) diff --git a/config/routes.php b/config/routes.php index bec2b22d6..efde37627 100644 --- a/config/routes.php +++ b/config/routes.php @@ -1,41 +1,38 @@ 'installer', 'controller' => 'install', 'action' => 'index')); -} + /** + * redirect to the installer if there is nothing + */ + if (!file_exists(APP . 'config' . DS . 'database.php')) { + Router::connect('/', array('plugin' => 'installer', 'controller' => 'install', 'action' => 'index')); + } -Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); + $routes = Cache::read('routes', 'core'); -/** - * ...and connect the rest of 'Pages' controller's urls. - */ -Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); + if (!$routes) { + $routes = Classregistry::init('Management.Route')->getRoutes(); + if (empty($routes)) { + } + } -/** - * management routes - */ -Router::connect('/admin', array('plugin' => 'management', 'controller' => 'management', 'action' => 'dashboard', 'prefix' => 'admin', 'admin' => true)); -Router::connect('/admin/management', array('plugin' => 'management', 'controller' => 'management', 'action' => 'dashboard', 'prefix' => 'admin', 'admin' => true)); + foreach($routes as $route ){ + //Router::connect($route['Route']['url'], $route['Route']['values'], $route['Route']['regex'] ); + //Router::connect('/p/:year/:month/:day', array('plugin'=>'blog','controller'=>'posts','day'=>null,'admin'=>null), array('year'=>'[12][0-9]{3}','month'=>'0[1-9]|1[012]','day'=>'0[1-9]|[12][0-9]|3[01]') ); + } + $db = array( + 'url' => $routes[0]['Route']['url'], + 'values' => $routes[0]['Route']['values'], + 'regex' => $routes[0]['Route']['regex'] -/** - * blog routes - */ -Router::connect('/admin/blog', array('plugin' => 'blog', 'controller' => 'posts', 'action' => 'dashboard', 'prefix' => 'admin', 'admin' => true)); -Router::connect('/blog', array('plugin' => 'blog', 'controller' => 'posts')); + ); -/** - * newsletter routes - */ -Router::connect('/admin/newsletter', array('plugin' => 'newsletter', 'controller' => 'newsletters', 'action' => 'dashboard', 'prefix' => 'admin', 'admin' => true)); -Router::connect('/newsletter', array('plugin' => 'newsletter', 'controller' => 'newsletters')); - -/** - * cms routes - */ -Router::connect('/admin/cms', array('plugin' => 'cms', 'controller' => 'categories', 'action' => 'dashboard', 'prefix' => 'admin', 'admin' => true)); -// Router::connect('/admin/cms/:controller/:action/*', array( 'plugin' => 'cms', 'admin' => true ) ); -Router::connect('/cms', array('plugin' => 'cms', 'controller' => 'contentFrontpages')); + $code = array( + 'url' => '/p/:year/:month/:day', + 'values' => array('plugin'=>'blog','controller'=>'posts','day'=>null,'admin'=>null), + 'regex' => array('year'=>'[12][0-9]{3}','month'=>'0[1-9]|1[012]','day'=>'0[1-9]|[12][0-9]|3[01]') + ); + var_dump( $db ); + echo '
'; + var_dump( $code ); + exit; ?> \ No newline at end of file diff --git a/extensions/libs/models/behaviors/ordered.php b/extensions/libs/models/behaviors/ordered.php index f66199fa1..58fc8ad66 100644 --- a/extensions/libs/models/behaviors/ordered.php +++ b/extensions/libs/models/behaviors/ordered.php @@ -42,9 +42,9 @@ * To set up this behavior we add this property to the Page model : * * var $actsAs = array('Ordered' => array( -* 'field' => 'page_number', -* 'foreign_key' => 'book_id' -* )); +* 'field' => 'page_number', +* 'foreign_key' => 'book_id' +* )); * * Now when you save a new page (no changes needed to action or view, * but leave page_number out of the form), it will be added to the end @@ -81,8 +81,8 @@ * * You can also use actions to find out if the node is first or last page : * -* - isfirst($id) -* - islast($id) +* - isfirst($id) +* - islast($id) * * And a last feature is the ability to sort the list by any field * you want and have it set weights based on that. You do that like this : @@ -103,8 +103,8 @@ class OrderedBehavior extends ModelBehavior { * field : (string) The field to be ordered by. * * foreign_key : (string) The field to identify one SET by. - * Each set has their own order (ie they start at 1). - * Set to FALSE to not use this feature (and use only 1 set) + * Each set has their own order (ie they start at 1). + * Set to FALSE to not use this feature (and use only 1 set) */ var $_defaults = array( 'field' => 'ordering', @@ -196,9 +196,9 @@ function moveto(&$Model, $id = null, $new_weight = null) { $action = array($this->settings['field'] => $this->settings['field'] . ' - 1'); $conditions[$this->settings['field'] . ' <='] = $new_weight; $conditions[$this->settings['field'] . ' >' ] = $old_weight; - }else { // $new_weight < $old_weight - // move all where weight >= new_weight AND < old_weight down one (+1) - $action = array($this->settings['field'] => $this->settings['field'] . ' + 1'); + } else { // $new_weight < $old_weight + // move all where weight >= new_weight AND < old_weight down one (+1) + $action = array($this->settings['field'] => $this->settings['field'] . ' + 1'); $conditions[$this->settings['field'] . ' >='] = $new_weight; $conditions[$this->settings['field'] . ' <' ] = $old_weight; } @@ -257,7 +257,7 @@ function moveup(&$Model, $id = null, $number = 1) { $id = $Model->id; } elseif (!empty($Model->data) && isset($Model->data[$Model->alias][$Model->primaryKey])) { $id = $Model->data[$Model->alias][$Model->primaryKey]; - }else { + } else { return false; } } @@ -267,7 +267,7 @@ function moveup(&$Model, $id = null, $number = 1) { } if (is_numeric($number)) { if ($number == 1) { // move 1 space - $previous = $this->_previous($Model); + $previous = $this->_previous($Model); if (!$previous) { return false; } @@ -280,287 +280,287 @@ function moveup(&$Model, $id = null, $number = 1) { return $Model->saveAll($data, array('validate' => false)); } elseif ($number < 1) { // cant move 0 or negative spaces + return false; + } else { // move Model up N spaces UP + if ($this->settings['foreign_key']) { + $conditions = array($this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] + ); + } else { + $conditions = array(); + } + // find the one occupying new space and its weight + $new_weight = $Model->data[$Model->alias][$this->settings['field']] - $number; + // check if new weight is possible. else move last + if (!$this->_findByWeight($Model, $new_weight)) { return false; - }else { // move Model up N spaces UP - if ($this->settings['foreign_key']) { - $conditions = array($this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] - ); - }else { - $conditions = array(); - } - // find the one occupying new space and its weight - $new_weight = $Model->data[$Model->alias][$this->settings['field']] - $number; - // check if new weight is possible. else move last - if (!$this->_findByWeight($Model, $new_weight)) { - return false; - } - $conditions[$this->settings['field'] . ' >='] = $new_weight; - $conditions[$this->settings['field'] . ' <'] = $old_weight; - // increase weight of all where weight > new weight and id != Model.id - $Model->updateAll(array($this->settings['field'] => $this->settings['field'] . ' + 1'), $conditions); - // set Model weight to new weight and save it - $Model->data[$Model->alias][$this->settings['field']] = $new_weight; - return $Model->save(null, false); } - } elseif (is_bool($number) && $number && $Model->data[$Model->alias][$this->settings['field']] != 1) { // move Model FIRST; - if ($this->settings['foreign_key']) { - $conditions = array($this->settings['field'] . ' <' => $old_weight, - $this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] - ); - }else { - $conditions = array($this->settings['field'] . ' <' => $old_weight); - } - $Model->id = $Model->data[$Model->alias][$Model->primaryKey]; - $Model->saveField($this->settings['field'], 0); + $conditions[$this->settings['field'] . ' >='] = $new_weight; + $conditions[$this->settings['field'] . ' <'] = $old_weight; + // increase weight of all where weight > new weight and id != Model.id + $Model->updateAll(array($this->settings['field'] => $this->settings['field'] . ' + 1'), $conditions); + // set Model weight to new weight and save it + $Model->data[$Model->alias][$this->settings['field']] = $new_weight; + return $Model->save(null, false); + } + } elseif (is_bool($number) && $number && $Model->data[$Model->alias][$this->settings['field']] != 1) { // move Model FIRST; + if ($this->settings['foreign_key']) { + $conditions = array($this->settings['field'] . ' <' => $old_weight, + $this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] + ); + } else { + $conditions = array($this->settings['field'] . ' <' => $old_weight); + } + $Model->id = $Model->data[$Model->alias][$Model->primaryKey]; + $Model->saveField($this->settings['field'], 0); - $Model->updateAll( - array(// update - $this->settings['field'] => $this->settings['field'] . ' + 1' - ), - $conditions - ); + $Model->updateAll( + array(// update + $this->settings['field'] => $this->settings['field'] . ' + 1' + ), + $conditions + ); - return true; - }else { // $number is neither a number nor a bool - return false; - } + return true; + } else { // $number is neither a number nor a bool + return false; + } + } + + /** + * Reorder the node, by moving it $number spaces down. Defaults to 1 + * + * If the node is the last node (or less then $number spaces from last) + * this method will return false. + * + * @param AppModel $Model + * @param mixed $id The ID of the record to move + * @param mixed $number how many places to move the node or true to move to last position + * @return boolean true on success, false on failure + * @access public + */ + function movedown(&$Model, $id = null, $number = 1) { + if (!$id) { + if ($Model->id) { + $id = $Model->id; + } elseif (!empty($Model->data) && isset($Model->data[$Model->alias][$Model->primaryKey])) { + $id = $Model->data[$Model->alias][$Model->primaryKey]; + } else { + return false; } + } + $old_weight = $this->_read($Model, $id); + if (empty($Model->data)) { + return false; + } + if (is_numeric($number)) { + if ($number == 1) { // move node 1 space down + $next = $this->_next($Model); + if (!$next) { // it is the last node + return false; + } + // switch the node's weight around + $Model->data[$Model->alias][$this->settings['field']] = $next[$Model->alias][$this->settings['field']]; - /** - * Reorder the node, by moving it $number spaces down. Defaults to 1 - * - * If the node is the last node (or less then $number spaces from last) - * this method will return false. - * - * @param AppModel $Model - * @param mixed $id The ID of the record to move - * @param mixed $number how many places to move the node or true to move to last position - * @return boolean true on success, false on failure - * @access public - */ - function movedown(&$Model, $id = null, $number = 1) { - if (!$id) { - if ($Model->id) { - $id = $Model->id; - } elseif (!empty($Model->data) && isset($Model->data[$Model->alias][$Model->primaryKey])) { - $id = $Model->data[$Model->alias][$Model->primaryKey]; - }else { - return false; - } + $next[$Model->alias][$this->settings['field']] = $old_weight; + // create an array of the two nodes and save them + $data[0] = $Model->data; + $data[1] = $next; + return $Model->saveAll($data, array('validate' => false)); + } elseif ($number < 1) { // cant move 0 or negative number of spaces + return false; + } else { // move Model up N spaces DWN + if ($this->settings['foreign_key']) { + $conditions = array($this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] + ); + } else { + $conditions = array(); } - $old_weight = $this->_read($Model, $id); - if (empty($Model->data)) { + // find the one occupying new space and its weight + $new_weight = $Model->data[$Model->alias][$this->settings['field']] + $number; + // check if new weight is possible. else move last + if (!$this->_findByWeight($Model, $new_weight)) { return false; } - if (is_numeric($number)) { - if ($number == 1) { // move node 1 space down - $next = $this->_next($Model); - if (!$next) { // it is the last node - return false; - } - // switch the node's weight around - $Model->data[$Model->alias][$this->settings['field']] = $next[$Model->alias][$this->settings['field']]; - - $next[$Model->alias][$this->settings['field']] = $old_weight; - // create an array of the two nodes and save them - $data[0] = $Model->data; - $data[1] = $next; - return $Model->saveAll($data, array('validate' => false)); - } elseif ($number < 1) { // cant move 0 or negative number of spaces - return false; - }else { // move Model up N spaces DWN - if ($this->settings['foreign_key']) { - $conditions = array($this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] - ); - }else { - $conditions = array(); - } - // find the one occupying new space and its weight - $new_weight = $Model->data[$Model->alias][$this->settings['field']] + $number; - // check if new weight is possible. else move last - if (!$this->_findByWeight($Model, $new_weight)) { - return false; - } - // increase weight of all where weight > new weight and id != Model.id - $conditions[$this->settings['field'] . ' <='] = $new_weight; - $conditions[$this->settings['field'] . ' >'] = $old_weight; + // increase weight of all where weight > new weight and id != Model.id + $conditions[$this->settings['field'] . ' <='] = $new_weight; + $conditions[$this->settings['field'] . ' >'] = $old_weight; - $Model->updateAll(array($this->settings['field'] => $this->settings['field'] . ' - 1'), $conditions); - // set Model weight to new weight and save it - $Model->data[$Model->alias][$this->settings['field']] = $new_weight; - return $Model->save(null, false); - } - } elseif (is_bool($number) && $number) { // move Model LAST; - if ($this->settings['foreign_key']) { - $conditions = array($this->settings['field'] . ' >' => $old_weight, - $this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] - ); - }else { - $conditions = array($this->settings['field'] . ' >' => $old_weight); - } - // get highest weighted row - $highest = $this->_highest($Model); - // check of Model is allready highest - if ($highest[$Model->alias][$Model->primaryKey] == $Model->data[$Model->alias][$Model->primaryKey]) { - return false; - } - // Save models as highest +1 - $Model->saveField($this->settings['field'], $highest[$Model->alias][$this->settings['field']] + 1); - // updated all by taking off 1 - $Model->updateAll( - array(// action - $this->settings['field'] => $this->settings['field'] . ' - 1' - ), - $conditions - ); + $Model->updateAll(array($this->settings['field'] => $this->settings['field'] . ' - 1'), $conditions); + // set Model weight to new weight and save it + $Model->data[$Model->alias][$this->settings['field']] = $new_weight; + return $Model->save(null, false); + } + } elseif (is_bool($number) && $number) { // move Model LAST; + if ($this->settings['foreign_key']) { + $conditions = array($this->settings['field'] . ' >' => $old_weight, + $this->settings['foreign_key'] => $Model->data[$Model->alias][$this->settings['foreign_key']] + ); + } else { + $conditions = array($this->settings['field'] . ' >' => $old_weight); + } + // get highest weighted row + $highest = $this->_highest($Model); + // check of Model is allready highest + if ($highest[$Model->alias][$Model->primaryKey] == $Model->data[$Model->alias][$Model->primaryKey]) { + return false; + } + // Save models as highest +1 + $Model->saveField($this->settings['field'], $highest[$Model->alias][$this->settings['field']] + 1); + // updated all by taking off 1 + $Model->updateAll( + array(// action + $this->settings['field'] => $this->settings['field'] . ' - 1' + ), + $conditions + ); - return true; - }else { // $number is neither a number nor a bool - return false; - } - } + return true; + } else { // $number is neither a number nor a bool + return false; + } + } - /** - * Returns true if the specified item is the first item - * - * @param Model $Model - * @param Int $id - * @return Boolean , true if it is the first item, false if not - */ - function isfirst(&$Model, $id = null) { - if (!$id) { - if ($Model->id) { - $id = $Model->id; - } elseif (!empty($Model->data) && isset($Model->data[$Model->alias][$Model->primaryKey])) { - $id = $Model->id = $Model->data[$Model->alias][$Model->primaryKey]; - }else { - return false; - } - }else { - $Model->id = $id; - } - $Model->read(); + /** + * Returns true if the specified item is the first item + * + * @param Model $Model + * @param Int $id + * @return Boolean , true if it is the first item, false if not + */ + function isfirst(&$Model, $id = null) { + if (!$id) { + if ($Model->id) { + $id = $Model->id; + } elseif (!empty($Model->data) && isset($Model->data[$Model->alias][$Model->primaryKey])) { + $id = $Model->id = $Model->data[$Model->alias][$Model->primaryKey]; + } else { + return false; + } + } else { + $Model->id = $id; + } + $Model->read(); - $first = $this->_read($Model, $id); - if ($Model->data[$Model->alias][$this->settings['field']] == 1) { - return true; - }else { - return false; - } - } + $first = $this->_read($Model, $id); + if ($Model->data[$Model->alias][$this->settings['field']] == 1) { + return true; + } else { + return false; + } + } - /** - * Returns true if the specified item is the last item - * - * @param Model $Model - * @param Int $id - * @return Boolean , true if it is the last item, false if not - */ - function islast(&$Model, $id = null) { - if (!$id) { - if ($Model->id) { - $id = $Model->id; - } elseif (!empty($Model->data) && isset($Model->data[$Model->alias][$Model->primaryKey])) { - $id = $Model->id = $Model->data[$Model->alias][$Model->primaryKey]; - }else { - return false; - } - }else { - $Model->id = $id; - } - $Model->read(); - $last = $this->_highest($Model); - if ($last[$Model->alias][$Model->primaryKey] == $id) { - return true; - }else { - return false; - } - } + /** + * Returns true if the specified item is the last item + * + * @param Model $Model + * @param Int $id + * @return Boolean , true if it is the last item, false if not + */ + function islast(&$Model, $id = null) { + if (!$id) { + if ($Model->id) { + $id = $Model->id; + } elseif (!empty($Model->data) && isset($Model->data[$Model->alias][$Model->primaryKey])) { + $id = $Model->id = $Model->data[$Model->alias][$Model->primaryKey]; + } else { + return false; + } + } else { + $Model->id = $id; + } + $Model->read(); + $last = $this->_highest($Model); + if ($last[$Model->alias][$Model->primaryKey] == $id) { + return true; + } else { + return false; + } + } - function _findbyweight(&$Model, $weight) { - $conditions = array($this->settings['field'] => $weight); - $fields = array($Model->primaryKey, $this->settings['field']); - if ($this->settings['foreign_key']) { - $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; - $fields[] = $this->settings['foreign_key']; - } - return $Model->find('first', array( - 'conditions' => $conditions, - 'order' => $this->settings['field'] . ' DESC', - 'fields' => $fields - )); - } + function _findbyweight(&$Model, $weight) { + $conditions = array($this->settings['field'] => $weight); + $fields = array($Model->primaryKey, $this->settings['field']); + if ($this->settings['foreign_key']) { + $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; + $fields[] = $this->settings['foreign_key']; + } + return $Model->find('first', array( + 'conditions' => $conditions, + 'order' => $this->settings['field'] . ' DESC', + 'fields' => $fields + )); + } - function _highest(&$Model) { - $conditions = array(1 => 1); - $fields = array($Model->primaryKey, $this->settings['field']); - if ($this->settings['foreign_key']) { - $conditions[$Model->alias . '.' . $this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; - $fields[] = $this->settings['foreign_key']; - } + function _highest(&$Model) { + $conditions = array(1 => 1); + $fields = array($Model->primaryKey, $this->settings['field']); + if ($this->settings['foreign_key']) { + $conditions[$Model->alias . '.' . $this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; + $fields[] = $this->settings['foreign_key']; + } - return $Model->find('first', array( - 'conditions' => $conditions, - 'order' => $this->settings['field'] . ' DESC', - 'fields' => $fields - )); - } + return $Model->find('first', array( + 'conditions' => $conditions, + 'order' => $this->settings['field'] . ' DESC', + 'fields' => $fields + )); + } - function _previous(&$Model) { - $conditions = array($this->settings['field'] => $Model->data[$Model->alias][$this->settings['field']] - 1); - $fields = array($Model->primaryKey, $this->settings['field']); - if ($this->settings['foreign_key']) { - $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; - $fields[] = $this->settings['foreign_key']; - } - return $Model->find('first', array( - 'conditions' => $conditions, - 'order' => $this->settings['field'] . ' DESC', - 'fields' => $fields - )); - } + function _previous(&$Model) { + $conditions = array($this->settings['field'] => $Model->data[$Model->alias][$this->settings['field']] - 1); + $fields = array($Model->primaryKey, $this->settings['field']); + if ($this->settings['foreign_key']) { + $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; + $fields[] = $this->settings['foreign_key']; + } + return $Model->find('first', array( + 'conditions' => $conditions, + 'order' => $this->settings['field'] . ' DESC', + 'fields' => $fields + )); + } - function _next(&$Model) { - $conditions = array($this->settings['field'] => $Model->data[$Model->alias][$this->settings['field']] + 1); - $fields = array($Model->primaryKey, $this->settings['field']); - if ($this->settings['foreign_key']) { - $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; - $fields[] = $this->settings['foreign_key']; - } - return $Model->find('first', array( - 'conditions' => $conditions, - 'order' => $this->settings['field'] . ' DESC', - 'fields' => $fields - )); - } + function _next(&$Model) { + $conditions = array($this->settings['field'] => $Model->data[$Model->alias][$this->settings['field']] + 1); + $fields = array($Model->primaryKey, $this->settings['field']); + if ($this->settings['foreign_key']) { + $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; + $fields[] = $this->settings['foreign_key']; + } + return $Model->find('first', array( + 'conditions' => $conditions, + 'order' => $this->settings['field'] . ' DESC', + 'fields' => $fields + )); + } - function _all(&$Model) { - $conditions = array(1 => 1); - $fields = array($Model->primaryKey, $this->settings['field']); - if ($this->settings['foreign_key']) { - $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; - $fields[] = $this->settings['foreign_key']; - } - return $Model->find('all', array( - 'conditions' => $conditions, - 'order' => $this->settings['field'] . ' DESC', - 'fields' => $fields - )); - } + function _all(&$Model) { + $conditions = array(1 => 1); + $fields = array($Model->primaryKey, $this->settings['field']); + if ($this->settings['foreign_key']) { + $conditions[$this->settings['foreign_key']] = $Model->data[$Model->alias][$this->settings['foreign_key']]; + $fields[] = $this->settings['foreign_key']; + } + return $Model->find('all', array( + 'conditions' => $conditions, + 'order' => $this->settings['field'] . ' DESC', + 'fields' => $fields + )); + } - function _read(&$Model, $id) { - $Model->id = $id; - $Model->recursive = - 1; - $fields = array($Model->primaryKey, $this->settings['field']); - if ($this->settings['foreign_key']) { - $fields[] = $this->settings['foreign_key']; - } - $Model->data = $Model->find('first', array( - 'fields' => $fields, - 'conditions' => array($Model->primaryKey => $id) - )); - return $Model->data[$Model->alias][$this->settings['field']]; - } - } + function _read(&$Model, $id) { + $Model->id = $id; + $Model->recursive = - 1; + $fields = array($Model->primaryKey, $this->settings['field']); + if ($this->settings['foreign_key']) { + $fields[] = $this->settings['foreign_key']; + } + $Model->data = $Model->find('first', array( + 'fields' => $fields, + 'conditions' => array($Model->primaryKey => $id) + )); + return $Model->data[$Model->alias][$this->settings['field']]; + } +} - ?> \ No newline at end of file +?> \ No newline at end of file diff --git a/infinitas/management/models/route.php b/infinitas/management/models/route.php index 8d8b11f3c..5e24c2208 100644 --- a/infinitas/management/models/route.php +++ b/infinitas/management/models/route.php @@ -28,6 +28,14 @@ class Route extends ManagementAppModel { 'Libs' ); + var $actsAs = array( + 'Libs.Ordered' + ); + + var $order = array( + 'Route.ordering' => 'ASC' + ); + function getPlugins(){ $plugins = Configure::listObjects('plugin'); @@ -39,5 +47,145 @@ function getPlugins(){ return array(0 => 'None') + (array)$return; } + + function getRoutes(){ + $routes = Cache::read('routes'); + if ($routes !== false) { + //return $routes; + } + $routes = $this->find( + 'all', + array( + 'fields' => array( + 'Route.url', + 'Route.prefix', + 'Route.plugin', + 'Route.controller', + 'Route.action', + 'Route.values', + 'Route.rules', + 'Route.force_backend', + 'Route.force_frontend' + ), + 'conditions' => array( + 'Route.active' => 0 + ), + 'order' => array( + 'Route.ordering' => 'ASC' + ), + 'contain' => false + ) + ); + + + foreach( $routes as $array ){ + $vaules = $regex = array(); + + $routingRules[]['Route'] = array( + 'url' => $array['Route']['url'], + 'values' => $this->_getValues($array['Route']), + 'regex' => $this->_getRegex($array['Route']['rules']) + ); + } + + Cache::write('routes', $routingRules, 'core'); + + return $routingRules; + } + + function _getValues($route = array()){ + if (!$route) { + return false; + } + + if (!empty($route['prefix'])) { + $values['prefix'] = $route['prefix']; + } + + if (!empty($route['plugin'])) { + $values['plugin'] = $route['plugin']; + } + + if (!empty($route['controller'])) { + $values['controller'] = $route['controller']; + } + + if (!empty($route['action'])) { + $values['action'] = $route['action']; + } + + if (!empty($route['values'])) { + $_v = explode("\n", $route['values']); + + foreach((array)$_v as $line ){ + $parts = explode( ':', $line ); + if (count($parts) == 2) { + $_values[$parts[0]] = $this->_getType($parts[1]); + } + } + + $values = array_merge((array)$values, (array)$_values); + } + + if ($route['force_backend']) { + $values['admin'] = true; + } + else if ($route['force_frontend']) { + $values['admin'] = null; + } + + return $values; + } + + function _getRegex($field){ + $values = array(); + if (!empty($field)) { + $_v = explode("\n", $field); + + foreach((array)$_v as $line ){ + $parts = explode( ':', $line ); + if (count($parts) == 2 ) { + $values[$parts[0]] = $this->_getType($parts[1]); + } + } + } + return $values; + } + + function _getType($field = false){ + switch($field){ + case 'null': + case strstr($field, 'null'): // var_dump has "null " and trim/rtrim/str_replace("\n".. || "\n\r"..) wont remove the space + return null; + break; + + default: + return $field; + break; + + } // switch + } + + function afterSave($created) { + parent::afterSave($created); + + $this->__clearCache(); + return true; + } + + function afterDelete() { + parent::afterDelete(); + + $this->__clearCache(); + return true; + } + + function __clearCache() { + if (is_file(CACHE . 'core' . DS . 'routes')) { + unlink(CACHE . 'core' . DS . 'routes'); + } + + return true; + } } ?> \ No newline at end of file diff --git a/infinitas/management/views/routes/admin_add.ctp b/infinitas/management/views/routes/admin_add.ctp index b9b2de061..4c129e6aa 100644 --- a/infinitas/management/views/routes/admin_add.ctp +++ b/infinitas/management/views/routes/admin_add.ctp @@ -18,12 +18,18 @@ */ echo $this->Core->adminOtherHead( $this ); - echo $this->Form->create( 'Config' ); + echo $this->Form->create( 'Route' ); echo $this->Form->input( 'name' ); - echo $this->Form->input( 'url', array('value' => '/') ); + echo $this->Form->input( 'url' ); + echo $this->Form->input( 'prefix' ); echo $this->Form->input( 'plugin' ); echo $this->Form->input( 'controller' ); echo $this->Form->input( 'action' ); - echo $this->Form->input( 'match_all' ); + echo $this->Form->input( 'values' ); + echo $this->Form->input( 'rules' ); + echo $this->Form->input( 'force_backend' ); + echo $this->Form->input( 'force_frontend' ); + echo $this->Form->input( 'active' ); + echo $this->Form->hidden( 'order_id', array('value'=>1) ); echo $this->Form->end( 'Save Configuration' ); ?> \ No newline at end of file diff --git a/infinitas/management/views/routes/admin_edit.ctp b/infinitas/management/views/routes/admin_edit.ctp index 0c583c667..b2d62e062 100644 --- a/infinitas/management/views/routes/admin_edit.ctp +++ b/infinitas/management/views/routes/admin_edit.ctp @@ -18,51 +18,19 @@ */ echo $this->Core->adminOtherHead( $this ); - echo $this->Form->create( 'Config', array( 'action' => 'edit' ) ); + echo $this->Form->create( 'Route' ); echo $this->Form->input( 'id' ); - echo $this->Form->input( 'key', array( 'readonly' => true ) ); - - echo $this->Form->hidden( 'type' ); - - switch( $this->data['Config']['type'] ) - { - case 'bool': - $_label = explode( '.', $this->data['Config']['key'] ); - $label = ( isset( $_label[1] ) ? $_label[1] : $_label[0] ); - $this->data['Config']['value'] = ( $this->data['Config']['value'] == 'true' ) ? '1' : '0'; - echo $this->Form->input( - 'value', - array( - 'type' => 'checkbox', - 'label' => Inflector::humanize( $label ) - ) - ); - break; - - case 'dropdown': - $_options = explode( ',', $this->data['Config']['options'] ); - foreach( $_options as $o ) - { - $options[$o] = $o; - } - echo $this->Form->input( - 'value', - array( - 'type' => 'select', - 'options' => $options, - 'selected' => $this->data['Config']['value'] - ) - ); - break; - - case 'integer': - case 'string': - echo $this->Form->input( 'value' ); - break; - } // switch - - //echo $this->Form->input( 'options' ); - echo $this->Form->input( 'core', array( 'disabled' => true ) ); - echo $this->Core->wysiwyg( 'Config.description' ); + echo $this->Form->input( 'name' ); + echo $this->Form->input( 'url' ); + echo $this->Form->input( 'prefix' ); + echo $this->Form->input( 'plugin' ); + echo $this->Form->input( 'controller' ); + echo $this->Form->input( 'action' ); + echo $this->Form->input( 'values' ); + echo $this->Form->input( 'rules' ); + echo $this->Form->input( 'force_backend' ); + echo $this->Form->input( 'force_frontend' ); + echo $this->Form->input( 'active' ); + echo $this->Form->hidden( 'order_id', array('value'=>1) ); echo $this->Form->end( 'Save Configuration' ); -?> \ No newline at end of file + ?> \ No newline at end of file diff --git a/infinitas/management/views/routes/admin_index.ctp b/infinitas/management/views/routes/admin_index.ctp index 522ea0276..1f5613ef8 100644 --- a/infinitas/management/views/routes/admin_index.ctp +++ b/infinitas/management/views/routes/admin_index.ctp @@ -24,7 +24,9 @@ array( 'add', 'edit', - 'copy' + 'copy', + 'toggle', + 'delete' ) ); echo $this->Core->adminIndexHead( $this, $paginator, null, $massActions ); @@ -43,8 +45,14 @@ $this->Paginator->sort( 'name' ), __( 'Url', true ), __( 'Route', true ), + __( 'Order', true ) => array( + 'style' => 'width:50px;' + ), __( 'Core', true ) => array( 'style' => 'width:50px;' + ), + __( 'Active', true ) => array( + 'style' => 'width:50px;' ) ) ); @@ -63,25 +71,28 @@   + + Core->ordering( $route['Route']['id'], $route['Route']['ordering'] ); ?>  + Status->status( $route['Route']['core'] ); ?>  + + Status->status( $route['Route']['active'] ); ?>  + - Form->end(); - - ?> + Form->end(); ?> element( 'pagination/navigation' ); ?> \ No newline at end of file