Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
thesaurus.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 require_once SQ_FUDGE_PATH.'/general/text.inc';
20 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
21 require_once SQ_CORE_PACKAGE_PATH.'/interfaces/bridge/bridge.inc';
22 
35 class Thesaurus extends Asset implements Bridge
36 {
37 
38 
45  function Thesaurus($assetid=0)
46  {
47  $this->_ser_attrs = TRUE;
48  $this->Asset($assetid);
49 
50  }//end constructor
51 
52 
61  function delete($release_lock)
62  {
63  if (parent::delete($release_lock)) {
64  $this->_deleteStoredContents();
65  return TRUE;
66  }
67 
68  return FALSE;
69 
70  }//end delete()
71 
72 
83  function _createAdditional(&$link)
84  {
85  if (!parent::_createAdditional($link)) return FALSE;
86  return create_directory($this->data_path);
87 
88  }//end _createAdditional()
89 
90 
91 //-- BUILT-IN RELATION SUPPORT --//
92 
93 
104  function getAbbreviations()
105  {
106  $abbreviation_attr = $this->getAttribute('abbreviation_rel');
107  $abbreviation_rel = $abbreviation_attr->value;
108  if (empty($abbreviation_rel)) {
109  $abbreviation_rel = NULL;
110  }
111 
112  $abbr_rows = $this->getRelatedTerms($abbreviation_rel);
113 
114  if (is_null($abbr_rows)) return Array();
115 
116  $result = Array();
117  foreach ($abbr_rows as $abbr) {
118  $result[$abbr['major']] = $abbr['minor'];
119  }
120 
121  return $result;
122 
123  }//end getAbbreviations()
124 
125 
134  function getSynonymsForTerm($term)
135  {
136  $termid = $this->getTermIdByName($term);
137 
138  if (empty($termid)) return Array();
139 
140  $synonym_attr = $this->getAttribute('synonym_rel');
141  $synonym_rel = $synonym_attr->value;
142  if (empty($synonym_rel)) $synonym_rel = NULL;
143 
144  $synonyms = $this->getChildTerms($termid, $synonym_rel);
145  $result = Array();
146 
147  if (!empty($synonyms)) {
148  foreach ($synonyms as $synonym) {
149  $result[$synonym['term']] = $synonym['term'];
150  }
151  }
152 
153  return $result;
154 
155  }//end getSynonymsForTerm()
156 
157 
158 //-- CORE THESAURUS RELATIONAL ENGINE --//
159 
160 
172  function getChildTerms($termid, $relid=NULL)
173  {
174 
175  $limit_relation = TRUE;
176  if (is_null($relid)) $limit_relation = FALSE;
177 
178  $db = MatrixDAL::getDb();
179 
180  $sql = '
181  SELECT
182  linkid, term, termid, relid
183  FROM
184  sq_thes_term term
185  INNER JOIN sq_thes_lnk link ON term.termid = link.minor
186  WHERE
187  term.thesid = :assetid_1
188  AND
189  link.thesid = :assetid_2
190  AND
191  major = :termid
192  ';
193 
194  if ($limit_relation) {
195  $sql .= '
196  AND
197  relid = :relid
198  ';
199  }
200 
201  $sql .= '
202  ORDER BY
203  term
204  ';
205 
206  $query = MatrixDAL::preparePdoQuery($sql);
207  MatrixDAL::bindValueToPdo($query, 'assetid_1', $this->id);
208  MatrixDAL::bindValueToPdo($query, 'assetid_2', $this->id);
209  MatrixDAL::bindValueToPdo($query, 'termid', $termid);
210  if ($limit_relation) {
211  MatrixDAL::bindValueToPdo($query, 'relid', $relid);
212  }//end if
213 
214  try {
215  $result = MatrixDAL::executePdoAll($query);
216  } catch (Exception $e) {
217  throw new Exception('Unable to get thesaurus child terms due to database error: '. $e->getMessage());
218  }//end
219 
220  return $result;
221 
222  }//end getChildTerms()
223 
224 
235  function countChildTerms($termid)
236  {
237 
238  $db = MatrixDAL::getDb();
239 
240  $bind_vars = Array (
241  'assetid' => $this->id,
242  'termid' => $termid,
243  );
244 
245  try {
246  $result = MatrixDAL::executeOne('thesaurus', 'countChildTerms', $bind_vars);
247  } catch (Exception $e) {
248  throw new Exception('Unable to count thesaurus child terms due to database error: '. $e->getMessage());
249  }//end
250 
251  return $result;
252 
253  }//end countChildTerms()
254 
255 
262  function countTermLinks()
263  {
264 
265  $db = MatrixDAL::getDb();
266 
267  $bind_vars['assetid'] = $this->id;
268 
269  try {
270  $result = MatrixDAL::executeOne('thesaurus', 'countTermLinks', $bind_vars);
271  } catch (Exception $e) {
272  throw new Exception('Unable to count thesaurus term links due to database error: '. $e->getMessage());
273  }//end
274 
275  return $result;
276 
277  }//end countTermLinks()
278 
279 
286  function countTerms()
287  {
288 
289  $db = MatrixDAL::getDb();
290 
291  $bind_vars['assetid'] = $this->id;
292 
293  try {
294  $result = MatrixDAL::executeOne('thesaurus', 'countTerms', $bind_vars);
295  } catch (Exception $e) {
296  throw new Exception('Unable to count thesaurus terms due to database error: '. $e->getMessage());
297  }//end
298 
299 
300  return $result;
301 
302  }//end countTerms()
303 
304 
315  function countParentTerms($termid)
316  {
317 
318  $db = MatrixDAL::getDb();
319 
320  $bind_vars = Array (
321  'assetid' => $this->id,
322  'termid' => $termid,
323  );
324 
325  try {
326  $result = MatrixDAL::executeOne('thesaurus', 'countParentTerms', $bind_vars);
327  } catch (Exception $e) {
328  throw new Exception('Unable to count thesaurus parent terms due to database error: '. $e->getMessage());
329  }//end
330 
331 
332  return $result;
333 
334  }//end countParentTerms()
335 
336 
347  function getChildRelationsForTerm($termid)
348  {
349 
350  $db = MatrixDAL::getDb();
351 
352  $bind_vars = Array (
353  'assetid' => $this->id,
354  'termid' => $termid,
355  );
356 
357  try {
358  $results = MatrixDAL::executeAssoc('thesaurus', 'getChildRelationsForTerm', $bind_vars);
359  } catch (Exception $e) {
360  throw new Exception('Unable to get child relations for term due to database error: '. $e->getMessage());
361  }//end
362  if (!empty($results)) {
363  $new_res = Array();
364  foreach ($results as $result) {
365  $new_res[$result['relid']] = $result['relation'];
366  }//end foreach
367  $results = $new_res;
368  }//end if
369 
370  return $results;
371 
372  }//end getChildRelationsForTerm()
373 
374 
385  function getParentRelationsForTerm($termid)
386  {
387 
388  if (!isset($termid)) return NULL;
389 
390  $db = MatrixDAL::getDb();
391 
392  $bind_vars = Array (
393  'assetid' => $this->id,
394  'termid' => $termid,
395  );
396 
397  try {
398  $result = MatrixDAL::executeAssoc('thesaurus', 'getParentRelationsForTerm', $bind_vars);
399  } catch (Exception $e) {
400  throw new Exception('Unable to get parent relations for term due to database error: '. $e->getMessage());
401  }//end
402 
403  return $result;
404 
405  }//end getParentRelationsForTerm()
406 
407 
414  function getAllTerms()
415  {
416  $bind_vars = Array();
417  $bind_vars['assetid'] = $this->id;
418 
419  try {
420  $result = MatrixDAL::executeAll('thesaurus', 'getAllTerms', $bind_vars);
421  } catch (Exception $e) {
422  throw new Exception('Unable to get a list of all thesaurus terms due to database error: '. $e->getMessage());
423  }//end
424 
425  return $result;
426 
427  }//end getAllTerms()
428 
429 
441  function getParentTerms($termid, $relid=NULL)
442  {
443 
444  $limit_relation = TRUE;
445  if (is_null($relid)) $limit_relation = FALSE;
446 
447  $db = MatrixDAL::getDb();
448 
449  $sql = '
450  SELECT
451  linkid, term, termid, relid
452  FROM
453  sq_thes_term term
454  INNER JOIN sq_thes_lnk link ON term.termid = link.major
455  WHERE
456  term.thesid = :thesid_1
457  AND
458  link.thesid = :thesid_2
459  AND
460  minor = :termid
461  ';
462 
463  if ($limit_relation) {
464  $sql .= '
465  AND
466  relid = :relid
467  ';
468  }
469 
470  $sql .= '
471  ORDER BY
472  term
473  ';
474 
475 
476  $query = MatrixDAL::preparePdoQuery($sql);
477  MatrixDAL::bindValueToPdo($query, 'thesid_1', $this->id);
478  MatrixDAL::bindValueToPdo($query, 'thesid_2', $this->id);
479  MatrixDAL::bindValueToPdo($query, 'termid', $termid);
480  if ($limit_relation) {
481  MatrixDAL::bindValueToPdo($query, 'relid', $relid);
482  }//end if
483 
484  try {
485  $result = MatrixDAL::executePdoAll($query);
486  } catch (Exception $e) {
487  throw new Exception('Unable to get thesaurus parent terms due to database error: '. $e->getMessage());
488  }//end
489 
490  return $result;
491 
492  }//end getParentTerms()
493 
494 
503  function getRelatedTerms($relation_id='')
504  {
505  if (empty($relation_id)) return NULL;
506 
507  $db = MatrixDAL::getDb();
508 
509  $sql = '
510  SELECT
511  major.term as major,
512  major.termid as majorid,
513  minor.term as minor,
514  minor.termid as minorid
515  FROM
516  sq_thes_term major
517  INNER JOIN sq_thes_lnk link ON major.termid = link.major
518  INNER JOIN sq_thes_term minor ON minor.termid = link.minor
519  WHERE
520  link.relid = :relid
521  AND
522  major.thesid = :major_thesid
523  AND
524  minor.thesid = :minor_thesid
525  AND
526  link.thesid = :link_thesid
527  ';
528 
529  $query = MatrixDAL::preparePdoQuery($sql);
530  MatrixDAL::bindValueToPdo($query, 'relid', $relation_id);
531  MatrixDAL::bindValueToPdo($query, 'major_thesid', $this->id);
532  MatrixDAL::bindValueToPdo($query, 'minor_thesid', $this->id);
533  MatrixDAL::bindValueToPdo($query, 'link_thesid', $this->id);
534  $result = MatrixDAL::executePdoAll($query);
535 
536  return $result;
537 
538  }//end getRelatedTerms()
539 
540 
551  function _addTerm($term)
552  {
553  if (!isset($term) || $term == '') {
554  return NULL;
555  }
556 
557  // remove leading and trailing spaces
558  $term = trim($term);
559 
560  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
561 
562  $termid = MatrixDAL::executeOne('core', 'seqNextVal', Array('seqName' => 'sq_thes_term_seq'));
563  $bind_vars = Array (
564  'thesid' => $this->id,
565  'term' => $term,
566  'termid' => $termid,
567  );
568 
569  try {
570  $result = MatrixDAL::executeQuery('thesaurus', 'insertTerm', $bind_vars);
571  } catch (Exception $e) {
572  throw new Exception('Unable to insert a thesaurus term due to database error: '. $e->getMessage());
573  }//end
574 
575  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
576  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
577 
578  return $termid;
579 
580  }//end _addTerm()
581 
582 
593  function addTerm($term)
594  {
595  $termid = $this->getTermIdByName($term);
596  if (!is_null($termid)) return $termid;
597 
598  return $this->_addTerm($term);
599 
600  }//end addTerm()
601 
602 
613  function addTermNote($termid, $note_name, $note_value)
614  {
615  // before adding the Term Note we need to check if the note hasnt been added
616  // if the term note is already in the table we will throw an error
617  $db = MatrixDAL::getDb();
618  $sql = 'SELECT a.termid, a.name, a.value, a.thesid, b.term
619  FROM sq_thes_term_note a, sq_thes_term b
620  WHERE a.termid = '.MatrixDAL::quote($termid).' AND
621  a.name = '.MatrixDAL::quote($note_name).' AND
622  a.value = '.MatrixDAL::quote($note_value).' AND
623  a.thesid = '.MatrixDAL::quote($this->id).' AND
624  b.thesid = a.thesid AND b.termid = a.termid
625  ';
626  $result = NULL;
627  try {
628  $query = MatrixDAL::preparePdoQuery($sql);
629  $result = MatrixDAL::executePdoAssoc($query);
630  } catch (Exception $e) {
631  throw new Exception($e->getMessage());
632  }
633 
634  if (!empty($result)) {
635  // throw an error and return TRUE
636  trigger_localised_error('CORE0282', E_USER_WARNING, $note_name.":".$note_value, $result[0]['term']);
637  return TRUE;
638  }
639 
640  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
641 
642 
643  $bind_vars = Array (
644  'thesid' => $this->id,
645  'termid' => $termid,
646  'name' => $note_name,
647  'value' => $note_value,
648  );
649 
650  try {
651  $result = MatrixDAL::executeQuery('thesaurus', 'insertTermNote', $bind_vars);
652  } catch (Exception $e) {
653  throw new Exception('Unable to insert a thesaurus term note due to database error: '. $e->getMessage());
654  }//end
655 
656  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
657  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
658 
659  return TRUE;
660 
661  }//end addTermNote()
662 
663 
672  function getTermNotes($termid)
673  {
674 
675  $db = MatrixDAL::getDb();
676 
677  $bind_vars = Array (
678  'assetid' => $this->id,
679  'termid' => $termid,
680  );
681 
682  try {
683  $result = MatrixDAL::executeAssoc('thesaurus', 'getTermNotes', $bind_vars);
684  } catch (Exception $e) {
685  throw new Exception('Unable to get thesaurus term note due to database error: '. $e->getMessage());
686  }//end
687 
688  return $result;
689 
690  }//end getTermNotes()
691 
692 
701  function deleteTermNotes($termid)
702  {
703  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
704 
705  $db = MatrixDAL::getDb();
706 
707  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
708 
709  $bind_vars = Array (
710  'thesid' => $this->id,
711  'termid' => $termid,
712  );
713 
714  try {
715  $result = MatrixDAL::executeQuery('thesaurus', 'deleteTermNotes', $bind_vars);
716  } catch (Exception $e) {
717  throw new Exception('Unable to delete a thesaurus term note due to database error: '. $e->getMessage());
718  }//end
719 
720 
721  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
722  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
723 
724  return TRUE;
725 
726  }//end deleteTermNotes()
727 
728 
741  function linkTerms($minorid, $majorid, $relid)
742  {
743  $term_linkid = $this->_linkTerms($minorid, $majorid, $relid);
744 
745  if (is_null($term_linkid)) {
746  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
747  return NULL;
748  }
749 
750  if ($this->attr('enforce_reflection')) {
751  $ref_relid = $this->getReflectionForRelation($relid);
752  if ($ref_relid) {
753  $this->_linkTerms($majorid, $minorid, $ref_relid);
754  }
755  }
756 
757  return $term_linkid;
758 
759  }//end linkTerms()
760 
761 
775  function _linkTerms($minorid, $majorid, $relation_id=NULL)
776  {
777  if (is_null($minorid) || is_null($majorid)) {
778  return NULL;
779  }
780 
781  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
782  $db = MatrixDAL::getDb();
783 
784  $bind_vars = Array (
785  'thesid' => $this->id,
786  'major' => $majorid,
787  'minor' => $minorid,
788  'relation' => $relation_id,
789  );
790 
791  try {
792  $result = MatrixDAL::executeOne('thesaurus', 'getLinkId', $bind_vars);
793  } catch (Exception $e) {
794  throw new Exception('Unable to get thesaurus linkid due to database error: '. $e->getMessage());
795  }//end
796 
797 
798  // if the terms are already linked using this relation, just return the ID
799  if (!empty($result)) return $result;
800 
801  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
802 
803  // inserting a term link is the easiest thing ever
804  try {
805  $linkid = MatrixDAL::executeOne('core', 'seqNextVal', Array('seqName' => 'sq_thes_lnk_seq'));
806  $bind_vars['linkid'] = $linkid;
807  $result = MatrixDAL::executeQuery('thesaurus', 'insertLink', $bind_vars);
808  } catch (Exception $e) {
809  throw new Exception('Unable to link a thesaurus term due to database error: '. $e->getMessage());
810  }//end
811 
812 
813  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
814  $major = $this->getAsset($majorid);
815  $em->broadcastEvent($major, 'CreateLink', Array());
816 
817  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
818  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
819  return $linkid;
820 
821  }//end _linkTerms()
822 
823 
835  function updateTerm($termid, $name)
836  {
837 
838  $term = $this->getTermById($termid);
839 
840  // remove leading and trailing spaces
841  $name = trim($name);
842 
843  // if nothing has changed, no need to do any work
844  if ($term['term'] == $name) return TRUE;
845 
846  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
847  $db = MatrixDAL::getDb();
848 
849 
850  // if the new name is being used by another term, do not update
851  // updating the name if it is used by another term is hard as we don't know what to do with the children
852  // do we merge this term's children with the others' or do we only use our children or theirs?
853  if ($this->isTerm($name, $termid)) {
854  trigger_localised_error('CORE0239', E_USER_WARNING);
855  return FALSE;
856  }
857 
858 
859  // if the term has only one parent and the name is not used anywhere else - it is safe to update it
860  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
861  // update the term itself
862 
863  $bind_vars = Array (
864  'assetid' => $this->id,
865  'termid' => $termid,
866  'name' => $name,
867  );
868 
869  try {
870  $result = MatrixDAL::executeQuery('thesaurus', 'updateTerm', $bind_vars);
871  } catch (Exception $e) {
872  throw new Exception('Unable to update a thesaurus term due to database error: '. $e->getMessage());
873  }//end
874 
875 
876  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
877  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
878  return TRUE;
879 
880  }//end updateTerm()
881 
882 
894  function renameRelation($relation_id, $new_name)
895  {
896  if ($new_name == '') $new_name = NULL;
897 
898  // IMPORTANT: Ensure the new name is not already in use
899  $relid = $this->getRelationIdByName($new_name);
900  if (!is_null($relid)) return NULL;
901 
902  if (array_search($new_name, $this->getActiveRelations())!== FALSE) {
903  // can't rename an existing relation
904  $this->_tmp['error'][] = translate('thesaurus_new_relation_in_use');
905  return FALSE;
906  }
907 
908  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
909  $db = MatrixDAL::getDb();
910  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
911 
912  // Update relations
913 
914  $bind_vars = Array (
915  'assetid' => $this->id,
916  'relid' => $relation_id,
917  'relation' => $new_name,
918  );
919 
920  try {
921  $result = MatrixDAL::executeQuery('thesaurus', 'updateRelationName', $bind_vars);
922  } catch (Exception $e) {
923  throw new Exception('Unable to update a thesaurus relation name due to database error: '. $e->getMessage());
924  }//end
925 
926  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
927  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
928 
929  $this->markContentsChanged();
930  return;
931 
932  }//end renameRelation()
933 
934 
945  function getTermById($termid)
946  {
947  $db = MatrixDAL::getDb();
948 
949  $bind_vars = Array (
950  'assetid' => $this->id,
951  'termid' => $termid,
952  );
953 
954  try {
955  $result = MatrixDAL::executeAll('thesaurus', 'getTermById', $bind_vars);
956  if (!empty($result) && isset($result[0])) {
957  $result = $result[0];
958  }
959  } catch (Exception $e) {
960  throw new Exception('Unable to get a thesaurus term by id due to database error: '. $e->getMessage());
961  }//end
962 
963  return $result;
964 
965  }//end getTermById()
966 
967 
978  function getTermLinkById($linkid)
979  {
980  $db = MatrixDAL::getDb();
981 
982  $bind_vars = Array (
983  'assetid' => $this->id,
984  'linkid' => $linkid,
985  );
986 
987  try {
988  $result = MatrixDAL::executeAll('thesaurus', 'getLinkById', $bind_vars);
989  if (!empty($result) && isset($result[0])) {
990  $result = $result[0];
991  }
992  } catch (Exception $e) {
993  throw new Exception('Unable to get a thesaurus term link by id due to database error: '. $e->getMessage());
994  }//end
995 
996  return $result;
997 
998  }//end getTermLinkById()
999 
1000 
1009  function getRelationIdByName($name)
1010  {
1011  $db = MatrixDAL::getDb();
1012 
1013  $sql = '
1014  SELECT
1015  relid
1016  FROM
1017  sq_thes_rel
1018  WHERE
1019  relation '.(is_null($name)?'IS NULL':'= :name').'
1020  AND
1021  thesid = :assetid';
1022 
1023  $query = MatrixDAL::preparePdoQuery($sql);
1024  if (!is_null($name)) {
1025  MatrixDAL::bindValueToPdo($query, 'name', $name);
1026  }
1027  MatrixDAL::bindValueToPdo($query, 'assetid', $this->id);
1028 
1029  try {
1030  $result = MatrixDAL::executePdoOne($query);
1031  } catch (Exception $e) {
1032  throw new Exception('Unable to get thesaurus relation Id by name due to database error: '. $e->getMessage());
1033  }//end
1034 
1035  if (empty($result)) $result = NULL;
1036 
1037  return $result;
1038 
1039  }//end getRelationIdByName()
1040 
1041 
1050  function addRelation($name)
1051  {
1052  if ($name == '') $name = NULL;
1053 
1054  // IMPORTANT: Ensure the new name is not already in use
1055  $relid = $this->getRelationIdByName($name);
1056  if (!is_null($relid)) return $relid;
1057 
1058  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
1059  $db = MatrixDAL::getDb();
1060 
1061  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
1062 
1063  $id = MatrixDAL::executeOne('core', 'seqNextVal', Array('seqName' => 'sq_thes_rel_seq'));
1064  $bind_vars = Array (
1065  'thesid' => $this->id,
1066  'relation' => $name,
1067  'relid' => $id,
1068  );
1069 
1070  try {
1071  $result = MatrixDAL::executeQuery('thesaurus', 'insertRelation', $bind_vars);
1072  } catch (Exception $e) {
1073  throw new Exception('Unable to add a thesaurus relation due to database error: '. $e->getMessage());
1074  }//end
1075 
1076  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
1077  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
1078 
1079  return $id;
1080 
1081  }//end addRelation()
1082 
1083 
1093  function updateLinkRelation($linkid, $relid)
1094  {
1095 
1096  if (!isset($linkid) || !isset($relid)) {
1097  return FALSE;
1098  }
1099 
1100  $link_info = $this->getTermLinkById($linkid);
1101  $major = $link_info['major'];
1102  $minor = $link_info['minor'];
1103  $old_relid = $link_info['relid'];
1104 
1105  $this->deleteTermLink($linkid);
1106  $this->linkTerms($minor, $major, $relid);
1107 
1108  return TRUE;
1109 
1110  }//end updateLinkRelation()
1111 
1112 
1123  function getTermLinkByTermids($termid, $parentid, $relid=NULL)
1124  {
1125 
1126  $db = MatrixDAL::getDb();
1127 
1128  $bind_vars = Array (
1129  'thesid' => $this->id,
1130  'relid' => $relid,
1131  'parentid' => $parentid,
1132  'termid' => $termid,
1133  );
1134 
1135  try {
1136  $result = MatrixDAL::executeAll('thesaurus', 'getTermLinkByTermIds', $bind_vars);
1137  if (!empty($result) && isset($result[0])) {
1138  $result = $result[0];
1139  }
1140  } catch (Exception $e) {
1141  throw new Exception('Unable to get a thesaurus term link by id due to database error: '. $e->getMessage());
1142  }//end
1143 
1144  return $result;
1145 
1146  }//end getTermLinkByTermids()
1147 
1148 
1157  function getTermIdByName($name)
1158  {
1159  $db = MatrixDAL::getDb();
1160 
1161  $name = strtolower($name);
1162 
1163  $sql = '
1164  SELECT
1165  termid
1166  FROM
1167  sq_thes_term
1168  WHERE
1169  lower(term) = :term
1170  AND
1171  thesid = :thesid
1172  ';
1173 
1174  try {
1175  $query = MatrixDAL::preparePdoQuery($sql);
1176  MatrixDAL::bindValueToPdo($query, 'term', $name);
1177  MatrixDAL::bindValueToPdo($query, 'thesid', $this->id);
1178  $result = MatrixDAL::executePdoOne($query);
1179  } catch (Exception $e) {
1180  throw new Exception('Unable to get thesaurus term id by name due to database error: '. $e->getMessage());
1181  }//end
1182 
1183  if (empty($result)) $result = NULL;
1184 
1185  return $result;
1186 
1187  }//end getTermIdByName()
1188 
1189 
1205  function getTermIdsByNames($names)
1206  {
1207  if (empty($names)) return Array();
1208 
1209  $db = MatrixDAL::getDb();
1210 
1211  $formatted_names = Array();
1212  foreach ($names as $name) {
1213  $formatted_names[] = strtolower($name);
1214  }
1215 
1216  $bind_vars = Array (
1217  'names' => $formatted_names,
1218  'theid' => $this->id,
1219  );
1220 
1221  try {
1222  $result = MatrixDAL::executeAssoc('thesaurus', 'getTermIdByNames', $bind_vars);
1223  } catch (Exception $e) {
1224  throw new Exception('Unable to get thesaurus term ids by names due to database error: '. $e->getMessage());
1225  }//end
1226 
1227  return $result;
1228 
1229  }//end getTermIdsByNames()
1230 
1231 
1241  function isTerm($term=NULL, $termid=0)
1242  {
1243  if (is_null($term)) return FALSE;
1244 
1245  $db = MAtrixDAL::getDb();
1246 
1247  $sql = '
1248  SELECT
1249  termid
1250  FROM
1251  sq_thes_term
1252  WHERE
1253  lower(term) = :term
1254  AND
1255  thesid = :thesid
1256  ';
1257  if ($termid != 0) {
1258  $sql .= ' AND termid <> :termid';
1259  }
1260 
1261  $bind_vars = Array (
1262  'term' => strtolower($term),
1263  'thesid' => $this->id,
1264  );
1265 
1266  if ($termid != 0) {
1267  $bind_vars['termid'] = $termid;
1268  }
1269 
1270  try {
1271  $query = MatrixDAL::preparePdoQuery($sql);
1272  MatrixDAL::bindValueToPdo($query, 'term', strtolower($term));
1273  MatrixDAL::bindValueToPdo($query, 'thesid', $this->id);
1274  if ($termid != 0) {
1275  MatrixDAL::bindValueToPdo($query, 'termid', $termid);
1276  }
1277  $result = MatrixDAL::executePdoOne($query);
1278  } catch (Exception $e) {
1279  throw new Exception('Unable to get thesaurus term id due to database error: '.$e->getMessage());
1280  }//end
1281 
1282  return !empty($result);
1283 
1284  }//end isTerm()
1285 
1286 
1294  {
1295  $db = MatrixDAL::getDb();
1296 
1297  $bind_vars = Array (
1298  'assetid' => $this->id,
1299  );
1300 
1301  try {
1302  $results = MatrixDAL::executeAssoc('thesaurus', 'getActiveRelations', $bind_vars);
1303  } catch (Exception $e) {
1304  throw new Exception('Unable to get thesaurus active relations due to database error: '. $e->getMessage());
1305  }//end
1306 
1307  if(!empty($results)) {
1308  $new_res = Array();
1309  foreach ($results as $result) {
1310  $new_res[$result['relid']] = $result['relation'];
1311  }//end foreach
1312  $results = $new_res;
1313  }//end if
1314 
1315  return $results;
1316 
1317  }//end getActiveRelations()
1318 
1319 
1326  function erase()
1327  {
1328 
1329  if (!$this->_deleteStoredContents()) return FALSE;
1330 
1331  $this->setAttrValue('relations', Array());
1332  $this->setAttrValue('reflections', Array());
1333 
1334  $this->markContentsChanged();
1335 
1336  return TRUE;
1337 
1338  }//end erase()
1339 
1340 
1348  {
1349 
1350  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
1351  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
1352 
1353  $db = MatrixDAL::getDb();
1354 
1355  $bind_vars = Array (
1356  'assetid' => $this->id,
1357  );
1358 
1359  try {
1360  $result = MatrixDAL::executeQuery('thesaurus', 'deleteAllTerms', $bind_vars);
1361  } catch (Exception $e) {
1362  throw new Exception('Unable to delete all thesaurus terms due to database error: '. $e->getMessage());
1363  }//end
1364 
1365  try {
1366  $result = MatrixDAL::executeQuery('thesaurus', 'deleteAllLinks', $bind_vars);
1367  } catch (Exception $e) {
1368  throw new Exception('Unable to delete all thesaurus links due to database error: '. $e->getMessage());
1369  }//end
1370 
1371  try {
1372  $result = MatrixDAL::executeQuery('thesaurus', 'deleteAllRelations', $bind_vars);
1373  } catch (Exception $e) {
1374  throw new Exception('Unable to delete all thesaurus relations due to database error: '. $e->getMessage());
1375  }//end
1376 
1377  $bind_vars['assetid'] = $this->id.':%';
1378 
1379  try {
1380  $result = MatrixDAL::executeQuery('thesaurus', 'deleteAllShadowLinks', $bind_vars);
1381  } catch (Exception $e) {
1382  throw new Exception('Unable to delete all shadow thesaurus links due to database error: '. $e->getMessage());
1383  }//end
1384 
1385  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
1386  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
1387 
1388  return TRUE;
1389 
1390  }//end _deleteStoredContents()
1391 
1392 
1399  function getXmlFilePath()
1400  {
1401  return $this->data_path.'/content.xml';
1402 
1403  }//end getXmlFilePath()
1404 
1405 
1416  function _getName($short_name=FALSE, $contextid=NULL)
1417  {
1418  // No context specified, using the current context
1419  if ($contextid === NULL) {
1420  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
1421  }//end if
1422 
1423  // Obtain the attribute value for Name from the specified Context
1424  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
1425  if (empty($values) === TRUE) {
1426  return parent::_getName($short_name, $contextid);
1427  } else {
1428  return $values[$this->id];
1429  }
1430 
1431  }//end _getName()
1432 
1433 
1442  function getAbsoluteParentsInRelation($relation_id=NULL)
1443  {
1444 
1445  $db = MatrixDAL::getDb();
1446 
1447  if (!isset($relation_id)) return Array();
1448 
1449  $sql ='
1450  SELECT
1451  termid,
1452  term
1453  FROM
1454  sq_thes_term
1455  WHERE
1456  thesid = :thesid_1
1457  AND
1458  termid IN (
1459  SELECT DISTINCT
1460  major
1461  FROM
1462  sq_thes_lnk
1463  WHERE
1464  thesid = :thesid_2
1465  AND
1466  relid = :relid_1
1467  AND
1468  major NOT IN (
1469  SELECT DISTINCT
1470  minor
1471  FROM
1472  sq_thes_lnk
1473  WHERE
1474  thesid = :thesid_3
1475  AND
1476  relid = :relid_2
1477  )
1478  )
1479  ORDER BY
1480  term
1481  ';
1482 
1483  $query = MatrixDAL::preparePdoQuery($sql);
1484  MatrixDAL::bindValueToPdo($query, 'thesid_1', $this->id);
1485  MatrixDAL::bindValueToPdo($query, 'thesid_2', $this->id);
1486  MatrixDAL::bindValueToPdo($query, 'thesid_3', $this->id);
1487  MatrixDAL::bindValueToPdo($query, 'relid_1', $relation_id);
1488  MatrixDAL::bindValueToPdo($query, 'relid_2', $relation_id);
1489 
1490  try {
1491  $result = MatrixDAL::executePdoAll($query);
1492  } catch (Exception $e) {
1493  throw new Exception('Unable to get absolute parent om relation due to database error: '. $e->getMessage());
1494  }//end
1495 
1496  return $result;
1497 
1498  }//end getAbsoluteParentsInRelation()
1499 
1500 
1509  function getLineagesForTerm($termid)
1510  {
1511  $result = Array();
1512 
1513  // this function is empty because lineages are hard to define and lookup for the general thesaurus
1514  // the only lineage that makes sense is the one using the hierarchy relations
1515 
1516  return $result;
1517 
1518  }//end getLineagesForTerm()
1519 
1520 
1521 //-- BRIDGE FUNCTIONS --//
1522 
1523 
1538  function getAsset($shadowid, $type_code='', $mute_errors=FALSE)
1539  {
1540  $asset = NULL;
1541 
1542  $id_parts = explode(':', $shadowid);
1543  if (!empty($id_parts[1])) {
1544  $id = $id_parts[1];
1545 
1546  $term = $this->getTermById($id);
1547  if (!empty($term)) {
1548  $GLOBALS['SQ_SYSTEM']->am->includeAsset('thesaurus_term');
1549  $asset = new Thesaurus_Term($shadowid);
1550  }
1551  }
1552 
1553  return $asset;
1554 
1555  }//end getAsset()
1556 
1557 
1564  function getAssetMapLinks()
1565  {
1566  return $this->_getTopTermsAssetMapLinks();
1567 
1568  }//end getAssetMapLinks()
1569 
1570 
1579  function _getShadowAssetMapLinks($parent_term_id=NULL)
1580  {
1581  $bridge_id = $this->id;
1582 
1583  $majorid = $bridge_id;
1584  if (!is_null($parent_term_id)) {
1585  $majorid .= ':'.$parent_term_id;
1586  }
1587 
1588 
1589  $active_relations = $this->getActiveRelations();
1590  $linked_terms = $this->getChildTerms($parent_term_id);
1591 
1592  $links = Array();
1593 
1594  foreach ($linked_terms as $term_link) {
1595  $link = Array();
1596 
1597  // mould it all to the asset map's liking
1598  $link['url'] = '';
1599  $link['path'] = '';
1600  $link['num_kids'] = -1;
1601  $link['accessible'] = 1;
1602 
1603  $link['majorid'] = $majorid;
1604  $link['minorid'] = $bridge_id.':'.$term_link['termid'];
1605  $link['assetid'] = $link['minorid'];
1606  $link['type_code'] = 'thesaurus_term';
1607  $link['minor_type_code'] = $link['type_code'];
1608 
1609  $link['linkid'] = $bridge_id.':'.$term_link['linkid'];
1610  // make name and short name the same
1611  $rel_name = array_get_index($active_relations, $term_link['relid']);
1612  $name_prefix = (empty($rel_name) ? '' : '('.$rel_name.') ');
1613  $link['name'] = $name_prefix.$term_link['term'];
1614  $link['short_name'] = $link['name'];
1615 
1616  $link['status'] = $this->status;
1617  $link['link_type'] = SQ_LINK_TYPE_1;
1618  $link['sort_order'] = 1;
1619  $link['is_dependant'] = 0;
1620 
1621  $links[] = $link;
1622  }
1623 
1624  return $links;
1625 
1626  }//end _getShadowAssetMapLinks()
1627 
1628 
1636  {
1637  $terms = Array();
1638 
1639  $linkid_prefix = '';
1640  if ($this->attr('hierarchy_mode')) {
1641  $linkid_prefix = ':hierarchy';
1642  $terms = $this->getAbsoluteParentsInRelation($this->attr('hierarchy_rel'));
1643  } else {
1644  $linkid_prefix = ':atoz';
1645  $terms = $this->getAllTerms();
1646  }
1647 
1648  $bridge_id = $this->id;
1649 
1650 
1651  $links = Array();
1652 
1653  foreach ($terms as $one_term) {
1654  $link = Array();
1655 
1656  // mould it all to the asset map's liking
1657  $link['url'] = '';
1658  $link['path'] = '';
1659  $link['num_kids'] = -1;
1660  $link['accessible'] = 1;
1661 
1662  $link['majorid'] = $bridge_id;
1663  $link['minorid'] = $bridge_id.':'.$one_term['termid'];
1664  $link['assetid'] = $link['minorid'];
1665  $link['type_code'] = 'thesaurus_term';
1666  $link['minor_type_code'] = $link['type_code'];
1667 
1668  $link['linkid'] = $bridge_id.$linkid_prefix.':'.$one_term['termid'];
1669 
1670  // make name and short name the same
1671  $link['name'] = $one_term['term'];
1672  $link['short_name'] = $link['name'];
1673 
1674  $link['status'] = $this->status;
1675  $link['link_type'] = SQ_LINK_TYPE_2;
1676  $link['sort_order'] = 1;
1677  $link['is_dependant'] = 0;
1678 
1679  $links[] = $link;
1680 
1681  }//end foreach
1682 
1683  return $links;
1684 
1685  }//end _getTopTermsAssetMapLinks()
1686 
1687 
1702  function getAssetInfo($assetids, $type_code=Array(), $strict_type_code=TRUE, $field='')
1703  {
1704  $info_array = Array();
1705 
1706  foreach ($assetids as $id) {
1707  $id_parts = explode(':', $id);
1708  if (empty($id_parts[1])) continue;
1709 
1710  $term = $this->getTermById($id_parts[1]);
1711  if (empty($term)) continue;
1712 
1713  $info_array[$id]['assetid'] = $id;
1714  $info_array[$id]['name'] = $term['term'];
1715  $info_array[$id]['short_name'] = $term['term'];
1716  $info_array[$id]['version'] = $this->version;
1717  $info_array[$id]['status'] = $this->status;
1718  $info_array[$id]['type_code'] = 'thesaurus_term';
1719  $info_array[$id]['num_kids'] = $this->countChildTerms($term['termid']);
1720  $info_array[$id]['accessible'] = 1;
1721  $info_array[$id]['url'] = '';
1722  $info_array[$id]['web_path'] = '';
1723 
1724  if (!empty($field)) {
1725  $info_array[$id] = array_get_index($info_array[$id], $field);
1726  }
1727 
1728  }
1729 
1730  return $info_array;
1731 
1732  }//end getAssetInfo()
1733 
1734 
1743  function getAssetMapAssetInfo($assetid)
1744  {
1745  $asset_info = Array();
1746  $asset_info['assetid'] = $assetid;
1747 
1748  $id_parts = explode(':', $assetid);
1749  if (empty($id_parts[1])) return $asset_info;
1750 
1751  $term = $this->getTermById($id_parts[1]);
1752  if (empty($term)) return $asset_info;
1753 
1754  $asset_info['name'] = $term['term'];
1755  $asset_info['short_name'] = $term['term'];
1756  $asset_info['version'] = $this->version;
1757  $asset_info['status'] = $this->status;
1758  $asset_info['type_code'] = 'thesaurus_term';
1759  $asset_info['num_kids'] = $this->countChildTerms($term['termid']);
1760  $asset_info['accessible'] = 1;
1761  $asset_info['url'] = '';
1762  $asset_info['web_path'] = '';
1763 
1764  return $asset_info;
1765 
1766  }//end getAssetMapAssetInfo()
1767 
1768 
1789  function getLinks($assetid, $link_types, $type_code='', $strict_type_code=TRUE, $side_of_link='major', $sort_by=NULL)
1790  {
1791  return Array();
1792 
1793  }//end getLinks()
1794 
1795 
1817  function getLink($assetid, $link_type=NULL, $type_code='', $strict_type_code=TRUE, $value=NULL, $side_of_link='major', $exclusive=NULL)
1818  {
1819  return Array();
1820 
1821  }//end getLink()
1822 
1823 
1844  function getLinkByAsset($assetid, $other_assetid, $link_types=NULL, $value=NULL, $side_of_link='major', $force_array=FALSE, $dependant=NULL, $exclusive=NULL)
1845  {
1846  assert_valid_assetid($assetid);
1847  assert_valid_assetid($other_assetid);
1848  assert_false($side_of_link != 'major' && $side_of_link != 'minor', 'Unknown Side of Link "'.$side_of_link.'"');
1849 
1850  if ($side_of_link == 'major') {
1851  $major_assetid = $assetid;
1852  $minor_assetid = $other_assetid;
1853  } else {
1854  $major_assetid = $other_assetid;
1855  $minor_assetid = $assetid;
1856  }
1857 
1858  $major_parts = explode(':', $major_assetid);
1859  $majorid = array_get_index($major_parts, 1, NULL);
1860 
1861  $minor_parts = explode(':', $minor_assetid);
1862  $minorid = array_get_index($minor_parts, 1, NULL);
1863  if (empty($minorid)) return Array();
1864 
1865  $link_info = $this->getTermLinkByTermids($minorid, $majorid, $value);
1866 
1867  if (empty($link_info)) return Array();
1868 
1869  // construct template
1870  $link = Array(
1871  'majorid' => $major_assetid,
1872  'minorid' => $minor_assetid,
1873  'link_type' => SQ_LINK_TYPE_2,
1874  'major_type_code' => '',
1875  'minor_type_code' => 'thesaurus_term',
1876  'value' => $value,
1877  'linkid' => $this->id.':'.$link_info['linkid'],
1878  'is_dependant' => 0,
1879  'is_exclusive' => 0,
1880  'locked' => 0,
1881  );
1882 
1883  return $link;
1884 
1885  }//end getLinkByAsset()
1886 
1887 
1899  function getLinkById($linkid, $assetid=0, $side_of_link='major')
1900  {
1901  $link_parts = explode(':', $linkid);
1902 
1903  $majorid = $this->id;
1904 
1905  if (isset($link_parts[2])) {
1906  // the major is the thesaurus
1907  $term_id = array_get_index($link_parts, 2);
1908  $minorid = $term_id;
1909  $value = NULL;
1910  $major_type_code = 'thesaurus';
1911 
1912  } else {
1913  $term_linkid = array_get_index($link_parts, 1);
1914  if (is_null($term_linkid)) {
1915  trigger_error('Invalid Link ID', E_USER_ERROR);
1916  }
1917  $link_info = $this->getTermLinkById($term_linkid);
1918  $minorid = array_get_index($link_info, 'minorid');
1919  $majorid .= ':'.array_get_index($link_info, 'major');
1920  $value = array_get_index($link_info, 'relation');
1921  $major_type_code = 'thesaurus_term';
1922  }
1923 
1924  // construct template
1925  $link = Array(
1926  'majorid' => $majorid,
1927  'minorid' => $this->id.':'.$minorid,
1928  'link_type' => SQ_LINK_TYPE_2,
1929  'major_type_code' => $major_type_code,
1930  'minor_type_code' => 'thesaurus_term',
1931  'value' => $value,
1932  'linkid' => $linkid,
1933  'is_dependant' => 0,
1934  'is_exclusive' => 0,
1935  'sort_order' => NULL,
1936  'locked' => 0,
1937  );
1938 
1939  return $link;
1940 
1941  }//end getLinkById()
1942 
1943 
1957  function _getAllowedLinks()
1958  {
1959  return Array(
1960  SQ_LINK_TYPE_1 => Array(
1961  'thesaurus_term' => Array(
1962  'card' => 'M',
1963  'exclusive' => FALSE,
1964  ),
1965  ),
1966  SQ_LINK_TYPE_2 => Array(
1967  'thesaurus_term' => Array(
1968  'card' => 'M',
1969  'exclusive' => FALSE,
1970  ),
1971  ),
1972  SQ_LINK_TYPE_3 => Array(),
1973  );
1974 
1975  }//end _getAllowedLinks()
1976 
1977 
2000  function getChildren($assetid, $type_code='thesaurus_term', $strict_type_code=TRUE, $dependant=TRUE, $sort_by=NULL)
2001  {
2002  $result = Array();
2003  $id_parts = explode(':', $assetid);
2004  if ($type_code == 'thesaurus_term' || (is_array($type_code) && in_array('thesaurus_term', $type_code))) {
2005  // will return all terms, regardless of what param you pass in
2006  $terms = Array();
2007  if (isset($id_parts[1]) && $id_parts[0] == $this->id) {
2008  // if we are only looking for children terms underneath a term
2009  $terms = $this->getChildTerms($id_parts[1]);
2010  } else {
2011  $terms = $this->getAllTerms();
2012  }
2013 
2014  foreach ($terms as $term_info) {
2015  $result[$this->id.':'.$term_info['termid']] = Array(Array('type_code' => 'thesaurus_term'));
2016  }
2017  }
2018 
2019  return $result;
2020 
2021  }//end getChildren()
2022 
2023 
2038  function getParents($assetid, $type_code='', $strict_type_code=TRUE)
2039  {
2040  if (strpos($assetid, ':') !== FALSE) {
2041  list($realid, $shadowid) = explode(':', $assetid);
2042  $parents = Array($realid => $this->type());
2043  $parents += $GLOBALS['SQ_SYSTEM']->am->getParents($realid, $type_code, $strict_type_code);
2044  return $parents;
2045  }
2046 
2047  return Array();
2048 
2049  }//end getParents()
2050 
2051 
2070  function createAssetLink(&$major, &$minor, $link_type, $value='', $sort_order=NULL, $dependant='0', $exclusive='0', $moving=FALSE)
2071  {
2072  if ($major->id == $this->id) {
2073  // cannot link a term to a thesaurus
2074  // terms can only be linked to themselves
2075  // this term already exists; its appearance as the top term is controlled by the 'hierarchy' set of options
2076  trigger_error('Cannot link term to thesaurus. Term already exists in thesaurus. Its appearance as a top-level term is governed by the HIERARCHY options of the thesaurus and cannot be forced.', E_USER_ERROR);
2077  return 0;
2078  }
2079 
2080  $major_parts = explode(':',$major->id);
2081  $majorid = array_get_index($major_parts, 1);
2082 
2083  $minor_parts = explode(':',$minor->id);
2084  $minorid = array_get_index($minor_parts, 1);
2085  if (empty($minorid)) return NULL;
2086 
2087  $relid = empty($value) ? NULL : $value;
2088  $minor_term = NULL;
2089  $major_term = NULL;
2090 
2091  // can only link terms which belong to this thesaurus
2092  if (($minor_parts[0] != $this->id) || ($major_parts[0] != $this->id)) {
2093  return 0;
2094  }
2095 
2096  $minor_term = $minor->name;
2097 
2098  if (isset($major_parts[1])) {
2099  $major_term = $major->name;
2100  }
2101 
2102  if ($moving) {
2103  $link_info = $this->getTermLinkByTermids($minorid, $majorid, $relid);
2104  if (!is_null($link_info)) {
2105  trigger_localised_error('CORE0240');
2106  return FALSE;
2107  }
2108  } else {
2109  // if it's a new link, we use the default relation
2110  $relid = $this->attr('default_rel');
2111  }
2112 
2113  $term_linkid = $this->linkTerms($minorid, $majorid, $relid);
2114 
2115  if (is_null($term_linkid)) return FALSE;
2116 
2117  return $this->id.':'.$term_linkid;
2118 
2119  }//end createAssetLink()
2120 
2121 
2142  function getPermission($assetid, $permission, $granted=NULL, $and_greater=TRUE, $expand_groups=FALSE, $all_info=FALSE)
2143  {
2144  // All thesaurus terms inherit permissions of thesaurus.
2145  return $GLOBALS['SQ_SYSTEM']->am->getPermission($this->id, $permission, $granted, $and_greater, $expand_groups, $all_info);
2146 
2147  }//end getPermission()
2148 
2149 
2160  {
2161  if ($this->attr('contents_changed')) return;
2162 
2163  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
2164  $this->setAttrValue('contents_changed',TRUE);
2165  $this->saveAttributes();
2166  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
2167 
2168  return;
2169 
2170  }//end markContentsChanged()
2171 
2172 
2194  function countLinks($assetid, $side_of_link='major', $link_types=0, $type_code='', $strict_type_code=TRUE, $ignore_linkid=0)
2195  {
2196 
2197  $id_parts = explode(':', $assetid);
2198 
2199  $termid = array_get_index($id_parts, 1);
2200  if (is_null($termid)) return -1;
2201 
2202  $relations = Array();
2203 
2204  if ($side_of_link == 'major') {
2205  $keyword = 'Child';
2206  } else {
2207  $keyword = 'Parent';
2208  }
2209  $get_term_function = 'count'.$keyword.'Terms';
2210  return $this->$get_term_function($termid);
2211 
2212  }//end countLinks()
2213 
2214 
2224  function deleteAssetLink($linkid, $moving=FALSE)
2225  {
2226  $link_parts = explode(':', $linkid);
2227 
2228  if (isset($link_parts[1])) {
2229  if (isset($link_parts[2])) {
2230  if ($link_parts[1] == 'atoz') {
2231  // delete term (in A-to-Z mode)
2232  $this->deleteTerm($link_parts[2]);
2233  }
2234  } else {
2235  $this->deleteTermLink($link_parts[1]);
2236  }
2237  }
2238  return TRUE;
2239 
2240  }//end deleteAssetLink()
2241 
2242 
2251  function deleteTermLink($linkid)
2252  {
2253  if (empty($linkid)) return FALSE;
2254 
2255  // reflection support
2256  $link_info = $this->getTermLinkById($linkid);
2257  $major = $link_info['major'];
2258  $minor = $link_info['minor'];
2259  $relid = $link_info['relid'];
2260 
2261  $this->_deleteTermLink($linkid);
2262 
2263  if ($this->attr('enforce_reflection')) {
2264  $ref_relid = $this->getReflectionForRelation($relid);
2265  if (!is_null($ref_relid)) {
2266  $reflect_link = $this->getTermLinkByTermids($major, $minor, $ref_relid);
2267  if (!empty($reflect_link)) {
2268  $this->_deleteTermLink($reflect_link['linkid']);
2269  }
2270  }
2271  }
2272  return TRUE;
2273 
2274  }//end deleteTermLink()
2275 
2276 
2285  function _deleteTermLink($linkid)
2286  {
2287  if (empty($linkid)) return FALSE;
2288 
2289  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
2290  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
2291 
2292  try {
2293  $bind_vars = Array (
2294  'linkid' => $linkid,
2295  'assetid' => $this->id,
2296  );
2297  $result = MatrixDAL::executeQuery('thesaurus', 'deleteLink', $bind_vars);
2298  } catch (Exception $e) {
2299  throw new Exception('Unable to delete thesaurus term link due to the following database error:'.$e->getMessage());
2300  }//end try catch
2301 
2302  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
2303  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
2304 
2305  return TRUE;
2306 
2307  }//end _deleteTermLink()
2308 
2309 
2318  function deleteTerm($termid)
2319  {
2320  if (empty($termid)) return FALSE;
2321 
2322  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
2323  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
2324 
2325  // delete the term
2326  try {
2327  $bind_vars = Array (
2328  'assetid' => $this->id,
2329  'termid' => $termid,
2330  );
2331  $result = MatrixDAL::executeQuery('thesaurus', 'deleteTerm', $bind_vars);
2332  } catch (Exception $e) {
2333  throw new Exception('Unable to delete thesaurus term due to the following database error:'.$e->getMessage());
2334  }//end try catch
2335 
2336  try {
2337  $bind_vars = Array (
2338  'termid' => $termid,
2339  'assetid' => $this->id,
2340  );
2341  $result = MatrixDAL::executeQuery('thesaurus', 'deleteThesaurusLinksContainingTerm', $bind_vars);
2342  } catch (Exception $e) {
2343  throw new Exception('Unable to delete thesaurus links containing term due to the following database error:'.$e->getMessage());
2344  }//end try catch
2345 
2346  try {
2347  $bind_vars = Array (
2348  'assetid' => $this->id.':'.$termid,
2349  );
2350  $result = MatrixDAL::executeQuery('thesaurus', 'deleteAllShadowLinks', $bind_vars);
2351  } catch (Exception $e) {
2352  throw new Exception('Unable to delete shadow links due to the following database error:'.$e->getMessage());
2353  }//end try catch
2354 
2355  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
2356  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
2357 
2358  return TRUE;
2359 
2360  }//end deleteTerm()
2361 
2362 
2371  function deleteRelation($relid)
2372  {
2373  if (empty($relid)) return FALSE;
2374 
2375  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
2376  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
2377 
2378  // delete the term
2379  try {
2380  $bind_vars = Array (
2381  'relation' => $relid,
2382  'assetid' => $this->id,
2383  );
2384  $result = MatrixDAL::executeQuery('thesaurus', 'deleteRelation', $bind_vars);
2385  } catch (Exception $e) {
2386  throw new Exception('Unable to delete thesaurus terms relation due to the following database error:'.$e->getMessage());
2387  }//end try catch
2388 
2389  try {
2390  $bind_vars = Array (
2391  'relation' => $relid,
2392  'assetid' => $this->id,
2393  );
2394  $result = MatrixDAL::executeQuery('thesaurus', 'deleteLinksContainingRelation', $bind_vars);
2395  } catch (Exception $e) {
2396  throw new Exception('Unable to delete links containing relation due to the following database error:'.$e->getMessage());
2397  }//end try catch
2398 
2399  // cleanup the reflections
2400  // NOTE: In the future it might be necessary to wipe the reflective relation as well - Reflection Integrity
2401  $this->deleteReflection($relid);
2402 
2403  // make sure any settings we have do not use this relation id
2404  $synonym_rel = $this->attr('synonym_rel');
2405  if ($synonym_rel == $relid) {
2406  $this->setAttrValue('synonym_rel', NULL);
2407  }
2408 
2409  $abbreviation_rel = $this->attr('abbreviation_rel');
2410  if ($abbreviation_rel == $relid) {
2411  $this->setAttrValue('abbreviation_rel', NULL);
2412  }
2413 
2414  $default_rel = $this->attr('default_rel');
2415  if ($default_rel == $relid) {
2416  $this->setAttrValue('default_rel', NULL);
2417  }
2418 
2419 
2420  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
2421  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
2422 
2423 
2424  return TRUE;
2425 
2426  }//end deleteRelation()
2427 
2428 
2440  function moveLink($linkid, $to_parentid, $link_type, $to_parent_pos)
2441  {
2442 
2443  }//end moveLink()
2444 
2445 
2460  function updateLink($linkid, $link_type=NULL, $value=NULL, $new_sort_order=NULL)
2461  {
2462  $linkid_parts = explode(':', $linkid);
2463  if (!isset($linkid_parts[1]) || ($linkid_parts[0] != $this->id)) {
2464  return FALSE;
2465  }
2466 
2467  return TRUE;
2468 
2469  }//end updateLink()
2470 
2471 
2481  function assetExists($assetids)
2482  {
2483 
2484  $shadows = Array();
2485  $shadow_asset_ids = Array();
2486 
2487  // number of assetids
2488  $count = count($assetids);
2489 
2490  $is_array = is_array($assetids);
2491  $is_empty = empty($assetids);
2492 
2493  // assert assetid format
2494  if ($is_array && !$is_empty) {
2495  foreach ($assetids as $key => $value) {
2496  assert_valid_assetid($value);
2497  }
2498  } else if (!$is_empty) {
2499  assert_valid_assetid($assetids);
2500  }
2501 
2502  $db = MatrixDAL::getDb();
2503  $sql = 'SELECT termid, thesid
2504  FROM '.SQ_TABLE_RUNNING_PREFIX.'thes_term ';
2505 
2506  // single asset id
2507  if (!$is_array) {
2508  if ($is_empty) return FALSE;
2509  $id_parts = explode(':', $assetids);
2510 
2511  $where = 'termid = :termid AND thesid = :thesid';
2512  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where);
2513 
2514  $query = MatrixDAL::preparePdoQuery($sql.$where);
2515  MatrixDAL::bindValueToPdo($query, 'thesid', $id_parts[0]);
2516  MatrixDAL::bindValueToPdo($query, 'termid', $id_parts[1]);
2517  $db_assetids = MatrixDAL::executePdoOne($query);
2518 
2519  return ($db_assetids == $assetids);
2520  }
2521 
2522  // array of asset ids
2523  $existing_asset_ids = Array();
2524  if (!$is_empty) {
2525  $in = 'termid IN (';
2526  $thesid_array = Array();
2527  for ($i=0; $i<count($assetids); $i++) {
2528  $id_parts = explode(':', $assetids[$i]);
2529  // build an array of thesids for each termid
2530  $thesid_array[$id_parts[1]] = $id_parts[0];
2531  $in .= MatrixDAL::quote($id_parts[1]).(($i == count($assetids) - 1) ? ')' : ',');
2532  }
2533 
2534  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($in);
2535  $db_assetids = MatrixDAL::executeSqlAll($sql.$where);
2536 
2537  foreach ($db_assetids as $db_id) {
2538  if ($db_id['thesid'] == $thesid_array[$db_id['termid']]) {
2539  $existing_asset_ids[] = $db_id['thesid'].':'.$db_id['termid'];
2540  }
2541  }
2542  }
2543 
2544  return $existing_asset_ids;
2545 
2546  }//end assetExists()
2547 
2548 
2555  function getReflections()
2556  {
2557  return $this->attr('reflections');
2558 
2559  }//end getReflections()
2560 
2561 
2570  function getReflectionForRelation($start)
2571  {
2572  $reflections = $this->getReflections();
2573  return array_get_index($reflections, $start);
2574 
2575  }//end getReflectionForRelation()
2576 
2577 
2587  function addReflection($start, $end)
2588  {
2589  $reflections = $this->getReflections();
2590 
2591  if (isset($relfections[$start]) || isset($reflections[$end])) {
2592  return FALSE;
2593  }
2594 
2595  $reflections[$start] = $end;
2596  $reflections[$end] = $start;
2597 
2598  $this->setReflections($reflections);
2599 
2600  }//end addReflection()
2601 
2602 
2611  function deleteReflection($start)
2612  {
2613  $reflections = $this->getReflections();
2614 
2615  $end = array_get_index($reflections, $start);
2616  if (is_null($end)) return FALSE;
2617 
2618  unset($reflections[$start]);
2619  unset($reflections[$end]);
2620 
2621  $this->setReflections($reflections);
2622 
2623  }//end deleteReflection()
2624 
2625 
2634  function setReflections($ref_array)
2635  {
2636  $this->setAttrValue('reflections', $ref_array);
2637  return TRUE;
2638 
2639  }//end setReflections()
2640 
2641 
2650  function canClone()
2651  {
2652  return FALSE;
2653 
2654  }//end canClone()
2655 
2656 
2667  function getLineageFromURL($assetid, $protocol, $url)
2668  {
2669  return Array();
2670 
2671  }//end getLineageFromURL()
2672 
2673 
2674 }//end class
2675 ?>