Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
thesaurus_term.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
20 
31 class Thesaurus_Term extends Asset
32 {
33 
37  var $_thesaurus;
38 
42  var $_termid;
43 
44 
45  function Thesaurus_Term($asset_id=0)
46  {
47  if (empty($asset_id)) {
48  $this->_loadVars();
49  return;
50  }
51  if (strpos($asset_id, ':') === FALSE) return;
52 
53  list($thesaurus_id, $termid) = explode(':', $asset_id);
54 
55  $this->_thesaurus = $GLOBALS['SQ_SYSTEM']->am->getAsset($thesaurus_id);
56 
57  $term = $this->_thesaurus->getTermById($termid);
58  if (is_null($term)) return;
59 
60  $this->id = $asset_id;
61  $this->_termid = $termid;
62  $this->_loadVars();
63 
64  $this->name = $term['term'];
65  $this->short_name = $this->name;
66  $this->status = $this->_thesaurus->status;
67 
68  $this->setAttrValue('name', $term['term']);
69 
70  }//end constructor
71 
72 
90  function create(&$link)
91  {
92  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
93  $this->name = $this->attr('name');
94  if (empty($this->name)) return FALSE;
95 
96  $majorid = NULL;
97 
98  if ($link['asset'] instanceof Thesaurus) {
99  $this->_thesaurus =& $link['asset'];
100  } else if ($link['asset'] instanceof Thesaurus_Term) {
101  $this->_thesaurus =& $link['asset']->_thesaurus;
102  $majorid = $link['asset']->_termid;
103  } else {
104  $type = method_exists($link['asset'], 'type') ? $link['asset']->type() : '';
105  trigger_localised_error('CORE0236', E_USER_WARNING, $type);
106  return FALSE;
107  }
108 
109  // see if a term with this name already exists under this parent with this particular relation
110  $term = $this->attr('name');
111  $relation = isset($this->_tmp['relation']) ? $this->_tmp['relation'] : $link['value'];
112 
113  $relation = trim($relation);
114 
115  $created = FALSE;
116 
117  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
118  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
119 
120  // check if this term already exists, if it does, then we just link
121  $termid = $this->_thesaurus->getTermIdByName($term);
122  if (is_null($termid)) {
123  $termid = $this->_thesaurus->_addTerm($term);
124  if (is_null($termid)) {
125  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
126  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
127  trigger_error('Unknown error. Cannot add.');
128  return FALSE;
129  }
130  $created = TRUE;
131  }
132 
133  $this->id = $this->_thesaurus->id.':'.$termid;
134  $this->_termid = $termid;
135 
136  // if we're adding a top level term it's a stray term - not linked to anything
137  if (is_null($majorid)) {
138  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
139  if ($created) {
140  $em->broadcastEvent($this, 'AssetCreate', Array('name','assetid'));
141  }
142  $em->broadcastEvent($this->_thesaurus, 'CreateLink', Array());
143  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
144  return $this->_thesaurus->id.':atoz:'.$termid;
145  }
146 
147  $existing_link = $this->_thesaurus->getTermLinkByTermids($termid, $majorid, $relation);
148  if (!empty($existing_link)) {
149  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
150  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
151  return $this->_thesaurus->id.':'.$existing_link['linkid'];
152  }
153 
154  $linkid = $this->_thesaurus->linkTerms($termid, $majorid, $relation);
155  if (is_null($linkid)) {
156  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
157  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
158  trigger_localised_error('CORE0237', E_USER_WARNING);
159  return FALSE;
160  }
161 
162  $this->_thesaurus->markContentsChanged();
163  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
164  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
165 
166  if ($created) {
167  $em->broadcastEvent($this, 'AssetCreate', Array('name','assetid'));
168  }
169 
170  return $this->_thesaurus->id.':'.$linkid;
171 
172  }//end create()
173 
174 
181  function getAssetMapLinks()
182  {
183  $links = Array();
184 
185  if (isset($this->_termid)) {
186  $links = $this->_thesaurus->_getShadowAssetMapLinks($this->_termid);
187  }
188 
189  return $links;
190 
191  }//end getAssetMapLinks()
192 
193 
207  function _getAllowedLinks()
208  {
209  return Array(
210  SQ_LINK_TYPE_1 => Array(
211  'thesaurus_term' => Array(
212  'card' => 'M',
213  'exclusive' => FALSE,
214  ),
215  ),
216  SQ_LINK_TYPE_2 => Array(
217  'thesaurus_term' => Array(
218  'card' => 'M',
219  'exclusive' => FALSE,
220  ),
221  ),
222  SQ_LINK_TYPE_3 => Array(),
223  );
224 
225  }//end _getAllowedLinks()
226 
227 
234  function saveAttributes()
235  {
236  if (empty($this->_thesaurus)) return TRUE;
237 
238  $name = $this->attr('name');
239 
240  // If the name is updated, we are going to update the name of this object so when it broadcasts an
241  // even, the new name is used instead. In particular it is
242  if ($this->name != $this->attr('name')) {
243  $this->name = $name;
244  }//end if
245 
246  $success = $this->_thesaurus->updateTerm($this->_termid, $name);//, $notes);
247 
248  if ($success) {
249  $this->_thesaurus->saveAttributes();
250 
251  if (!empty($this->_tmp['vars_set'])) {
252  // prepare to broadcast the attributes_changed
253  foreach ($this->_tmp['vars_set'] as $attr => $details) {
254  $changed_vars[$attr] = Array(
255  'type' => $this->vars[$attr]['type'],
256  'old_value' => $details['old_value'],
257  'new_value' => $this->attr($attr),
258  );
259  }
260  $GLOBALS['SQ_SYSTEM']->broadcastTriggerEvent('trigger_event_attributes_changed', $this, $changed_vars);
261  }
262 
263  unset($this->_tmp['vars_set']);
264  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
265  $em->broadcastEvent($this, 'AssetUpdate', Array('name'));
266  $GLOBALS['SQ_SYSTEM']->broadcastTriggerEvent('trigger_event_attributes_saved', $this);
267  }
268  return $success;
269 
270  }//end saveAttributes()
271 
272 
282  function createAssetLink($major, $minor)
283  {
284 
285  }//end createAssetLink()
286 
287 
306  function canMoveLink(&$minor, &$old_major, $link_type)
307  {
308  return $this->canCreateLink($minor, $link_type, 0);
309 
310  }//end canMoveLink()
311 
312 
324  function canCreateLink(&$minor, $link_type, $exclusive)
325  {
326  $create_link = FALSE;
327 
328  if (($minor instanceof Thesaurus_Term) && ($exclusive == 0)) {
329  $create_link = (($link_type == SQ_LINK_TYPE_1) || ($link_type == SQ_LINK_TYPE_2));
330  }
331 
332  return $create_link;
333 
334  }//end canCreateLink()
335 
336 
346  function addTermNote($name, $note)
347  {
348  $this->_thesaurus->addTermNote($this->_termid, $name, $note);
349 
350  }//end addTermNote()
351 
352 
359  function deleteTermNotes()
360  {
361  $this->_thesaurus->deleteTermNotes($this->_termid);
362 
363  }//end deleteTermNotes()
364 
365 
372  function getTermNotes()
373  {
374  return $this->_thesaurus->getTermNotes($this->_termid);
375 
376  }//end getTermNotes()
377 
378 }//end class
379 ?>