From d4509edf7d65f81767f6f49947e8becbbe17e16a Mon Sep 17 00:00:00 2001 From: SecretR Date: Wed, 27 Feb 2013 19:36:53 +0200 Subject: [PATCH] Admin UI - batch navigation link creation improvements, testing required --- e107_admin/cpage.php | 3 +- e107_admin/newspost.php | 8 ++- e107_handlers/admin_ui.php | 91 +++++++++++++++++++++--- e107_handlers/form_handler.php | 18 ++--- e107_handlers/model_class.php | 126 ++++++++++++++++----------------- 5 files changed, 164 insertions(+), 82 deletions(-) diff --git a/e107_admin/cpage.php b/e107_admin/cpage.php index ae690b8ff..4c06bdf3c 100644 --- a/e107_admin/cpage.php +++ b/e107_admin/cpage.php @@ -244,7 +244,8 @@ class page_admin_ui extends e_admin_ui protected $batchLink = true; protected $sortField = 'page_order'; protected $orderStep = 10; - protected $url = array('profile'=>'page/view', 'name' => 'page_title', 'description' => '', 'link'=>'{e_BASE}page.php?id=[id]'); // 'link' only needed if profile not provided. + //protected $url = array('profile'=>'page/view', 'name' => 'page_title', 'description' => '', 'link'=>'{e_BASE}page.php?id=[id]'); // 'link' only needed if profile not provided. + protected $url = array('route'=>'page/view/index', 'vars' => array('id' => 'page_id', 'sef' => 'page_sef'), 'name' => 'page_title', 'description' => ''); // 'link' only needed if profile not provided. protected $tabs = array("Main","Advanced"); // protected $listSorting = true; diff --git a/e107_admin/newspost.php b/e107_admin/newspost.php index 86ada2829..91fb27505 100644 --- a/e107_admin/newspost.php +++ b/e107_admin/newspost.php @@ -353,7 +353,13 @@ class news_admin_ui extends e_admin_ui protected $batchCopy = true; protected $batchLink = true; protected $listOrder = "news_id desc"; - protected $url = array('profile'=>'news/view/item', 'name' => 'news_title', 'description' => 'news_summary', 'link'=>'{e_BASE}news.php?extend.[id]'); // 'link' only needed if profile not provided. + // true for 'vars' value means use same var + protected $url = array( + 'route'=>'news/view/item', + 'name' => 'news_title', + 'description' => 'news_summary', + 'vars'=> array('news_id' => true, 'news_sef' => true, 'category_id' => 'news_category', 'category_sef' => true) // FIXME category_sef missing, we have to retrieve category data on the list view + ); // 'link' only needed if profile not provided. protected $listQry = "SELECT n.*,u.user_id,u.user_name FROM #news AS n LEFT JOIN #user AS u ON n.news_author = u.user_id "; // without any Order or Limit. diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index 6941e94a8..ff418fc2b 100644 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -3933,25 +3933,100 @@ class e_admin_ui extends e_admin_controller_ui protected function handleListCopyBatch($selected) { // Batch Copy - $set_messages = true; $this->getTreeModel()->copy($selected); - if($set_messages) $this->getTreeModel()->setMessages(); + // move messages to default stack + $this->getTreeModel()->setMessages(); + // send messages to session + e107::getMessage()->moveToSession(); + // redirect $this->redirect(); } - /** TODO - * Batch copy trigger + /** + * Batch URL trigger * @param array $selected * @return void */ protected function handleListUrlBatch($selected) { // Batch Copy - $set_messages = true; - $this->getTreeModel()->url($selected); - if($set_messages) $this->getTreeModel()->setMessages(); - $this->redirect(); + if($this->_add2nav($selected)) + { + //e107::getMessage()->moveToSession(); + //$this->redirect(); + } } + + + protected function _add2nav($selected) + { + if(empty($selected)) return false;// TODO warning message + + if(!is_array($selected)) $selected = array($selected); + + $sql = e107::getDb(); + $urlData = $this->getUrl(); + $allData = $this->getTreeModel()->url($selected, array('sc' => true), true); + + e107::getMessage()->addDebug('Using Url Route:'.$urlData['route']); + + $scount = 0; + foreach($allData as $id => $data) + { + $name = $data['name']; + $desc = $data['description']; + + $link = $data['url']; + + $link = str_replace('{e_BASE}', "", $link); // TODO temporary here, discuss + + // _FIELD_TYPES auto created inside mysql handler now + $linkArray = array( + 'link_name' => $name, + 'link_url' => $link, + 'link_description' => e107::getParser()->toDB($desc), // retrieved field type is string, we might need todb here + 'link_button' => '', + 'link_category' => 255, // Using an unassigned template rather than inactive link-class, since other inactive links may already exist. + 'link_order' => 0, + 'link_parent' => 0, + 'link_open' => '', + 'link_class' => 0, + 'link_sefurl' => e107::getParser()->toDB($urlData['route'].'?'.$id), + ); + + $res = $sql->insert('links', $linkArray); + + // FIXME lans + if($res !== FALSE) + { + e107::getMessage()->addSuccess('Created Sitelink: '.($name ? $name : 'n/a').""); + $scount++; + } + else + { + if($sql->getLastErrorNumber()) + { + e107::getMessage()->addError('SQL Link Creation Error'); //TODO - Lan + e107::getMessage()->addDebug('SQL Link Creation Error #'.$sql->getLastErrorNumber().': '.$sql->getLastErrorText()); + } + else + { + e107::getMessage()->addError('Unknown error: '.$name." not added"); + } + } + + } + + if($scount > 0) + { + e107::getMessage()->addSuccess("
{$scount} new sitelinks were added but are currently unassigned. You should now modify these links to your liking.

