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;
 }