/** * Select handler, returns the records for the grid. * * @param DataGrid $grid * * @return array Records for the grid */ public function selectHandlerForAdd(DataGrid $grid) { $records = $this->getRecordsFromSession(); $limit = $grid->getLimit(); $offset = $grid->getOffset(); $records_count = count($records); // If we don't need to limit the result, then we don't if ((int) $offset === 0 && $limit >= $records_count) { // We have to sort the data first, because the datagrid // is very sensitive with regards to it's numerical keys // being sequential sort($records); return $records; } // Limit the search results and return the limited results $ret = []; $records_keys = array_keys($records); for ($i = $offset, $j = 0; $i < $records_count && $j < $limit; $i++, $j++) { $ret[] = $records[$records_keys[$i]]; } return $ret; }
/** * 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; }
/** * 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; }
/** * Updates the datagrid for the edit and display actions. * * @return string grid html */ public function partial_grid() { $this->createDestination(); $node = $this->getDestination(); try { $grid = DataGrid::resume($node); $this->modifyDataGrid($grid, DataGrid::RESUME); } catch (Exception $e) { $grid = DataGrid::create($node); $this->modifyDataGrid($grid, DataGrid::CREATE); } return $grid->render(); }
/** * This method returns an html page containing a recordlist to select * records from. The recordlist can be searched, sorted etc. like an * admin screen. * * @return string The html select page. */ public function multiSelectPage() { // add the postvars to the form global $g_stickyurl; $sm = SessionManager::getInstance(); $g_stickyurl[] = 'atktarget'; $g_stickyurl[] = 'atktargetvar'; $g_stickyurl[] = 'atktargetvartpl'; $GLOBALS['atktarget'] = $this->getNode()->m_postvars['atktarget']; $GLOBALS['atktargetvar'] = $this->getNode()->m_postvars['atktargetvar']; $GLOBALS['atktargetvartpl'] = $this->getNode()->m_postvars['atktargetvartpl']; $params['header'] = Tools::atktext('title_multiselect', $this->getNode()->m_module, $this->getNode()->m_type); $actions['actions'] = []; $actions['mra'][] = 'multiselect'; $grid = DataGrid::create($this->getNode(), 'multiselect'); /* * At first the changes below looked like the solution for the error * on the contact multiselect page. Except this is not the case, because * the MRA actions will not be shown, which is a must. */ if (is_array($actions['actions'])) { $grid->setDefaultActions($actions['actions']); } else { $grid->setDefaultActions($actions); } $grid->removeFlag(DataGrid::EXTENDED_SEARCH); $grid->addFlag(DataGrid::MULTI_RECORD_ACTIONS); $params['list'] = $grid->render(); if ($sm->atkLevel() > 0) { $backlinkurl = $sm->sessionUrl(Config::getGlobal('dispatcher') . '?atklevel=' . $sm->newLevel(SessionManager::SESSION_BACK)); $params['footer'] = '<br><div style="text-align: center"><input type="button" class="btn btn-default" onclick="window.location=\'' . $backlinkurl . '\';" value="' . Tools::atktext('cancel') . '"></div>'; } $output = $this->getUi()->renderList('multiselect', $params); return $this->getUi()->renderBox(array('title' => $this->getNode()->actionTitle('multiselect'), 'content' => $output)); }
/** * Call grid modifiers for the given grid. * * @param DataGrid $grid grid * @param int $mode creation mode */ private static function callModifiers(DataGrid $grid, $mode) { $keys = array('*', $grid->getNode()->atkNodeUri()); foreach ($keys as $key) { if (!isset(self::$s_modifiers[$key])) { continue; } foreach (self::$s_modifiers[$key] as $callback) { call_user_func($callback, $grid, $mode); } } }
/** * Sets the sortorder options for this attribute which is used in recordlists * and search actions. * * @param ColumnConfig $columnConfig The config that contains options for * extended sorting and grouping to a * recordlist. * @param string $fieldprefix The prefix of the attribute on HTML forms * @param DataGrid $grid The grid that this attribute lives on. * * @return string HTML */ public function sortOrder($columnConfig, $fieldprefix = '', $grid = null) { $fieldname = $this->fieldName(); $currentOrder = $columnConfig->getOrder($fieldname); $res = ''; if ($currentOrder > 0) { $direction = $columnConfig->getSortDirection($this->fieldName()) == 'desc' ? 'asc' : 'desc'; if ($grid == null) { $res = Tools::href(Config::getGlobal('dispatcher') . '?' . $columnConfig->getUrlCommand($fieldname, $direction), Tools::atktext('column_' . $direction)) . ' '; } else { $call = $grid->getUpdateCall($columnConfig->getUrlCommandParams($fieldname, $direction)); $res = '<a href="javascript:void(0)" onclick="' . htmlentities($call) . '">' . $this->text('column_' . $direction) . '</a>'; } } $res .= '<select class="form-control select-standard" name="atkcolcmd[][setorder][' . $fieldprefix . $fieldname . ']">'; $res .= '<option value="">'; for ($i = 1; $i < 6; ++$i) { $selected = $currentOrder == $i ? 'selected' : ''; $res .= '<option value="' . $i . '" ' . $selected . '>' . $i; } $res .= '</select>'; return $res; }