/** * @param Request $request * * @return string * @throws \Exception * @throws exceptions\NotFoundException */ public function run(Request $request) { if ($request->post('action')) { $method = $request->post('action'); if (method_exists($this, $method)) { $this->{$method}($request); } else { throw new BadMethodCallException(); } } $repo = new AuthorRepository($this->db); $author = $repo->where('id', '=', $request->get('id'))->findSingle(); if (!$author) { throw new NotFoundException('author not found'); } $repo = new PublicationRepository($this->db); $publications = $repo->where('author_id', '=', $request->get('id'))->order('date_published', 'DESC')->find(); /* * The configuration of the index parameters and the selection * of the requested indices is realized in the Config class of * Publin by using class constants. As class constants can't be * arrays below PHP 5.6, the configuration is done by using a * string with an array like syntax. The strings have to be * converted back to an array before calling e.g. the method * fetchIndices($requestedIndices). */ if (!is_null(Config::INDICES_PARAMETERS)) { $parameters = IndexHelper::convertStringToArray(Config::INDICES_PARAMETERS, true); } else { $parameters = array(); } if (!is_null(Config::INDICES_SELECTION)) { $requestedIndices = IndexHelper::convertStringToArray(Config::INDICES_SELECTION, false); } else { $requestedIndices = null; } $this->configureIndices($parameters, $request); $indices = $this->fetchIndices($requestedIndices); if ($request->get('m') === 'edit') { $view = new AuthorView($author, $publications, $indices, $this->errors, true); } else { $view = new AuthorView($author, $publications, $indices, $this->errors); } return $view->display(); }
/** * @param Request $request * * @return bool */ public function searchAuthors(Request $request) { $field = Validator::sanitizeText($request->get('field')); $search = Validator::sanitizeText($request->get('search')); if (!$search) { $this->errors[] = 'Your search input is invalid'; return false; } $search_words = explode(' ', $search); $repo = new AuthorRepository($this->db); switch (true) { case $field === 'given': foreach ($search_words as $word) { $repo->where('given', 'LIKE', '%' . $word . '%'); } break; case $field === 'family': foreach ($search_words as $word) { $repo->where('family', 'LIKE', '%' . $word . '%'); } break; case $field === 'about': foreach ($search_words as $word) { $repo->where('about', 'LIKE', '%' . $word . '%'); } break; default: throw new UnexpectedValueException(); } $this->result = $repo->order('family', 'ASC')->find(); return true; }