public function testCollectionAndTags() { $templateJsonString = <<<'EOD' {"itemType":"journalArticle","title":"","creators":[{"creatorType":"author","firstName":"","lastName":""}],"abstractNote":"","publicationTitle":"","volume":"","issue":"","pages":"","date":"","series":"","seriesTitle":"","seriesText":"","journalAbbreviation":"","language":"","DOI":"","ISSN":"","shortTitle":"","url":"","accessDate":"","archive":"","archiveLocation":"","libraryCatalog":"","callNumber":"","rights":"","extra":"","tags":[],"collections":[],"relations":{}} EOD; $templateArray = json_decode($templateJsonString, true); $item = new Zotero_Item(); $item->initItemFromTemplate($templateArray); $item->addToCollection("ASDF1234"); $item->addToCollection("FDSA4321"); $item->addTag("Green"); $item->addTag("purple", 1); $item->addTag("Red"); $itemCollections = $item->get('collections'); $this->assertEquals(count($itemCollections), 2, 'right number of collections'); $this->assertEquals($itemCollections[0], "ASDF1234", "first col match"); $this->assertEquals($itemCollections[1], "FDSA4321", "Second col match"); $itemTags = $item->get('tags'); $this->assertEquals(count($itemTags), 3); $this->assertEquals($itemTags[0], "Green"); $this->assertEquals($itemTags[1]['tag'], "purple"); $this->assertEquals($itemTags[1]['type'], 1); $this->assertEquals($itemTags[2], "Red"); //test removal $item->removeFromCollection('OIUSDGAS'); $item->removeFromCollection("ASDF1234"); $item->removeTag("Red"); $item->removeTag("Green"); $itemCollections = $item->get('collections'); $this->assertEquals(count($itemCollections), 1, 'right number of collections'); $this->assertEquals($itemCollections[0], "FDSA4321", "first col match"); $itemTags = $item->get('tags'); $this->assertEquals(count($itemTags), 1); $this->assertEquals($itemTags[0]['tag'], "purple"); $this->assertEquals($itemTags[0]['type'], 1); }
/** * Get a template for a new item of a certain type * * @param string $itemType type of item the template is for * @return Zotero_Item */ public function getTemplateItem($itemType, $linkMode = null) { $newItem = new Zotero_Item(null, $this); $aparams = array('target' => 'itemTemplate', 'itemType' => $itemType); if ($linkMode) { $aparams['linkMode'] = $linkMode; } $reqUrl = $this->apiRequestString($aparams); libZoteroDebug($reqUrl); $response = $this->_request($reqUrl); if ($response->isError()) { throw new Exception("API error retrieving item template - {$response->getStatus()} : {$response->getRawBody()}"); } libZoteroDebug($response->getRawBody()); $itemTemplate = json_decode($response->getRawBody(), true); $newItem->initItemFromTemplate($itemTemplate); return $newItem; }