Example #1
0
 /**
  * If a search action has been defined and a search only returns one result
  * the user will be automatically redirected to the search action.
  *
  * @param DataGrid $grid data grid
  *
  * @return bool redirect active?
  */
 protected function redirectToSearchAction($grid)
 {
     $node = $this->getNode();
     $search = $grid->getPostvar('atksearch');
     // check if we are searching and a search action has been defined
     if (!is_array($search) || count($search) == 0 || !is_array($node->m_search_action) || count($node->m_search_action) == 0) {
         return false;
     }
     // check if there is only a single record in the result
     $grid->loadRecords();
     if ($grid->getCount() != 1) {
         return false;
     }
     $records = $grid->getRecords();
     foreach ($node->m_search_action as $action) {
         if (!$node->allowed($action, $records[0])) {
             continue;
         }
         // reset search so we can back to the normal admin screen if we want
         $grid->setPostvar('atksearch', array());
         $sm = SessionManager::getInstance();
         $url = $sm->sessionUrl(Tools::dispatch_url($node->atkNodeUri(), $action, array('atkselector' => $node->primaryKey($records[0]))), SessionManager::SESSION_NESTED);
         if ($grid->isUpdate()) {
             $script = 'document.location.href = ' . Json::encode($url) . ';';
             $node->getPage()->register_loadscript($script);
         } else {
             $node->redirect($url);
         }
         return true;
     }
     return false;
 }
Example #2
0
 /**
  * If the auto-select flag is set and only one record exists we immediately
  * return with the selected record.
  *
  * @param DataGrid $grid data grid
  *
  * @return bool auto-select active?
  */
 protected function autoSelectRecord($grid)
 {
     $node = $this->getNode();
     if (!$node->hasFlag(Node::NF_AUTOSELECT)) {
         return false;
     }
     $grid->loadRecords();
     if ($grid->getCount() != 1) {
         return false;
     }
     $sm = SessionManager::getInstance();
     if ($sm->atkLevel() > 0 && $grid->getPostvar('atkprevlevel', 0) > $sm->atkLevel()) {
         $backUrl = $sm->sessionUrl(Config::getGlobal('dispatcher') . '?atklevel=' . $sm->newLevel(SessionManager::SESSION_BACK));
         $node->redirect($backUrl);
     } else {
         $records = $grid->getRecords();
         // There's only one record and the autoselect flag is set, so we
         // automatically go to the target.
         $parser = new StringParser(rawurldecode(Tools::atkurldecode($grid->getPostvar('atktarget'))));
         // For backwardscompatibility reasons, we also support the '[pk]' var.
         $records[0]['pk'] = $node->primaryKey($records[0]);
         $target = $parser->parse($records[0], true);
         $node->redirect($sm->sessionUrl($target, SessionManager::SESSION_NESTED));
     }
     return true;
 }