PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Engine/Application/Module/Api.php

https://github.com/shopaholiccompany/shopaholic
PHP | 334 lines | 246 code | 45 blank | 43 comment | 37 complexity | 3c5a35e8bc00039216f1f959d033aa80 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Application
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Api.php 7244 2010-09-01 01:49:53Z john $
  10. */
  11. /**
  12. * @category Engine
  13. * @package Engine_Application
  14. * @copyright Copyright 2006-2010 Webligo Developments
  15. * @license http://www.socialengine.net/license/
  16. */
  17. abstract class Engine_Application_Module_Api
  18. {
  19. protected $_moduleName;
  20. protected $_lastResource;
  21. protected $_items;
  22. protected $_supportedItemTypes;
  23. public function api()
  24. {
  25. return Engine_Api::_()->setCurrentModule($this->getModuleName());
  26. }
  27. public function getModuleName()
  28. {
  29. if( empty($this->_moduleName) )
  30. {
  31. $class = get_class($this);
  32. if (preg_match('/^([a-z][a-z0-9]*)_/i', $class, $matches)) {
  33. $prefix = $matches[1];
  34. } else {
  35. $prefix = $class;
  36. }
  37. // @todo sanity
  38. $this->_moduleName = strtolower($prefix);
  39. }
  40. return $this->_moduleName;
  41. }
  42. public function __call($type, $args)
  43. {
  44. if( substr($type, 0, 3) === 'get' )
  45. {
  46. $type = preg_replace('/([A-Z])/', '-\\1', $type);
  47. $parts = explode('-', $type);
  48. array_shift($parts);
  49. $itemType = strtolower(array_shift($parts));
  50. $suffix = join('', $parts);
  51. if( $suffix === '' )
  52. {
  53. return $this->getItem($itemType, $args[0]);
  54. }
  55. else if( $suffix === 'Multi' )
  56. {
  57. return $this->getItemMulti($itemType, $args[0]);
  58. }
  59. else if( $suffix === 'MultiQuery' )
  60. {
  61. return $this->getItemMulti($itemType, $args[0], $args[1]);
  62. }
  63. }
  64. // Backwards
  65. $resource = array_shift($args);
  66. // Experimental
  67. if( !$resource )
  68. {
  69. if( !$this->_lastResource )
  70. {
  71. $this->_lastResource = $type;
  72. return $this;
  73. }
  74. else
  75. {
  76. $resource = $type;
  77. $type = $this->_lastResource;
  78. $this->_lastResource = null;
  79. }
  80. }
  81. $method = 'get'.ucfirst($type);
  82. return $this->api()->$method($resource);
  83. //return Engine_Api::_()->load(strtolower($this->getModuleName()), $type, $resource);
  84. }
  85. public function getItem($type, $identity)
  86. {
  87. $method = 'get' . ucfirst($type);
  88. if( method_exists($this, $method) )
  89. {
  90. return $this->$method($identity);
  91. }
  92. $this->_isTypeSupported($type);
  93. $id = $this->_getIdentity($type, $identity);
  94. if( !isset($this->_items[$type][$id]) )
  95. {
  96. if( !isset($this->_items[$type]) )
  97. {
  98. $this->_items[$type] = array();
  99. }
  100. $class = $this->api()->getItemClass($type);
  101. $item = new $class($identity);
  102. return $this->_items[$type][$item->getIdentity()] = $item;
  103. }
  104. return $this->_items[$type][$id];
  105. }
  106. public function getItemMulti($type, array $ids)
  107. {
  108. $method = 'get' . ucfirst($type) . 'Multi';
  109. if( method_exists($this, $method) )
  110. {
  111. return $this->$method($identity);
  112. }
  113. $this->_isTypeSupported($type);
  114. // Remove any non-numeric values and already retv rows
  115. $getIds = array();
  116. foreach( $ids as $index => $value )
  117. {
  118. if( !is_numeric($value) )
  119. {
  120. unset($ids[$index]);
  121. }
  122. else if( !isset($this->_items[$type][$value]) )
  123. {
  124. $getIds[] = $value;
  125. }
  126. }
  127. // Now get any remaining rows, if necessary
  128. if( !empty($getIds) )
  129. {
  130. $table = $this->api()->getItemTable($type);
  131. $class = $this->api()->getItemClass($type);
  132. //$class = $this->getModuleName() . '_Model_' . ucfirst($type);
  133. foreach( $table->find($getIds) as $row )
  134. {
  135. $item = new $class($row);
  136. $this->_items[$type][$item->getIdentity()] = $item;
  137. }
  138. }
  139. // Now build the return data
  140. $items = array();
  141. foreach( $ids as $id )
  142. {
  143. if( isset($this->_items[$type][$id]) )
  144. {
  145. $items[] = $this->_items[$type][$id];
  146. }
  147. }
  148. return $items;
  149. }
  150. /*
  151. * Get all items of type $type that satisfy the query specified by $query_string and $query_args.
  152. * The format of $query_string and $query_args should be the same as for Zend_Db's select method
  153. */
  154. public function getItemMultiQuery($type, $text, $values, $order = NULL)
  155. {
  156. $method = 'get' . ucfirst($type) . 'MultiQuery';
  157. if( method_exists($this, $method) )
  158. {
  159. return $this->$method($identity);
  160. }
  161. $this->_isTypeSupported($type);
  162. $table = $this->api()->getItemTable($type);
  163. if (!is_array($values)) {
  164. $values = Array($values);
  165. }
  166. foreach ($values as $value)
  167. {
  168. $text = $table->getAdapter()->quoteInto($text, $value, NULL, 1);
  169. }
  170. $id_field_name = $type . '_id';
  171. $items = array();
  172. $select = $table->select()->where($text);
  173. if (!is_null($order))
  174. {
  175. $select = $select->order($order);
  176. }
  177. $rows = $table->fetchAll($select);
  178. foreach( $rows as $row )
  179. {
  180. $items[] = $this->getItem($type, $row);
  181. }
  182. return $items;
  183. }
  184. public function getItemCountQuery($type, $text, $values)
  185. {
  186. $method = 'get' . ucfirst($type) . 'MultiQuery';
  187. if( method_exists($this, $method) )
  188. {
  189. return $this->$method($identity);
  190. }
  191. $this->_isTypeSupported($type);
  192. $table = $this->api()->getItemTable($type);
  193. if (!is_array($values)) {
  194. $values = Array($values);
  195. }
  196. foreach ($values as $value)
  197. {
  198. $text = $table->getAdapter()->quoteInto($text, $value, NULL, 1);
  199. }
  200. $id_field_name = $type . '_id';
  201. $row = $table->fetchRow($table->select()->from($table->info('name'), 'count(*)')->where($text));
  202. return $row['count(*)'];
  203. }
  204. public function getOwnedItems($type, $owner_id, $owner_type = "user")
  205. {
  206. $method = 'get' . ucfirst($type) . 'OwnedItems';
  207. if( method_exists($this, $method) )
  208. {
  209. return $this->$method($identity);
  210. }
  211. return $this->getItemMultiQuery($type, "owner_id = ? AND owner_type = ?", Array($owner_id, $owner_type));
  212. }
  213. /*
  214. * Creates a new item of type $type and inserts it into the appropriate Db table.
  215. * Each $key=>$value pair in $params indicates the desired value of a single table column
  216. */
  217. protected function _createItem($type, $params)
  218. {
  219. $table = Engine_Api::_()->getItemTable($type);
  220. $row = $table->createRow();
  221. if( !isset($params['creation_date']) )
  222. {
  223. $params['creation_date'] = new Zend_Db_Expr('NOW()');
  224. }
  225. if( !isset($params['modified_date']) )
  226. {
  227. $params['modified_date'] = new Zend_Db_Expr('NOW()');
  228. }
  229. foreach( $params as $key => $value )
  230. {
  231. if( isset($row->$key) )
  232. {
  233. $row->$key = $value;
  234. }
  235. }
  236. // Pre-create hook
  237. //Engine_Hooks_Dispatcher::getInstance()
  238. // ->call('on'.ucfirst($type).'CreateBefore', $row);
  239. // Create item
  240. $row->save();
  241. $id_field = $type . '_id';
  242. $item = $this->api()->getItem($type, $row->$id_field);
  243. // Post create hook
  244. //Engine_Hooks_Dispatcher::getInstance()
  245. // ->call('on'.ucfirst($type).'CreateAfter', $item);
  246. return $item;
  247. }
  248. /*
  249. * Update the database row corresponding to item with id $item_id of type $type.
  250. * Each $key=>$value pair in $params indicates the desired value of a single table column
  251. */
  252. protected function _editItem($type, $item_id, $params)
  253. {
  254. $item = $this->getItem($type, $item_id);
  255. if( !isset($params['modified_date']) )
  256. {
  257. $params['modified_date'] = $time; // new Zend_Db_Expr('NOW()');
  258. }
  259. $item->setData($params);
  260. $item->saveData();
  261. }
  262. protected function _isTypeSupported($type)
  263. {
  264. if( !is_array($this->_supportedItemTypes) || !in_array($type, $this->_supportedItemTypes) )
  265. {
  266. //throw new Engine_Exception(sprintf('Type "%s" is no supported', $type));
  267. }
  268. }
  269. protected function _getIdentity($type, $identity)
  270. {
  271. if( is_scalar($identity) )
  272. {
  273. return $identity;
  274. }
  275. else if( $identity instanceof Zend_Db_Table_Row_Abstract )
  276. {
  277. $prop = $type . '_id';
  278. return $identity->$prop;
  279. }
  280. else if( $identity instanceof Core_Model_Item_Abstract )
  281. {
  282. return $identity->getIdentity();
  283. }
  284. else
  285. {
  286. throw new Engine_Exception('Not an identity');
  287. }
  288. }
  289. }