Example #1
0
 /**
  * Call action (mark as fav, archive, delete, etc.)
  */
 public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose = FALSE, $tags = null)
 {
     switch ($action) {
         case 'add':
             $content = Tools::getPageContent($url);
             $title = $content['rss']['channel']['item']['title'] != '' ? $content['rss']['channel']['item']['title'] : _('Untitled');
             $body = $content['rss']['channel']['item']['description'];
             // clean content from prevent xss attack
             $purifier = $this->_getPurifier();
             $title = $purifier->purify($title);
             $body = $purifier->purify($body);
             //search for possible duplicate
             $duplicate = NULL;
             $duplicate = $this->store->retrieveOneByURL($url->getUrl(), $this->user->getId());
             $last_id = $this->store->add($url->getUrl(), $title, $body, $this->user->getId());
             if ($last_id) {
                 Tools::logm('add link ' . $url->getUrl());
                 if (DOWNLOAD_PICTURES) {
                     $content = Picture::filterPicture($body, $url->getUrl(), $last_id);
                     Tools::logm('updating content article');
                     $this->store->updateContent($last_id, $content, $this->user->getId());
                 }
                 if ($duplicate != NULL) {
                     // duplicate exists, so, older entry needs to be deleted (as new entry should go to the top of list), BUT favorite mark and tags should be preserved
                     Tools::logm('link ' . $url->getUrl() . ' is a duplicate');
                     // 1) - preserve tags and favorite, then drop old entry
                     $this->store->reassignTags($duplicate['id'], $last_id);
                     if ($duplicate['is_fav']) {
                         $this->store->favoriteById($last_id, $this->user->getId());
                     }
                     if ($this->store->deleteById($duplicate['id'], $this->user->getId())) {
                         Tools::logm('previous link ' . $url->getUrl() . ' entry deleted');
                     }
                 }
                 // if there are tags, add them to the new article
                 if (isset($_GET['tags'])) {
                     $_POST['value'] = $_GET['tags'];
                     $_POST['entry_id'] = $last_id;
                     $this->action('add_tag', $url);
                 }
                 $this->messages->add('s', _('the link has been added successfully'));
             } else {
                 $this->messages->add('e', _('error during insertion : the link wasn\'t added'));
                 Tools::logm('error during insertion : the link wasn\'t added ' . $url->getUrl());
             }
             if ($autoclose == TRUE) {
                 Tools::redirect('?view=home&closewin=true');
             } else {
                 Tools::redirect('?view=home');
             }
             return $last_id;
             break;
         case 'delete':
             if (isset($_GET['search'])) {
                 //when we want to apply a delete to a search
                 $tags = array($_GET['search']);
                 $allentry_ids = $this->store->search($tags[0], $this->user->getId());
                 $entry_ids = array();
                 foreach ($allentry_ids as $eachentry) {
                     $entry_ids[] = $eachentry[0];
                 }
             } else {
                 // delete a single article
                 $entry_ids = array($id);
             }
             foreach ($entry_ids as $id) {
                 $msg = 'delete link #' . $id;
                 if ($this->store->deleteById($id, $this->user->getId())) {
                     if (DOWNLOAD_PICTURES) {
                         Picture::removeDirectory(ABS_PATH . $id);
                     }
                     $this->messages->add('s', _('the link has been deleted successfully'));
                 } else {
                     $this->messages->add('e', _('the link wasn\'t deleted'));
                     $msg = 'error : can\'t delete link #' . $id;
                 }
                 Tools::logm($msg);
             }
             Tools::redirect('?');
             break;
         case 'toggle_fav':
             $this->store->favoriteById($id, $this->user->getId());
             Tools::logm('mark as favorite link #' . $id);
             if (Tools::isAjaxRequest()) {
                 echo 1;
                 exit;
             } else {
                 Tools::redirect();
             }
             break;
         case 'toggle_archive':
             if (isset($_GET['tag_id'])) {
                 //when we want to archive a whole tag
                 $tag_id = $_GET['tag_id'];
                 $allentry_ids = $this->store->retrieveEntriesByTag($tag_id, $this->user->getId());
                 $entry_ids = array();
                 foreach ($allentry_ids as $eachentry) {
                     $entry_ids[] = $eachentry[0];
                 }
             } else {
                 //archive a single article
                 $entry_ids = array($id);
             }
             foreach ($entry_ids as $id) {
                 $this->store->archiveById($id, $this->user->getId());
                 Tools::logm('archive link #' . $id);
             }
             if (Tools::isAjaxRequest()) {
                 echo 1;
                 exit;
             } else {
                 Tools::redirect();
             }
             break;
         case 'archive_all':
             $this->store->archiveAll($this->user->getId());
             Tools::logm('archive all links');
             Tools::redirect();
             break;
         case 'add_tag':
             if (isset($_GET['search'])) {
                 //when we want to apply a tag to a search
                 $tags = array($_GET['search']);
                 $allentry_ids = $this->store->search($tags[0], $this->user->getId());
                 $entry_ids = array();
                 foreach ($allentry_ids as $eachentry) {
                     $entry_ids[] = $eachentry[0];
                 }
             } else {
                 //add a tag to a single article
                 $tags = explode(',', $_POST['value']);
                 $entry_ids = array($_POST['entry_id']);
             }
             foreach ($entry_ids as $entry_id) {
                 $entry = $this->store->retrieveOneById($entry_id, $this->user->getId());
                 if (!$entry) {
                     $this->messages->add('e', _('Article not found!'));
                     Tools::logm('error : article not found');
                     Tools::redirect();
                 }
                 //get all already set tags to preven duplicates
                 $already_set_tags = array();
                 $entry_tags = $this->store->retrieveTagsByEntry($entry_id);
                 foreach ($entry_tags as $tag) {
                     $already_set_tags[] = $tag['value'];
                 }
                 foreach ($tags as $key => $tag_value) {
                     $value = trim($tag_value);
                     if ($value && !in_array($value, $already_set_tags)) {
                         $tag = $this->store->retrieveTagByValue($value);
                         if (is_null($tag)) {
                             # we create the tag
                             $tag = $this->store->createTag($value);
                             $sequence = '';
                             if (STORAGE == 'postgres') {
                                 $sequence = 'tags_id_seq';
                             }
                             $tag_id = $this->store->getLastId($sequence);
                         } else {
                             $tag_id = $tag['id'];
                         }
                         # we assign the tag to the article
                         $this->store->setTagToEntry($tag_id, $entry_id);
                     }
                 }
             }
             $this->messages->add('s', _('The tag has been applied successfully'));
             Tools::logm('The tag has been applied successfully');
             Tools::redirect();
             break;
         case 'remove_tag':
             $tag_id = $_GET['tag_id'];
             $entry = $this->store->retrieveOneById($id, $this->user->getId());
             if (!$entry) {
                 $this->messages->add('e', _('Article not found!'));
                 Tools::logm('error : article not found');
                 Tools::redirect();
             }
             $this->store->removeTagForEntry($id, $tag_id);
             Tools::logm('tag entry deleted');
             if ($this->store->cleanUnusedTag($tag_id)) {
                 Tools::logm('tag deleted');
             }
             $this->messages->add('s', _('The tag has been successfully deleted'));
             Tools::redirect();
             break;
         case 'reload_article':
             Tools::logm('reload article');
             $id = $_GET['id'];
             $entry = $this->store->retrieveOneById($id, $this->user->getId());
             Tools::logm('reload url ' . $entry['url']);
             $url = new Url(base64_encode($entry['url']));
             $this->action('add', $url);
             break;
             /* For some unknown reason I can't get displayView() to work here (it redirects to home view afterwards). So here's a dirty fix which redirects directly to URL */
         /* For some unknown reason I can't get displayView() to work here (it redirects to home view afterwards). So here's a dirty fix which redirects directly to URL */
         case 'random':
             Tools::logm('get a random article');
             if ($this->store->getRandomId($this->user->getId())) {
                 $id_array = $this->store->getRandomId($this->user->getId());
                 $id = $id_array[0];
                 Tools::redirect('?view=view&id=' . $id[0]);
                 Tools::logm('got the article with id ' . $id[0]);
             }
             break;
         default:
             break;
     }
 }