Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tag_manager.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
20 
29 class Tag_Manager extends Asset
30 {
31 
32 
37  function Tag_Manager($assetid=0)
38  {
39  $this->_ser_attrs = TRUE;
40  $this->Asset($assetid);
41 
42  }//end constructor
43 
44 
56  function create(&$link)
57  {
58  require_once SQ_CORE_PACKAGE_PATH.'/system/system_asset_fns.inc';
59  if (!system_asset_fns_create_pre_check($this)) {
60  return FALSE;
61  }
62 
63  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
64  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
65 
66  if ($linkid = parent::create($link)) {
67  if (!system_asset_fns_create_cleanup($this)) {
68  $linkid = FALSE;
69  }
70  }
71 
72  if ($linkid) {
73  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
74  } else {
75  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
76  }
77 
78  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
79  return $linkid;
80 
81  }//end create()
82 
83 
93  function _getName($short_name=FALSE)
94  {
95  return $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name');
96 
97  }//end _getName()
98 
99 
106  function canClone()
107  {
108  return FALSE;
109 
110  }//end canClone()
111 
112 
122  function getTagLinks($assetid, $thesaurus_filter_ids=Array())
123  {
124 
125  $tag_links = Array();
126  $notice_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($assetid, SQ_LINK_NOTICE);
127  foreach ($notice_links as $notice_link) {
128  $id_parts = explode(':', $notice_link['minorid']);
129  if (!empty($id_parts[1])) {
130  $thes_id = $id_parts[0];
131  if (empty($thesaurus_filter_ids) || in_array($thes_id, $thesaurus_filter_ids)) {
132  $thes = $GLOBALS['SQ_SYSTEM']->am->getAsset($thes_id, 'thesaurus', TRUE);
133  if (!is_null($thes)) {
134  $tag_term_exists = $GLOBALS['SQ_SYSTEM']->am->getAsset($notice_link['minorid'], 'thesaurus_term', FALSE);
135  if (!is_null($tag_term_exists)) {
136  $tag_links[] = $notice_link;
137  }
138  }
139  }
140  }
141  }
142 
143  return $tag_links;
144 
145  }//end getTagLinks()
146 
147 
158  function isAssetTaggedWith($assetid, $tagids, $strict=FALSE)
159  {
160  if (!is_array($tagids)) $tagids = Array($tagids);
161 
162  $tag_link = $this->getAssetTagLinks($assetid, $tagids);
163  if (!$strict) {
164  return !empty($tag_link);
165  } else {
166  $array_keys = array_keys($tag_link);
167  $result = array_diff($tagids, $array_keys);
168  return empty($result);
169  }
170 
171  }//end isAssetTaggedWith()
172 
173 
183  function getAssetTagLinks($assetid, $tagids)
184  {
185  $tag_links = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($assetid, $tagids, SQ_LINK_NOTICE);
186  if (isset($tag_links['minorid'])) {
187  $tag_links = Array($tag_links);
188  }
189 
190  $links = Array();
191 
192  foreach ($tag_links as $link) {
193  $links[$link['minorid']] = $link;
194  }
195 
196  return $links;
197 
198  }//end getAssetTagLinks()
199 
200 
209  function getAssetLinksByTag($tagid)
210  {
211  // get a list of links to assets from these tags
212 
213  if (!is_array($tagid)) {
214  $tagids = Array($tagid);
215  } else {
216  $tagids = $tagid;
217  }
218 
219  $links = Array();
220  $asset_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($tagids, SQ_LINK_NOTICE, NULL, FALSE, 'minor');
221 
222  return $asset_links;
223 
224  }//end getAssetLinksByTag()
225 
226 
236  function getRelatedAssets($relator_id, $thesaurus_filter_ids=Array())
237  {
238  // Get the tag links for all the relators then all the asset links these tags have.
239  // we need to iterate through the relator's tag links to build the array for getting the tags' assets, so
240  // build another array of link values.
241 
242  if (!is_array($relator_id)) {
243  $relator_ids = Array($relator_id);
244  } else {
245  $relator_ids = $relator_id;
246  }
247 
248 
249  $relator_weightings = Array();
250  foreach ($relator_ids as $relator_id) {
251  $tag_links = $this->getTagLinks($relator_id, $thesaurus_filter_ids);
252  foreach ($tag_links as $tag_link) {
253  $tag_id = $tag_link['minorid'];
254  $current_value = array_get_index($relator_weightings, $tag_id, 0);
255  $relator_weightings[$tag_id] = $current_value + $tag_link['value'];
256  }
257  }
258 
259 
260  // related_asset_links are links from the relatees to the relator's tags
261  $all_related_asset_links = $this->getAssetLinksByTag(array_keys($relator_weightings));
262 
263 
264  $related_ids = Array();
265  // loop through relateds and check relators for each
266  foreach ($all_related_asset_links as $tag_id => $related_asset_links) {
267  $multiplier = array_get_index($relator_weightings, $tag_id);
268 
269  if (is_null($multiplier)) continue;
270 
271  foreach ($related_asset_links as $one_related_link) {
272  $related_assetid = $one_related_link['majorid'];
273  $current_value = array_get_index($related_ids, $related_assetid, 0);
274  $related_ids[$related_assetid] = $current_value + $multiplier * $one_related_link['value'];
275  }
276  }
277 
278  arsort($related_ids);
279 
280 
281  return $related_ids;
282 
283  }//end getRelatedAssets()
284 
285 
294  function isSetTaggingScreen($assetid)
295  {
296  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset((int) $assetid);
297  if (is_null($asset)) return TRUE;
298  $edit_fns = $asset->getEditFns();
299  return isset($edit_fns->static_screens['tagging']);
300 
301  }//end isSetTaggingScreen()
302 
303 
314  function setTag($assetid, $tagids, $weight)
315  {
316  $has_error = FALSE;
317 
318  if ($this->isSetTaggingScreen($assetid)) {
319 
320  if (!is_array($tagids)) $tagids = Array($tagids);
321 
322  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
323  if (is_null($asset)) return 0;
324 
325 
326  foreach ($tagids as $tagid) {
327  if (!$this->isFromTaggingThesaurus($tagid)) {
328  // this tag is not in the required thesaurus
329  trigger_localised_error('CORE0274', E_USER_WARNING);
330  $has_error = TRUE;
331  continue;
332  }
333 
334  $new_tag_object = $GLOBALS['SQ_SYSTEM']->am->getAsset($tagid);
335  if (is_null($new_tag_object)) {
336  trigger_localised_error('SYS0087', E_USER_WARNING, $tagid);
337  $has_error = TRUE;
338  continue;
339  }
340 
341  // see if the asset is already tagged with this tag
342  $tag_links = $this->getTagLinks($assetid);
343  $already_tagged = FALSE;
344  foreach ($tag_links as $link) {
345  if ($tagid == $link['minorid']) {
346  $already_tagged = TRUE;
347  if ($weight == $link['value']) {
348  // already tagged with the same weighting
349  continue;
350  } else {
351  // already tagged, but with a different weighting; scream and shout
352  trigger_localised_error('CORE0275', E_USER_WARNING, $assetid, $weight);
353  $has_error = TRUE;
354  break;
355  }
356  }
357  }//end foreach existing tag link
358 
359  if (!$already_tagged) {
360  if (!$GLOBALS['SQ_SYSTEM']->am->createAssetLink($asset, $new_tag_object, SQ_LINK_NOTICE, $weight)) {
361  $has_error = TRUE;
362  }
363  }
364 
365  }//end foreach tag id
366 
367  }
368 
369  return ($has_error) ? 0 : 1;
370 
371  }//end setTag()
372 
373 
383  function deleteTag($assetid, $tagids)
384  {
385  $has_error = FALSE;
386 
387  if ($this->isSetTaggingScreen($assetid)) {
388 
389  if (!is_array($tagids)) $tagids = Array($tagids);
390 
391  foreach ($tagids as $tag) {
392 
393  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($assetid, $tag, SQ_LINK_NOTICE);
394  if (empty($link)) {
395  trigger_localised_error('CORE0277', E_USER_WARNING, $tag, $assetid);
396  $has_error = TRUE;
397  continue;
398  }
399  if (!$GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($link['linkid'])) {
400  trigger_localised_error('CORE0278', E_USER_WARNING, $tag, $assetid, $link['linkid']);
401  $has_error = TRUE;
402  }
403  }
404  }
405 
406  return ($has_error) ? 0 : 1;
407 
408  }//end deleteTag()
409 
410 
423  function setTagByName($assetid, $tag, $weight)
424  {
425  $thes = $this->getTagThesaurus();
426  $tagid = $thes->getTermIdByName($tag);
427  return $this->setTag($assetid, $tagid, $weight);
428 
429  }//end setTagByName()
430 
431 
438  function &getTagThesaurus()
439  {
440  $thes = NULL;
441  if ($this->attr('thesaurus')) {
442  $thes = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->attr('thesaurus'));
443  }
444  return $thes;
445 
446  }//end getTagThesaurus()
447 
448 
458  function setTagWeight($linkid, $weight)
459  {
460  $success = $GLOBALS['SQ_SYSTEM']->am->updateLink($linkid, SQ_LINK_NOTICE, $weight);
461  return $success;
462 
463  }//end setTagWeight()
464 
465 
475  function isFromTaggingThesaurus($tagid)
476  {
477  // are tags restricted to one thesaurus asset?
478  if ($this->isTagSourceRestricted()) {
479  $id_parts = explode(':', $tagid);
480  if ($this->attr('thesaurus') != $id_parts[0]) {
481  // this tag is not in the required thesaurus
482  return FALSE;
483  }
484  }
485 
486  return TRUE;
487 
488  }//end isFromTaggingThesaurus()
489 
490 
498  {
499  return $this->attr('restrict_tag_source');
500 
501  }//end isTagSourceRestricted()
502 
503 
504 }//end class
505 ?>