public function onBeforeBrowse() { // Buttons \JToolBarHelper::addNew(); \JToolBarHelper::editList(); \JToolBarHelper::divider(); \JToolBarHelper::publishList(); \JToolBarHelper::unpublishList(); \JToolBarHelper::divider(); $msg = Text::_($this->componentName . '_CONFIRM_DELETE'); \JToolBarHelper::deleteList(strtoupper($msg)); return parent::onBeforeBrowse(); // TODO: Change the autogenerated stub }
/** * Send the mail * * @return mixed True if successful * * @throws \RuntimeException */ public function Send() { $config = $this->container->appConfig; if ($config->get('mail.online', true)) { if ($this->Mailer == 'mail' && !function_exists('mail')) { throw new \RuntimeException(sprintf('%s::Send mail not enabled.', get_class($this))); } @($result = parent::Send()); if ($result == false) { throw new \RuntimeException(sprintf('%s::Send failed: "%s".', get_class($this), $this->ErrorInfo)); } return $result; } else { $this->container->application->enqueueMessage(Text::_('AWF_MAIL_FUNCTION_OFFLINE')); return false; } }
/** * Overrides the default method to execute and display a template script. * Instead of loadTemplate is uses loadAnyTemplate. * * @param string $tpl The name of the template file to parse * * @return boolean True on success * * @throws \Exception When the layout file is not found */ public function display($tpl = null) { $method = 'onBefore' . ucfirst($this->doTask); if (method_exists($this, $method)) { $result = $this->{$method}($tpl); if (!$result) { throw new \Exception(Text::_('AWF_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } } $method = 'onAfter' . ucfirst($this->doTask); if (method_exists($this, $method)) { $result = $this->{$method}($tpl); if (!$result) { throw new \Exception(Text::_('AWF_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } } return true; }
/** * Method to sort a column in a grid * * @param string $title The link title * @param string $order The order field for the column * @param string $direction The current direction * @param string $selected The selected ordering * @param string $task An optional task override * @param string $new_direction An optional direction for the new column * @param string $tip An optional text shown as tooltip title instead of $title * @param string $orderingJs (optional) The Javascript function which handles table reordering, e.g. "Foobar.System.tableOrdering" * * @return string */ public static function sort($title, $order, $direction = 'asc', $selected = '', $task = null, $new_direction = 'asc', $tip = '', $orderingJs = '') { $direction = strtolower($direction); $icon = array('caret-up', 'caret-down'); $index = (int) ($direction == 'desc'); if ($order != $selected) { $direction = $new_direction; } else { $direction = $direction == 'desc' ? 'asc' : 'desc'; } if (empty($orderingJs)) { $orderingJs = self::$javascriptPrefix . 'tableOrdering'; } $html = '<a href="#" onclick="' . $orderingJs . '(\'' . $order . '\',\'' . $direction . '\',\'' . $task . '\');return false;"' . ' class="hasTooltip" title="' . Text::_($tip ? $tip : $title) . '">'; $html .= Text::_($title); if ($order == $selected) { $html .= ' <span class="fa fa-' . $icon[$index] . '"></span>'; } $html .= '</a>'; return $html; }
/** * Overrides the default method to execute and display a template script. * Instead of loadTemplate is uses loadAnyTemplate. * * @param string $tpl The name of the template file to parse * * @return boolean True on success * * @throws \Exception When the layout file is not found */ public function display($tpl = null) { $method = 'onBefore' . ucfirst($this->doTask); if (method_exists($this, $method)) { $result = $this->{$method}($tpl); if (!$result) { throw new \Exception(Text::_('AWF_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } } $result = $this->loadTemplate($tpl); $method = 'onAfter' . ucfirst($this->doTask); if (method_exists($this, $method)) { $result = $this->{$method}($tpl); if (!$result) { throw new \Exception(Text::_('AWF_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } } if (is_object($result) && $result instanceof \Exception) { throw $result; } else { echo $result; return true; } }
/** * Try to log in a user given the username, password and any additional parameters which may be required by the * user class * * @param string $username The username of the user to log in * @param string $password The (unhashed) password of the user to log in * @param array $params [optional] Any additional parameters you may want to pass to the user object, e.g. 2FA * * @return boolean True on success * * @throws \Exception When the login fails */ public function loginUser($username, $password, $params = array()) { $user = $this->getUserByUsername($username); if (is_null($user)) { throw new \RuntimeException(Text::_('AWF_USER_ERROR_AUTHERROR'), 403); } if (!$user->verifyPassword($password, $params)) { throw new \RuntimeException(Text::_('AWF_USER_ERROR_AUTHERROR'), 403); } $this->container->segment->set('user_id', $user->getId()); $this->currentUser = $user; }
/** * Tries to figure out the page title to use in the Joomla! back-end * * @return string */ protected function getJoomlaPageTitle() { $appName = strtoupper($this->getContainer()->application_name); if (substr($appName, -5) == 'ADMIN') { $appName = substr($appName, 0, -5); } // e.g. FOOBAR_APP_TITLE $appTitle = $appName . '_APP_TITLE'; // e.g. COM_FOOBAR $componentTitle = 'COM_' . $appName; // e.g. COM_FOOBAR_ITEMS_TITLE $viewTitle = $componentTitle . '_' . strtoupper($this->name) . '_TITLE'; // e.g. COM_FOOBAR_ITEMS_BROWSE $taskTitle = substr($viewTitle, 0, -5) . strtoupper($this->doTask); // First try the task-specific title $title = Text::_($taskTitle); if ($title != $taskTitle) { return $title; } // Then try the view-specific title $title = Text::_($viewTitle); if ($title != $viewTitle) { return $title; } // Then try the component-specific title $title = Text::_($componentTitle); if ($title != $componentTitle) { return $title; } // Then try the app-specific title $title = Text::_($appTitle); if ($title != $appTitle) { return $title; } // If all else fails, try using the generic title... $title = Text::_($appName); return $title; }
/** * Method to get the model name * * The model name. By default parsed using the classname or it can be set * by passing a $config['name'] in the class constructor * * @return string The name of the model * * @throws \RuntimeException If it's impossible to get the name */ public function getName() { if (empty($this->name)) { $r = null; if (!preg_match('/(.*)\\\\Model\\\\(.*)/i', get_class($this), $r)) { throw new \RuntimeException(\Awf\Text\Text::_('AWF_APPLICATION_ERROR_MODEL_GET_NAME'), 500); } $this->name = strtolower($r[2]); } return $this->name; }
/** * Method to get the controller name * * The controller name is set by default parsed using the classname, or it can be set * by passing a $config['name'] in the class constructor * * @return string The name of the controller * * @throws \Exception If it's impossible to determine the name and it's not set */ public function getName() { if (empty($this->name)) { $r = null; if (!preg_match('/(.*)\\\\Controller\\\\(.*)/i', get_class($this), $r)) { throw new \Exception(Text::_('AWF_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500); } $this->name = strtolower($r[2]); } return $this->name; }
/** * The main code of the Dispatcher. It spawns the necessary controller and * runs it. * * @return void * * @throws \Exception */ public function dispatch() { try { $result = $this->onBeforeDispatch(); $error = ''; } catch (\Exception $e) { $result = false; $error = $e->getMessage(); } if (!$result) { // For json, don't use normal 403 page, but a json encoded message if ($this->input->get('format', '') == 'json') { echo json_encode(array('code' => '403', 'error' => $error)); $this->container->application->close(); } throw new \Exception(Text::_('AWF_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } // Get and execute the controller $view = $this->input->getCmd('view', $this->defaultView); $task = $this->input->getCmd('task', 'default'); if (empty($task)) { $task = 'default'; $this->input->set('task', $task); } $controller = Mvc\Controller::getInstance($this->container->application_name, $view, $this->container); $status = $controller->execute($task); if ($status === false) { throw new \Exception(Text::_('AWF_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } if (!$this->onAfterDispatch()) { throw new \Exception(Text::_('AWF_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } $controller->redirect(); }
/** * Check the data for validity. By default it only checks for fields declared as NOT NULL * * @return static Self, for chaining * * @throws \RuntimeException When the data bound to this record is invalid */ public function check() { if (!$this->autoChecks) { return $this; } foreach ($this->knownFields as $fieldName => $field) { // Never check the key if it's empty; an empty key is normal for new records if ($fieldName == $this->idFieldName) { continue; } $value = $this->{$fieldName}; if ($field->Null == 'NO' && empty($value) && !is_numeric($value) && !in_array($fieldName, $this->fieldsSkipChecks)) { $text = $this->container->application->getName() . '_' . Inflector::singularize($this->getName()) . '_ERR_' . $fieldName . '_EMPTY'; throw new \RuntimeException(Text::_($text), 500); } } return $this; }
/** * Converts a double colon separated string or 2 separate strings to a string ready for bootstrap tooltips * * @param string $title The title of the tooltip (or combined '::' separated string). * @param string $content The content to tooltip. * @param int $translate If true will pass texts through Text. * @param int $escape If true will pass texts through htmlspecialchars. * * @return string The tooltip string */ public static function tooltipText($title = '', $content = '', $translate = 1, $escape = 1) { // Return empty in no title or content is given. if ($title == '' && $content == '') { return ''; } // Split title into title and content if the title contains '::' (migrated Joomla! code, using the obsolete Mootools format). if ($content == '' && !(strpos($title, '::') === false)) { list($title, $content) = explode('::', $title, 2); } // Pass strings through Text. if ($translate) { $title = Text::_($title); $content = Text::_($content); } // Escape the strings. if ($escape) { $title = str_replace('"', '"', $title); $content = str_replace('"', '"', $content); } // Return only the content if no title is given. if ($title == '') { return $content; } // Return only the title if title and text are the same. if ($title == $content) { return '<strong>' . $title . '</strong>'; } // Return the formatted sting combining the title and content. if ($content != '') { return '<strong>' . $title . '</strong><br />' . $content; } // Return only the title. return $title; }
/** * Delete selected item(s) * * @return void */ public function remove() { // CSRF prevention $this->csrfProtection(); $model = $this->getModel(); $ids = $this->getIDsFromRequest($model, false); try { $status = true; foreach ($ids as $id) { $model->find($id); $model->delete(); } } catch (\Exception $e) { $status = false; $error = $e->getMessage(); } // Redirect if ($customURL = $this->input->getBase64('returnurl', '')) { $customURL = base64_decode($customURL); } $router = $this->container->router; $url = !empty($customURL) ? $customURL : $router->route('index.php?view=' . Inflector::pluralize($this->view)); if (!$status) { $this->setRedirect($url, $error, 'error'); } else { $textKey = $this->container->application_name . '_LBL_' . Inflector::singularize($this->view) . '_DELETED'; $this->setRedirect($url, Text::_($textKey)); } }
/** * Runs a restoration step and returns an array to be used in the response. * * @return array * * @throws \Exception */ public function stepRestoration() { $parts = $this->getParam('parts', 1); $this->openFile(); $this->lineNumber = $this->start; $this->totalSizeRead = 0; $this->queries = 0; while ($this->timer->getTimeLeft() > 0) { $query = ''; // Get the next query line try { $query = $this->readNextLine(); } catch (\Exception $exc) { if ($exc->getCode() == 200) { break; } elseif ($exc->getCode() == 201) { continue; } } if (empty($query)) { continue; } // Update variables $this->totalSizeRead += strlen($query); $this->totalQueries++; $this->queries++; $this->lineNumber++; // Process the query line, running drop/rename queries as necessary $this->processQueryLine($query); } // Get the current file position $current_foffset = ftell($this->file); if ($current_foffset === false) { if (is_resource($this->file)) { @fclose($this->file); } throw new \Exception(Text::_('AWF_RESTORE_ERROR_CANTREADPOINTER')); } else { if (is_null($this->fileOffset)) { $this->fileOffset = 0; } $bytes_in_step = $current_foffset - $this->fileOffset; $this->runSize = (is_null($this->runSize) ? 0 : $this->runSize) + $bytes_in_step; $this->fileOffset = $current_foffset; } // Return statistics $bytes_togo = $this->totalSize - $this->runSize; // Check for global EOF if ($this->currentPart >= $parts - 1 && feof($this->file)) { $bytes_togo = 0; } // Save variables in storage $this->setToStorage('start', $this->start); $this->setToStorage('foffset', $this->fileOffset); $this->setToStorage('totalqueries', $this->totalQueries); $this->setToStorage('runsize', $this->runSize); if ($bytes_togo == 0) { // Clear stored variables if we're finished $this->removeInformationFromStorage(); } // Calculate estimated time $bytesPerSecond = $bytes_in_step / $this->timer->getRunningTime(); if ($bytesPerSecond <= 0.01) { $remainingSeconds = 120; } else { $remainingSeconds = round($bytes_togo / $bytesPerSecond, 0); } // Return meaningful data return array('percent' => round(100 * ($this->runSize / $this->totalSize), 1), 'restored' => $this->sizeformat($this->runSize), 'total' => $this->sizeformat($this->totalSize), 'queries_restored' => $this->totalQueries, 'current_line' => $this->lineNumber, 'current_part' => $this->currentPart, 'total_parts' => $parts, 'eta' => $this->etaformat($remainingSeconds), 'error' => '', 'done' => $bytes_togo == 0 ? '1' : '0'); }
/** * Create and return the pagination data object. * * @return object Pagination data object. */ protected function _buildDataObject() { $data = new \stdClass(); $router = $this->application->getContainer()->router; // Build the additional URL parameters string. $params = ''; if (!empty($this->additionalUrlParams)) { foreach ($this->additionalUrlParams as $key => $value) { $params .= '&' . $key . '=' . $value; } } $params = 'index.php?' . substr($params, 1); $data->all = new Object(Text::_('AWF_PAGINATION_LBL_VIEW_ALL')); if (!$this->viewAll) { $data->all->base = '0'; $data->all->link = $router->route($params . '&limitstart='); } // Set the start and previous data objects. $data->start = new Object('«'); $data->previous = new Object('‹'); if ($this->pagesCurrent > 1) { $page = ($this->pagesCurrent - 2) * $this->limit; $data->start->base = '0'; $data->start->link = $router->route($params . '&limitstart=0'); $data->previous->base = $page; $data->previous->link = $router->route($params . '&limitstart=' . $page); } // Set the next and end data objects. $data->next = new Object('›'); $data->end = new Object('»'); if ($this->pagesCurrent < $this->pagesTotal) { $next = $this->pagesCurrent * $this->limit; $end = ($this->pagesTotal - 1) * $this->limit; $data->next->base = $next; $data->next->link = $router->route($params . '&limitstart=' . $next); $data->end->base = $end; $data->end->link = $router->route($params . '&limitstart=' . $end); } $data->pages = array(); $stop = $this->pagesStop; for ($i = $this->pagesStart; $i <= $stop; $i++) { $offset = ($i - 1) * $this->limit; $data->pages[$i] = new Object($i); if ($i != $this->pagesCurrent || $this->viewAll) { $data->pages[$i]->base = $offset; $data->pages[$i]->link = $router->route($params . '&limitstart=' . $offset); } else { $data->pages[$i]->active = true; } } return $data; }
/** * Generates an HTML radio list. * * @param array $data An array of objects * @param string $name The value of the HTML name attribute * @param string $attribs Additional HTML attributes for the <select> tag, or the following * - inline: boolean Create the radio list as inline elements * - radioType: radio|checkbox Use radio buttons (radio) or checkboxes (checkbox) * @param mixed $optKey The key that is selected * @param string $optText The name of the object variable for the option value * @param string $selected The name of the object variable for the option text * @param boolean $idtag Value of the field id or null by default * @param boolean $translate True if options will be translated * * @return string HTML for the select list */ public static function radioList($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false, $translate = false) { reset($data); $inline = false; $button = false; $radioType = 'radio'; if (isset($attribs['inline'])) { $inline = $attribs['inline']; unset($attribs['inline']); } if (isset($attribs['radioType'])) { $radioType = $attribs['radioType']; if (!in_array($radioType, array('radio', 'checkbox'))) { $radioType = 'radio'; } unset($attribs['radioType']); } if (isset($attribs['button'])) { $button = $attribs['button']; unset($attribs['button']); } if (is_array($attribs)) { $attribs = ArrayHelper::toString($attribs); } $id_text = $idtag ? $idtag : $name; $html = ''; if ($button) { $html .= '<div class="btn-group" data-toggle="buttons">'; } foreach ($data as $obj) { $k = $obj->{$optKey}; $t = $translate ? Text::_($obj->{$optText}) : $obj->{$optText}; $id = isset($obj->id) ? $obj->id : null; $extra = ''; $id = $id ? $obj->id : $id_text . $k; if (is_array($selected)) { foreach ($selected as $val) { $k2 = is_object($val) ? $val->{$optKey} : $val; if ($k == $k2) { if ($radioType == 'radio') { $extra .= ' selected="selected" '; } else { $extra .= ' checked="checked" '; } break; } } } else { if ($radioType == 'radio') { $extra .= (string) $k == (string) $selected ? ' checked="checked" ' : ''; } else { $extra .= (string) $k == (string) $selected ? ' selected="selected" ' : ''; } } if (!$inline && !$button) { $html .= "\n<div class=\"{$radioType}\">\n"; } $class = ''; if ($inline) { $class = ' class="' . $radioType . '-inline"'; } elseif ($button) { $class = ' class="btn btn-default"'; } $html .= "\n\t" . '<label' . $class . '>'; $html .= "\n\t\n\t" . '<input type="' . $radioType . '" name="' . $name . '" id="' . $id . '" value="' . $k . '" ' . $extra . $attribs . ' >' . $t; $html .= "\n\t" . '</label>'; if (!$inline && !$button) { $html .= "\n</div>\n"; } } if ($button) { $html .= '</div>'; } return $html; }