Exemplo n.º 1
0
 /**
  * Display the form allowing to edit a citation
  *
  * @return		 string HTML
  */
 private function _edit()
 {
     // create view object
     $view = $this->view('default', 'edit');
     // Check if they're logged in
     if (User::isGuest()) {
         $this->_loginTask();
     }
     // push objects to view
     $view->group = $this->group;
     $view->isManager = $this->authorized == 'manager' ? true : false;
     $view->config = new \Hubzero\Config\Registry($this->group->get('params'));
     if ($view->isManager == false) {
         App::redirect(Route::url('index.php?option=com_groups&cn=' . $this->group->cn . '&active=citations'), Lang::txt('PLG_GROUPS_CITATIONS_GROUP_MANAGER_ONLY'), 'warning');
     }
     // get the citation types
     $citationsType = \Components\Citations\Models\Type::all();
     $view->types = $citationsType->rows()->toObject();
     $fields = array();
     foreach ($view->types as $type) {
         if (isset($type->fields)) {
             $f = $type->fields;
             if (strpos($f, ',') !== false) {
                 $f = str_replace(',', "\n", $f);
             }
             $f = array_map('trim', explode("\n", $f));
             $f = array_values(array_filter($f));
             $fields[strtolower(str_replace(' ', '', $type->type_title))] = $f;
         }
     }
     // Incoming - expecting an array id[]=4232
     $id = Request::getInt('id', 0);
     // Pub author
     $pubAuthor = false;
     // Is user authorized to edit citations?
     if (!$view->isManager && !$pubAuthor) {
         $id = 0;
     }
     // Load the object
     $view->row = \Components\Citations\Models\Citation::oneorNew($id);
     // make sure title isnt too long
     $maxTitleLength = 30;
     $shortenedTitle = strlen($view->row->title) > $maxTitleLength ? substr($view->row->title, 0, $maxTitleLength) . '…' : $view->row->title;
     // Set the pathway
     if ($id && $id != 0) {
         Pathway::append($shortenedTitle, 'index.php?option=com_citations&task=view&id=' . $view->row->id);
         Pathway::append(Lang::txt('PLG_GROUPS_CITATIONS_EDIT'));
     } else {
         Pathway::append(Lang::txt('PLG_GROUPS_CITATIONS_ADD'));
     }
     // non-owner redirect
     if (!$view->row->isNew() && $view->row->scope != 'group') {
         App::redirect(Route::url('index.php?option=com_groups&cn=' . $this->group->cn . '&active=citations'), Lang::txt('PLG_GROUPS_CITATIONS_PERMISSION_DENIED'), 'warning');
     }
     // Set the page title
     Document::setTitle(Lang::txt('PLG_GROUPS_CITATIONS_CITATION') . $shortenedTitle);
     // push jquery to doc
     Document::addScriptDeclaration('var fields = ' . json_encode($fields) . ';');
     // Instantiate a new view
     $view->title = Lang::txt(strtoupper($this->_name)) . ': ' . Lang::txt(strtoupper($this->_name) . '_' . strtoupper($this->action));
     // No ID, so we're creating a new entry
     // Set the ID of the creator
     if (!$id) {
         $view->row->uid = User::get('id');
         // tags & badges
         $view->tags = array();
         $view->badges = array();
         $view->row->id = -time();
     } else {
         if ($view->row->relatedAuthors->count()) {
             $view->authors = $view->row->relatedAuthors;
         } elseif ($view->row->relatedAuthors->count() == 0 && $view->row->author != '') {
             // formats the author for the multi-author plugin
             $authors = explode(';', $view->row->author);
             $authorString = '';
             $totalAuths = count($authors);
             $x = 0;
             foreach ($authors as &$author) {
                 /***
                  * Because the multi-select keys off of a comma,
                  * imported entries may display incorrectly (Wojkovich, Kevin) breaks the multi-select
                  * Convert this to Kevin Wojkovich and I'll @TODO add some logic in the formatter to
                  * format it properly within the bibilographic format ({LASTNAME},{FIRSTNAME})
                  ***/
                 $authorEntry = explode(',', $author);
                 if (count($authorEntry) == 2) {
                     $author = $authorEntry[1] . ' ' . $authorEntry[0];
                 }
                 $authorString .= $author;
                 if ($totalAuths > 1 && $x < $totalAuths - 1) {
                     $authorString .= ',';
                 }
                 $x = $x + 1;
             }
             $view->authorString = $authorString;
         }
         // tags & badges
         $view->tags = \Components\Citations\Helpers\Format::citationTags($view->row, $this->database, false);
         $view->badges = \Components\Citations\Helpers\Format::citationBadges($view->row, $this->database, false);
     }
     // Output HTML
     foreach ($this->getErrors() as $error) {
         $view->setError($error);
     }
     return $view->loadTemplate();
 }