Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Tag_Block_All
  • Mage_Tag_Block_Customer_Recent
  • Mage_Tag_Block_Customer_Tags
  • Mage_Tag_Block_Customer_View
  • Mage_Tag_Block_Popular
  • Mage_Tag_Block_Product_List
  • Mage_Tag_Block_Product_Result
  • Mage_Tag_CustomerController
  • Mage_Tag_Helper_Data
  • Mage_Tag_IndexController
  • Mage_Tag_ListController
  • Mage_Tag_Model_Api
  • Mage_Tag_Model_Api_V2
  • Mage_Tag_Model_Entity_Customer_Collection
  • Mage_Tag_Model_Indexer_Summary
  • Mage_Tag_Model_Mysql4_Customer_Collection
  • Mage_Tag_Model_Mysql4_Indexer_Summary
  • Mage_Tag_Model_Mysql4_Popular_Collection
  • Mage_Tag_Model_Mysql4_Product_Collection
  • Mage_Tag_Model_Mysql4_Tag
  • Mage_Tag_Model_Mysql4_Tag_Collection
  • Mage_Tag_Model_Mysql4_Tag_Relation
  • Mage_Tag_Model_Resource_Customer_Collection
  • Mage_Tag_Model_Resource_Indexer_Summary
  • Mage_Tag_Model_Resource_Popular_Collection
  • Mage_Tag_Model_Resource_Product_Collection
  • Mage_Tag_Model_Resource_Tag
  • Mage_Tag_Model_Resource_Tag_Collection
  • Mage_Tag_Model_Resource_Tag_Relation
  • Mage_Tag_Model_Session
  • Mage_Tag_Model_Tag
  • Mage_Tag_Model_Tag_Relation
  • Mage_Tag_ProductController
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Tag
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: /**
 28:  * Product Tag API
 29:  *
 30:  * @category   Mage
 31:  * @package    Mage_Tag
 32:  * @author     Magento Core Team <core@magentocommerce.com>
 33:  */
 34: class Mage_Tag_Model_Api extends Mage_Catalog_Model_Api_Resource
 35: {
 36:     /**
 37:      * Retrieve list of tags for specified product
 38:      *
 39:      * @param int $productId
 40:      * @param string|int $store
 41:      * @return array
 42:      */
 43:     public function items($productId, $store = null)
 44:     {
 45:         $result = array();
 46:         // fields list to return
 47:         $fieldsForResult = array('tag_id', 'name');
 48: 
 49:         /** @var $product Mage_Catalog_Model_Product */
 50:         $product = Mage::getModel('catalog/product')->load($productId);
 51:         if (!$product->getId()) {
 52:             $this->_fault('product_not_exists');
 53:         }
 54: 
 55:         /** @var $tags Mage_Tag_Model_Resource_Tag_Collection */
 56:         $tags = Mage::getModel('tag/tag')->getCollection()->joinRel()->addProductFilter($productId);
 57:         if ($store) {
 58:             $tags->addStoreFilter($this->_getStoreId($store));
 59:         }
 60: 
 61:         /** @var $tag Mage_Tag_Model_Tag */
 62:         foreach ($tags as $tag) {
 63:             $result[$tag->getId()] = $tag->toArray($fieldsForResult);
 64:         }
 65: 
 66:         return $result;
 67:     }
 68: 
 69:     /**
 70:      * Retrieve tag info as array('name'-> .., 'status' => ..,
 71:      * 'base_popularity' => .., 'products' => array($productId => $popularity, ...))
 72:      *
 73:      * @param int $tagId
 74:      * @param string|int $store
 75:      * @return array
 76:      */
 77:     public function info($tagId, $store)
 78:     {
 79:         $result = array();
 80:         $storeId = $this->_getStoreId($store);
 81:         /** @var $tag Mage_Tag_Model_Tag */
 82:         $tag = Mage::getModel('tag/tag')->setStoreId($storeId)->setAddBasePopularity()->load($tagId);
 83:         if (!$tag->getId()) {
 84:             $this->_fault('tag_not_exists');
 85:         }
 86:         $result['status'] = $tag->getStatus();
 87:         $result['name'] = $tag->getName();
 88:         $result['base_popularity'] = (is_numeric($tag->getBasePopularity())) ? $tag->getBasePopularity() : 0;
 89:         // retrieve array($productId => $popularity, ...)
 90:         $result['products'] = array();
 91:         $relatedProductsCollection = $tag->getEntityCollection()->addTagFilter($tagId)
 92:             ->addStoreFilter($storeId)->addPopularity($tagId);
 93:         foreach ($relatedProductsCollection as $product) {
 94:             $result['products'][$product->getId()] = $product->getPopularity();
 95:         }
 96: 
 97:         return $result;
 98:     }
 99: 
100:     /**
101:      * Add tag(s) to product.
102:      * Return array of added/updated tags as array($tagName => $tagId, ...)
103:      *
104:      * @param array $data
105:      * @return array
106:      */
107:     public function add($data)
108:     {
109:         $data = $this->_prepareDataForAdd($data);
110:         /** @var $product Mage_Catalog_Model_Product */
111:         $product = Mage::getModel('catalog/product')->load($data['product_id']);
112:         if (!$product->getId()) {
113:             $this->_fault('product_not_exists');
114:         }
115:         /** @var $customer Mage_Customer_Model_Customer */
116:         $customer = Mage::getModel('customer/customer')->load($data['customer_id']);
117:         if (!$customer->getId()) {
118:             $this->_fault('customer_not_exists');
119:         }
120:         $storeId = $this->_getStoreId($data['store']);
121: 
122:         try {
123:             /** @var $tag Mage_Tag_Model_Tag */
124:             $tag = Mage::getModel('tag/tag');
125:             $tagNamesArr = Mage::helper('tag')->cleanTags(Mage::helper('tag')->extractTags($data['tag']));
126:             foreach ($tagNamesArr as $tagName) {
127:                 // unset previously added tag data
128:                 $tag->unsetData();
129:                 $tag->loadByName($tagName);
130:                 if (!$tag->getId()) {
131:                     $tag->setName($tagName)
132:                         ->setFirstCustomerId($customer->getId())
133:                         ->setFirstStoreId($storeId)
134:                         ->setStatus($tag->getPendingStatus())
135:                         ->save();
136:                 }
137:                 $tag->saveRelation($product->getId(), $customer->getId(), $storeId);
138:                 $result[$tagName] = $tag->getId();
139:             }
140:         } catch (Mage_Core_Exception $e) {
141:             $this->_fault('save_error', $e->getMessage());
142:         }
143: 
144:         return $result;
145:     }
146: 
147:     /**
148:      * Change existing tag information
149:      *
150:      * @param int $tagId
151:      * @param array $data
152:      * @param string|int $store
153:      * @return bool
154:      */
155:     public function update($tagId, $data, $store)
156:     {
157:         $data = $this->_prepareDataForUpdate($data);
158:         $storeId = $this->_getStoreId($store);
159:         /** @var $tag Mage_Tag_Model_Tag */
160:         $tag = Mage::getModel('tag/tag')->setStoreId($storeId)->setAddBasePopularity()->load($tagId);
161:         if (!$tag->getId()) {
162:             $this->_fault('tag_not_exists');
163:         }
164: 
165:         // store should be set for 'base_popularity' to be saved in Mage_Tag_Model_Resource_Tag::_afterSave()
166:         $tag->setStore($storeId);
167:         if (isset($data['base_popularity'])) {
168:             $tag->setBasePopularity($data['base_popularity']);
169:         }
170:         if (isset($data['name'])) {
171:             $tag->setName(trim($data['name']));
172:         }
173:         if (isset($data['status'])) {
174:             // validate tag status
175:             if (!in_array($data['status'], array(
176:                 $tag->getApprovedStatus(), $tag->getPendingStatus(), $tag->getDisabledStatus()))) {
177:                 $this->_fault('invalid_data');
178:             }
179:             $tag->setStatus($data['status']);
180:         }
181: 
182:         try {
183:             $tag->save();
184:         } catch (Mage_Core_Exception $e) {
185:             $this->_fault('save_error', $e->getMessage());
186:         }
187: 
188:         return true;
189:     }
190: 
191:     /**
192:      * Remove existing tag
193:      *
194:      * @param int $tagId
195:      * @return bool
196:      */
197:     public function remove($tagId)
198:     {
199:         /** @var $tag Mage_Tag_Model_Tag */
200:         $tag = Mage::getModel('tag/tag')->load($tagId);
201:         if (!$tag->getId()) {
202:             $this->_fault('tag_not_exists');
203:         }
204:         try {
205:             $tag->delete();
206:         } catch (Mage_Core_Exception $e) {
207:             $this->_fault('remove_error', $e->getMessage());
208:         }
209: 
210:         return true;
211:     }
212: 
213:     /**
214:      * Check data before add
215:      *
216:      * @param array $data
217:      * @return array
218:      */
219:     protected function _prepareDataForAdd($data)
220:     {
221:         if (!isset($data['product_id']) or !isset($data['tag'])
222:             or !isset($data['customer_id']) or !isset($data['store'])) {
223:             $this->_fault('invalid_data');
224:         }
225: 
226:         return $data;
227:     }
228: 
229:     /**
230:      * Check data before update
231:      *
232:      * @param $data
233:      * @return
234:      */
235:     protected function _prepareDataForUpdate($data)
236:     {
237:         // $data should contain at least one field to change
238:         if ( !(isset($data['name']) or isset($data['status']) or isset($data['base_popularity']))) {
239:             $this->_fault('invalid_data');
240:         }
241: 
242:         return $data;
243:     }
244: }
245: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0