/gadgets/Tags/Model/Admin/Tags.php

https://github.com/jaws-project/jaws · PHP · 434 lines · 257 code · 38 blank · 139 comment · 36 complexity · 3c9a0fe3b0e9c9acad527339ebb29502 MD5 · raw file

  1. <?php
  2. /**
  3. * Tags Gadget Admin
  4. *
  5. * @category GadgetModel
  6. * @package Tags
  7. */
  8. class Tags_Model_Admin_Tags extends Jaws_Gadget_Model
  9. {
  10. /**
  11. * Update a gadget reference tags
  12. *
  13. * @access public
  14. * @param string $gadget gadget name
  15. * @param string $action action name
  16. * @param int $reference reference
  17. * @param bool $published reference published?
  18. * @param int $update_time reference update time
  19. * @param string/array $tags comma separated of tags name (tag1, tag2, tag3, ...)
  20. * @param int $user User owner of tag(0: for global tags)
  21. * @return mixed Array of Tag info or Jaws_Error on failure
  22. */
  23. function UpdateReferenceTags($gadget, $action , $reference, $published, $update_time, $tags, $user = 0)
  24. {
  25. $update_time = empty($update_time)? time() : $update_time;
  26. // First - Update old tag info
  27. $table = Jaws_ORM::getInstance()->table('tags_references');
  28. $table->update(array('published' => $published, 'update_time' => $update_time));
  29. $table->where('gadget', $gadget);
  30. $table->and()->where('action', $action);
  31. $table->and()->where('reference', $reference);
  32. $table->exec();
  33. $oldTags = $this->GetReferenceTags($gadget, $action, $reference, $user);
  34. if (Jaws_Error::IsError($oldTags)) {
  35. return $oldTags;
  36. }
  37. if (!is_array($tags)) {
  38. $tags = array_filter(array_map('Jaws_UTF8::trim', preg_split('/,|;|،/u', $tags)));
  39. }
  40. $to_be_added_tags = array_diff($tags, $oldTags);
  41. $res = $this->InsertReferenceTags(
  42. $gadget, $action, $reference, $published,
  43. $update_time, $to_be_added_tags, $user
  44. );
  45. if (Jaws_Error::IsError($res)) {
  46. return $res;
  47. }
  48. $to_be_removed_tags = array_diff($oldTags, $tags);
  49. $res = $this->DeleteReferenceTags($gadget, $action, $reference, $to_be_removed_tags, $user);
  50. if (Jaws_Error::IsError($res)) {
  51. return $res;
  52. }
  53. return true;
  54. }
  55. /**
  56. * Inserts gadget reference tags
  57. *
  58. * @access public
  59. * @param string $gadget gadget name
  60. * @param string $action action name
  61. * @param int $reference reference
  62. * @param bool $published reference published?
  63. * @param int $update_time reference update time
  64. * @param string/array $tagsString comma separated of tags name (tag1, tag2, tag3, ...)
  65. * @param int $user User owner of tag(0: for global tags)
  66. * @return mixed Array of Tag info or Jaws_Error on failure
  67. */
  68. function InsertReferenceTags($gadget, $action, $reference, $published, $update_time, $tags, $user = 0)
  69. {
  70. if (!is_array($tags)) {
  71. $tags = array_filter(array_map('Jaws_UTF8::trim', preg_split('/,|;|،/u', $tags)));
  72. }
  73. if (empty($tags)) {
  74. return true;
  75. }
  76. $references = array();
  77. $objORM = Jaws_ORM::getInstance()->table('tags');
  78. foreach($tags as $tag){
  79. $tagName = $this->GetRealFastUrl($tag, null, false);
  80. $tagId = $objORM->igsert(
  81. array(
  82. 'name' => $tagName,
  83. 'title' => $tag,
  84. 'user' => (int)$user
  85. )
  86. )->where('name', $tagName)
  87. ->and()
  88. ->where('user', (int)$user)
  89. ->exec();
  90. if (Jaws_Error::IsError($tagId)) {
  91. return $tagId;
  92. }
  93. $references[] = array($user, $gadget , $action, $reference, $tagId, time(), $published, $update_time);
  94. }
  95. return $objORM->table('tags_references')->insertAll(
  96. array('user', 'gadget', 'action', 'reference', 'tag', 'insert_time', 'published', 'update_time'),
  97. $references
  98. )->exec();
  99. }
  100. /**
  101. * Delete gadget reference tags
  102. *
  103. * @access public
  104. * @param string $gadget gadget name
  105. * @param string $action action name
  106. * @param int|array $references references
  107. * @param string|array $tags array|string of tags names
  108. * @param int $user User owner of tag(0: for global tags)
  109. * @return mixed Array of Tag info or Jaws_Error on failure
  110. */
  111. function DeleteReferenceTags($gadget, $action ,$references, $tags = null, $user = 0)
  112. {
  113. if (!is_null($tags)) {
  114. if (!is_array($tags)) {
  115. $tags = array_filter(array_map('Jaws_UTF8::trim', preg_split('/,|;|،/u', $tags)));
  116. }
  117. if (empty($tags)) {
  118. return false;
  119. }
  120. }
  121. if (!is_array($references)) {
  122. $references = array((int)$references);
  123. }
  124. $table = Jaws_ORM::getInstance()->table('tags_references')->delete()
  125. ->where('gadget', $gadget)
  126. ->and()
  127. ->where('action', $action)
  128. ->and()
  129. ->where('reference', $references, 'in');
  130. if (!is_null($tags)) {
  131. // internal query
  132. $objInternal = Jaws_ORM::getInstance()->table('tags');
  133. $objInternal->select('tags.id')
  134. ->where('user', (int)$user)
  135. ->and()
  136. ->where('tags.name', $tags, 'in')
  137. ->and()
  138. ->where('tags.id', array('tags_references.tag', 'expr'));
  139. $table->and()->where($objInternal, '', 'is not null');
  140. }
  141. return $table->exec();
  142. }
  143. /**
  144. * Add an new tag
  145. *
  146. * @access public
  147. * @param array $data Tag data
  148. * @param int $user User owner of tag(0: for global tags)
  149. * @return mixed Array of Tag info or Jaws_Error on failure
  150. */
  151. function AddTag($data, $user = 0)
  152. {
  153. $data['user'] = (int)$user;
  154. $data['name'] = $this->GetRealFastUrl($data['name'], null, false);
  155. return Jaws_ORM::getInstance()->table('tags')->insert($data)->exec(JAWS_ERROR_NOTICE);
  156. }
  157. /**
  158. * Update a tag
  159. *
  160. * @access public
  161. * @param int $id Tag id
  162. * @param array $data Tag data
  163. * @param int $user User owner of tag(0: for global tags)
  164. * @return mixed Array of Tag info or Jaws_Error on failure
  165. */
  166. function UpdateTag($id, $data, $user = 0)
  167. {
  168. $data['user'] = (int)$user;
  169. $data['name'] = $this->GetRealFastUrl($data['name'], null, false);
  170. $table = Jaws_ORM::getInstance()->table('tags');
  171. return $table->update($data)->where('id', $id)->exec();
  172. }
  173. /**
  174. * Delete tags
  175. *
  176. * @access public
  177. * @param array $ids Tags id
  178. * @param int $user User owner of tag(0: for global tags)
  179. * @return mixed True/False or Jaws_Error on failure
  180. */
  181. function DeleteTags($ids, $user = 0)
  182. {
  183. // start Transaction
  184. $objORM = Jaws_ORM::getInstance()->beginTransaction();
  185. // internal query
  186. $objInternal = Jaws_ORM::getInstance()->table('tags');
  187. $objInternal->select('tags.id')
  188. ->where('tags.id', $ids, 'in')
  189. ->and()
  190. ->where('user', (int)$user)
  191. ->and()
  192. ->where('tags.id', array('tags_references.tag', 'expr'));
  193. // delete references
  194. $result = $objORM->table('tags_references')->delete()->where($objInternal, '', 'is not null')->exec();
  195. if (Jaws_Error::IsError($result)) {
  196. return $result;
  197. }
  198. // delete tags
  199. $result = $objORM->table('tags')->delete()
  200. ->where('id', $ids, 'in')
  201. ->and()
  202. ->where('user', $user)
  203. ->exec();
  204. if (Jaws_Error::IsError($result)) {
  205. return $result;
  206. }
  207. // commit Transaction
  208. $objORM->commit();
  209. return true;
  210. }
  211. /**
  212. * Merge tags
  213. *
  214. * @access public
  215. * @param array $ids Tags id
  216. * @param string $newName New tag name
  217. * @param int $user User owner of tag(0: for global tags)
  218. * @return array Response array (notice or error)
  219. */
  220. function MergeTags($ids, $newName, $user = 0)
  221. {
  222. //Start Transaction
  223. $objORM = Jaws_ORM::getInstance()->beginTransaction();
  224. // Adding new tag
  225. $newTag = $this->AddTag(array('name' => $newName, 'title' => $newName), $user);
  226. if (Jaws_Error::IsError($newTag)) {
  227. return $newTag;
  228. }
  229. // Replacing tag of references with new tag
  230. $objORM->table('tags_references');
  231. $result = $objORM->update(array('tag' => (int)$newTag))
  232. ->where('user', (int)$user)
  233. ->and()
  234. ->where('tag', $ids, 'in')
  235. ->exec();
  236. if (Jaws_Error::IsError($result)) {
  237. return $result;
  238. }
  239. // Delete duplicate resource tags
  240. $objInternal = Jaws_ORM::getInstance()->table('tags_references')
  241. ->select('min(id)')
  242. ->where('user', (int)$user)
  243. ->groupBy('user', 'gadget', 'action', 'reference', 'tag');
  244. $result = $objORM->table('tags_references')
  245. ->delete()
  246. ->where('user', (int)$user)
  247. ->and()
  248. ->where('id', $objInternal, 'not in')
  249. ->exec();
  250. if (Jaws_Error::IsError($result)) {
  251. return $result;
  252. }
  253. // Delete old tags
  254. $result = $objORM->table('tags')->delete()
  255. ->where('id', $ids, 'in')
  256. ->and()
  257. ->where('id', $newTag, '<>')
  258. ->and()
  259. ->where('user', $user)
  260. ->exec();
  261. if (Jaws_Error::IsError($result)) {
  262. return $result;
  263. }
  264. //Commit Transaction
  265. $objORM->commit();
  266. return true;
  267. }
  268. /**
  269. * Get a tag info
  270. *
  271. * @access public
  272. * @param int $id Tag id
  273. * @return mixed Array of Tag info or Jaws_Error on failure
  274. */
  275. function GetTag($id)
  276. {
  277. $table = Jaws_ORM::getInstance()->table('tags');
  278. $table->select('name', 'user:integer', 'title', 'description', 'meta_keywords', 'meta_description');
  279. $result = $table->where('id', $id)->fetchRow();
  280. if (Jaws_Error::IsError($result)) {
  281. return new Jaws_Error($result->getMessage());
  282. }
  283. return $result;
  284. }
  285. /**
  286. * Get reference tags
  287. *
  288. * @access public
  289. * @param string $gadget Gadget name
  290. * @param string $action Action name
  291. * @param int $reference Reference
  292. * @param int $user User owner of tag(0: for global tags)
  293. * @return mixed Array of Tags info or Jaws_Error on failure
  294. */
  295. function GetReferenceTags($gadget, $action, $reference, $user = 0)
  296. {
  297. $table = Jaws_ORM::getInstance()->table('tags');
  298. $table->select('tags.name');
  299. $table->join('tags_references', 'tags_references.tag', 'tags.id');
  300. return $table->where('tags.user', (int)$user)
  301. ->and()->where('gadget', $gadget)
  302. ->and()->where('action', $action)
  303. ->and()->where('reference', (int)$reference)
  304. ->fetchColumn();
  305. }
  306. /**
  307. * Get tags
  308. *
  309. * @access public
  310. * @param array $filters Data that will be used in the filter
  311. * @param int $limit How many tags
  312. * @param mixed $offset Offset of data
  313. * @param int $user User owner of tag(0: for global tags)
  314. * @return mixed Array of Tags info or Jaws_Error on failure
  315. */
  316. function GetTags($filters = array(), $limit = null, $offset = 0, $user = 0)
  317. {
  318. $table = Jaws_ORM::getInstance()->table('tags');
  319. $table->select('tags.id:integer', 'name', 'title', 'count(tags_references.gadget) as usage_count:integer');
  320. $table->join('tags_references', 'tags_references.tag', 'tags.id', 'left');
  321. $table->groupBy('tags.id', 'name', 'title');
  322. $table->where('tags.user', (int)$user);
  323. if (!empty($filters) && count($filters) > 0) {
  324. if (array_key_exists('name', $filters) && !empty($filters['name'])) {
  325. $table->and()->openWhere('name', $filters['name'], 'like')->or();
  326. $table->closeWhere('title', $filters['name'], 'like');
  327. }
  328. if (array_key_exists('gadget', $filters) && !empty($filters['gadget'])) {
  329. $table->and()->where('gadget', $filters['gadget']);
  330. }
  331. if (array_key_exists('action', $filters) && !empty($filters['action'])) {
  332. $table->and()->where('action', $filters['action']);
  333. }
  334. }
  335. return $table->limit($limit, $offset)->fetchAll();
  336. }
  337. /**
  338. * Get tags count
  339. *
  340. * @access public
  341. * @param array $filters Data that will be used in the filter
  342. * @param int $user User owner of tag(0: for global tags)
  343. * @return mixed Array of Tags info or Jaws_Error on failure
  344. */
  345. function GetTagsCount($filters = array(), $user = 0)
  346. {
  347. $table = Jaws_ORM::getInstance()->table('tags');
  348. $table->select('tags.id', 'count(tags.id):integer');
  349. $table->join('tags_references', 'tags_references.tag', 'tags.id', 'left');
  350. $table->groupBy('tags.id');
  351. $table->where('tags.user', (int)$user);
  352. if (!empty($filters) && count($filters) > 0) {
  353. if (array_key_exists('name', $filters) && !empty($filters['name'])) {
  354. $table->and()->openWhere('name', $filters['name'], 'like')->or();
  355. $table->closeWhere('title', $filters['name'], 'like');
  356. }
  357. if (array_key_exists('gadget', $filters) && !empty($filters['gadget'])) {
  358. $table->and()->where('gadget', $filters['gadget']);
  359. }
  360. if (array_key_exists('action', $filters) && !empty($filters['action'])) {
  361. $table->and()->where('action', $filters['action']);
  362. }
  363. }
  364. $result = $table->fetchColumn();
  365. return Jaws_Error::IsError($result)? 0 : count($result);
  366. }
  367. /**
  368. * Get a gadget available actions
  369. *
  370. * @access public
  371. * @param string $gadget Gadget name
  372. * @return array gadget actions
  373. */
  374. function GetGadgetActions($gadget)
  375. {
  376. $table = Jaws_ORM::getInstance()->table('tags');
  377. $table->select('tags_references.action');
  378. $table->join('tags_references', 'tags_references.tag', 'tags.id', 'left');
  379. $result = $table->groupBy('tags_references.action')->where('tags_references.gadget', $gadget)->fetchColumn();
  380. if (Jaws_Error::IsError($result)) {
  381. return new Jaws_Error($result->getMessage());
  382. }
  383. return $result;
  384. }
  385. /**
  386. * Delete all gadget tags
  387. *
  388. * @access public
  389. * @param string $gadget gadget name
  390. * @return mixed True or Jaws_Error on failure
  391. */
  392. function DeleteGadgetTags($gadget)
  393. {
  394. return Jaws_ORM::getInstance()->table('tags_references')->delete()->where('gadget', $gadget)->exec();
  395. }
  396. }