/** * Create a new bookmark based on an url and options * * @param array $fields the fields parameters * * @throws Herisson\Model\Exception if bookmark is duplicate * * @return the id of the bookmark created */ public static function createBookmark($fields = array()) { if (!isset($fields['url'])) { throw new Exception(__("Missing Url. Can't create bookmark")); } $url = $fields['url']; if (WpHerissonBookmarksTable::checkDuplicate($url)) { throw new Exception(__("Ignoring duplicate entry : {$url}")); } $bookmark = new WpHerissonBookmarks(); $bookmark->setProperties($fields); $bookmark->save(); if (array_key_exists('tags', $fields) && $fields['tags']) { $bookmark->setTags($fields['tags']); } return $bookmark->id; }
/** * Handle the validation of bookmarks to import after the user choose which bookmarks he wants to import * * @return void */ function importValidateAction() { $bookmarks = post('bookmarks'); $nb = 0; foreach ($bookmarks as $bookmark) { if (array_key_exists('import', $bookmark) && $bookmark['import']) { $nb++; $tags = array_key_exists('tags', $bookmark) ? explode(",", $bookmark['tags']) : array(); /* if (!strlen($bookmark['url'])) { print_r($bookmark); } */ try { WpHerissonBookmarks::createBookmark($bookmark); } catch (\Herisson\Model\Exception $e) { Message::i()->addError($e->getMessage()); continue; } } } Message::i()->addSucces(sprintf(__("Successfully add %s bookmarks !", HERISSON_TD), $nb)); $this->indexAction(); $this->setView('index'); }
/** * Test adding a new bookmark and delete it * * @return void */ public function testSearch() { // Check it's saved in the DB $bookmarks = WpHerissonBookmarksTable::getSearch('example'); $this->assertEquals(0, sizeof($bookmarks)); // Create a sample bookmark $f = new WpHerissonBookmarks(); $f->url = $this->sampleUrl; $f->save(); $f = new WpHerissonBookmarks(); $f->title = $this->sampleName; $f->save(); $f = new WpHerissonBookmarks(); $f->description = $this->sampleDescription; $f->save(); // Check it's saved in the DB $bookmarks = WpHerissonBookmarksTable::getSearch('example'); $this->assertEquals(3, sizeof($bookmarks)); // Delete it and verify it's not there anymore foreach ($bookmarks as $f) { $f->delete(); } $bookmarks = WpHerissonBookmarksTable::getSearch('example'); $this->assertEquals(0, sizeof($bookmarks)); }
/** * Create a sample bookmark * * @param array $fields an array of fields/values to feed the bookmark with fake data * * @return a bookmark object */ private function _getBookmark($fields = array()) { // Create a sample bookmark $b = new WpHerissonBookmarks(); if (sizeof($fields)) { foreach ($fields as $key => $value) { $b->{$key} = $value; } } $b->save(); return $b; }