Modify Links"); + return $scount; + } + + return false; + + } + /** * Batch boolean trigger * @param array $selected diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php index 84d556775..93f6f441d 100644 --- a/e107_handlers/form_handler.php +++ b/e107_handlers/form_handler.php @@ -2188,7 +2188,7 @@ class e_form { $value = $tp->htmlwrap($value, (int)$parms['wrap'], varset($parms['wrapChar'], ' ')); } - if(vartrue($parms['link']) && $id && is_numeric($id)) + if(vartrue($parms['link']) && $id/* && is_numeric($id)*/) { $link = str_replace('[id]',$id,$parms['link']); $link = $tp->replaceConstants($link); // SEF URL is not important since we're in admin. @@ -2197,19 +2197,21 @@ class e_form $ext = vartrue($parms['target']) =='blank' ? " rel='external' " : ""; // new window $modal = vartrue($parms['target']) =='modal' ? " data-toggle='modal' data-cache='false' data-target='#uiModal' " : ""; - $data = $this->getController()->getListModel()->toArray(); - if($parms['link'] == 'sef') //XXX @Miro - Thoughts? - { - $urlData = $this->getController()->getUrl(); - $link = e107::getUrl()->create($urlData['profile'], $data); + if($parms['link'] == 'sef' && $this->getController()->getListModel()) + { + $model = $this->getController()->getListModel(); + // copy url config + $model->setUrl($this->getController()->getUrl()); + // assemble the url + $link = $model->url(); } elseif(vartrue($data[$parms['link']])) // support for a field-name as the link. eg. link_url. { $link = $tp->replaceConstants(vartrue($data[$parms['link']])); } - - $value = "".$value.""; + // in case something goes wrong... + if($link) $value = "".$value.""; } if(!vartrue($attributes['noedit']) && vartrue($parms['editable']) && !vartrue($parms['link'])) // avoid bad markup, better solution coming up diff --git a/e107_handlers/model_class.php b/e107_handlers/model_class.php index 320cb4802..7edfb4ec2 100644 --- a/e107_handlers/model_class.php +++ b/e107_handlers/model_class.php @@ -523,7 +523,8 @@ class e_model extends e_object protected $_db_table; /** - * Current url Profile + * Current url Profile data + * Example: array('route'=>'page/view/index', 'vars' => array('id' => 'page_id', 'sef' => 'page_sef'), 'name' => 'page_title', 'description' => ''); * @var string */ protected $_url; @@ -599,7 +600,6 @@ class e_model extends e_object return $this; } - /** * Get url profile * @return array @@ -609,6 +609,52 @@ class e_model extends e_object return $this->_url; } + /** + * Generic URL assembling method + * @param array $options [optional] see eRouter::assemble() for $options structure + * @param boolean $extended [optional] if true, method will return an array containing url, title and description of the url + * @return mixed URL string or extended array data + */ + public function url($options = array(), $extended = false) + { + $urldata = $this->getUrl(); + if(empty($urldata) || !vartrue($urldata['route'])) return ($extended ? array() : null); + + $eurl = e107::getUrl(); + + if(empty($options)) $options = array(); + elseif(!is_array($options)) parse_str($options, $options); + + $vars = $this->toArray(); + if(!vartrue($options['allow'])) + { + if(vartrue($urldata['vars']) && is_array($urldata['vars'])) + { + $vars = array(); + foreach ($urldata['vars'] as $var => $field) + { + if($field === true) $field = $var; + $vars[$var] = $this->get($field); + } + } + } + + $method = isset($options['sc']) ? 'sc' : 'create'; + + $url = e107::getUrl()->$method($urldata['route'], $vars, $options); + + if(!$extended) + { + return $url; + } + + return array( + 'url' => $url, + 'name' => vartrue($urldata['name']) ? $this->get($urldata['name']) : '', + 'description' => vartrue($urldata['description']) ? $this->get($urldata['description']) : '', + ); + } + /** * Get data fields array * @return array @@ -3446,69 +3492,21 @@ class e_admin_tree_model extends e_front_tree_model } - /** - * Batch Url Creation Table Rows. - */ - public function url($ids) + /** + * Get urls/url data for given nodes + */ + public function url($ids, $options = array(), $extended = false) { - $tp = e107::getParser(); - $ids = array_map(array($tp, 'toDB'), $ids); - $idstr = implode(', ', $ids); - $sql = e107::getDb(); - $allData = $this->toArray(); - $urlData = $this->getUrl(); - - $this->addMessageDebug('Using Url Profile:'.$urlData['profile']); - - foreach($ids as $id) - { - $data = vartrue($allData[$id]); - $name = $urlData['name']; - $desc = $urlData['description']; - - $link = vartrue($urlData['profile']) ? e107::getUrl()->create($urlData['profile'], $data) : str_replace("[id]", $id, $urlData['link']); - - $link = str_replace(e_HTTP,"",$link); // work-around fix. - - $linkArray = array( - 'link_name' => $tp->toDB($data[$name]), - 'link_url' => $link, - 'link_description' => $tp->toDB($desc), - 'link_button' => '', - 'link_category' => 255, // Using an unassigned template rather than inactive link-class, since other inactive links may already exist. - 'link_order' => 0, - 'link_parent' => 0, - 'link_open' => '', - 'link_class' => 0 - ); - - - $res = $sql->insert('links',$linkArray); - - if($res !== FALSE) - { - $this->addMessageSuccess('Created Sitelink: '.$data[$name].""); - } - else - { - if($sql->getLastErrorNumber()) - { - $this->addMessageError('SQL Link Creation Error', $session_messages); //TODO - Lan - $this->addMessageDebug('SQL Link Creation Error #'.$sql->getLastErrorNumber().': '.$sql->getLastErrorText()); - } - $this->_db_errno = $sql->getLastErrorNumber(); - $this->_db_errmsg = $sql->getLastErrorText(); - } - - } - - if($res !== FALSE) - { - $this->addMessageSuccess("
New sitelinks are currently unassigned. You should now modify these links to your liking.

Modify Links"); - } - - return true; - + $ret = array(); + foreach ($ids as $id) + { + if(!$this->hasNode($id)) continue; + + $model = $this->getNode($id); + if($this->getUrl()) $model->setUrl($this->getUrl()); // copy url config data if available + $ret[$id] = $model->url($options, $extended); + } + return $ret; } } \ No newline at end of file