Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tagging.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 
39 function paintTagging(&$owner, &$o, &$ei, $keywords=NULL)
40 {// TODO TOF TEST
41  $root = $ei->findScreen('static_screen_tagging');
42  if (is_null($root)) return FALSE;
43  $write_access = $owner->writeAccess('menu');
44  $prefix = $owner->getPrefix();
45  $print_commit_button = FALSE;
46 
47  foreach ($root->children() as $section) {
48 
49  // only do this if we are not painting a layout
50  if (is_null($keywords)) {
51  // if we are in limbo and this section is not to be printed in limbo - dont print it
52  if (SQ_IN_LIMBO && (!isset($section->attributes()->limbo_access) || ((int)$section->attributes()->limbo_access) == 0)) {
53  continue;
54  }
55  }
56 
57  $section_access = $ei->_getAccess($owner, $section, $prefix);
58  if (!($section_access & SQ_EI_READ)) {
59  continue;
60  }
61 
62  if (!is_null($keywords) && isset($keywords['sections'][strtolower( str_replace(' ', '_', (string)$section->attributes()->name) )])) {
63  ob_start();
64  }
65 
66  $section_name = isset($section->attributes()->display_name) ? $section->attributes()->display_name : $section->attributes()->name;
67  $o->openSection($section_name);
68 
69  if (!($num_fields = count($section->children()))) {
70  continue;
71  }
72 
73  foreach ($section->children() as $field) {
74 
75  $field_access = $ei->_getAccess($owner, $field, $prefix);
76  if (!($field_access & SQ_EI_READ)) {
77  continue;
78  }
79 
80  switch (strtolower((string)$field->attributes()->code)) {
81 
82  case 'suggest_tags' :
83 
84 
85 
86  $o->openField('');
87  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
88  ob_start();
89  }
90 
91  if ($write_access) {
92  ob_start();
93  $sm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('search_manager', TRUE);
94  if (!empty($sm)) {
95  ob_start();
96  ?>
97 <a href="#" onClick="Javascript: var win = window.open('<?php echo sq_web_path('lib'); ?>/tag_suggestion/tag_suggestion.php?assetid=<?php echo $owner->id; ?>&prefix=<?php echo $prefix; ?>', 'keywords', 'toolbar=0,menubar=0,location=0,status=0,scrollbars=1,resizable=1,width=580,height=320'); return FALSE;">
98  <?php echo translate('tag_suggestion') ?></a>
99 <?php
100  $replacements_link = ob_get_contents();
101  ob_end_clean();
102  echo(translate('tagging_suggest_indexed', $replacements_link));
103  }
104  ob_end_flush();
105  }
106  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
107  $ei->_tmp['layout_keyword_replacements'][$keywords['fields'][strtolower((string)$field->attributes()->code)]] = ob_get_contents();
108  ob_end_flush();
109  }
110  $o->closeField();
111  break;
112 
113  case 'tags' :
114 
115  $o->openField('');
116  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
117  ob_start();
118  }
119 
120  $tag_manager = $GLOBALS['SQ_SYSTEM']->getTagManager();
121  $current_tag_links = $tag_manager->getTagLinks($owner->id);
122  ob_start();
123  if ($write_access) {
124 
125  // there will be multiple tags, so start by getting a list of links to this assets to display
126  $current_tag_ids = Array();
127  foreach ($current_tag_links as $key => $current_tag_link) {
128  $current_tag_ids[$key] = $current_tag_link['minorid'];
129  }
130 
131  if ($tag_manager->isTagSourceRestricted()) {
132  // restricted; check whether there's any thesaurus to choose from
133  if ($tag_manager->attr('thesaurus')) {
134  multiple_asset_finder($prefix.'_tags', $current_tag_ids, Array('thesaurus_term'=>'D'));
135  $o->note(translate('tag_thesaurus_note', get_asset_tag_line($tag_manager->attr('thesaurus'), 'details')));
136  } else {
137  $o->note(translate('tag_no_thesaurus_set_note'));
138  }
139  } else {
140  // unrestricted; just print the asset finder
141  multiple_asset_finder($prefix.'_tags', $current_tag_ids, Array('thesaurus_term'=>'D'));
142  }
143 
144  } else {
145  foreach ($current_tag_links as $key => $current_tag_link) {
146  echo get_asset_tag_line($current_tag_link['minorid']).'<br>';
147  }
148  }
149 
150  if (isset($field->note) && (strlen((string)$field->note) > 0)) {
151  $o->note((string)$field->note);
152  }
153  ob_end_flush();
154  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
155  $ei->_tmp['layout_keyword_replacements'][$keywords['fields'][strtolower((string)$field->attributes()->code)]] = ob_get_contents();
156  ob_end_flush();
157  }
158  $o->closeField();
159  break;
160 
161  case 'weights' :
162 
163  $tag_manager = $GLOBALS['SQ_SYSTEM']->getTagManager();
164  $current_tag_links = $tag_manager->getTagLinks($owner->id);
165  $o->openField('');
166  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
167  ob_start();
168  }
169 
170  ob_start();
171 
172  if ($write_access) {
173  foreach ($current_tag_links as $key => $current_tag_link) {
174  echo get_asset_tag_line($current_tag_link['minorid']).text_box($prefix.'_weights['.$current_tag_link['linkid'].']', $current_tag_link['value'], 4).'<br>';
175  }
176  } else {
177  foreach ($current_tag_links as $key => $current_tag_link) {
178  echo get_asset_tag_line($current_tag_link['minorid']).' '.translate('weight').': '.$current_tag_link['value'].'<br>';
179  }
180  }
181 
182  if (isset($field->note) && (strlen((string)$field->note) > 0)) {
183  $o->note((string)$field->note);
184  }
185 
186  ob_end_flush();
187 
188  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
189  $ei->_tmp['layout_keyword_replacements'][$keywords['fields'][strtolower((string)$field->attributes()->code)]] = ob_get_contents();
190  ob_end_flush();
191  }
192  $o->closeField();
193  break;
194 
195  case 'cascading' :
196  if ($write_access) {
197  $kiddies = $GLOBALS['SQ_SYSTEM']->am->getChildren($owner->id, Array(), FALSE, FALSE);
198  $kiddies = array_filter($kiddies);
199  if (count($kiddies)) {
200  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
201  ob_start();
202  }
203 
204  $o->openSection(translate('cascade_options'));
205  $o->openField('Note');
206  echo translate('action_affects_assets_below_this', count($kiddies), htmlspecialchars($owner->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
207  $o->closeField();
208  $o->openField('&nbsp;');
209  check_box($prefix.'_tagging[cascade_changes]', '1', TRUE);
210  label(translate('cascade_tag_changes'), $prefix.'_tagging[cascade_changes]');
211  echo '<br/>';
212 
213  $o->note(translate('tagging_cascade_changes_note'));
214  check_box($prefix.'_tagging[manual_cascade]');
215  label(translate('manually_cascade_all_tags'), $prefix.'_tagging[manual_cascade]');
216  echo '<br/>';
217 
218  $o->note(translate('tagging_manual_cascade_note'));
219 
220  $o->closeField();
221 
222  $o->closeSection();
223  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
224  $ei->_tmp['layout_keyword_replacements'][$keywords['fields'][strtolower((string)$field->attributes()->code)]] = ob_get_contents();
225  ob_end_flush();
226  }
227 
228 
229  }
230  }
231  break;
232 
233  }//end switch
234 
235  }//end for
236 
237  $o->closeSection();
238 
239  if (!is_null($keywords) && isset($keywords['sections'][strtolower( str_replace(' ', '_', (string)$section->attributes()->name) )])) {
240  $ei->_tmp['layout_keyword_replacements'][$keywords['sections'][strtolower( str_replace(' ', '_', (string)$section->attributes()->name) )]] = ob_get_contents();
241  ob_end_flush();
242  }
243  }//end for
244 
245  return $write_access;
246 
247 }//end paintTagging()
248 
249 
260 function paintInlineTagging(&$owner, &$o, &$ei)
261 {
262  $prefix = $owner->getPrefix();
263 
264  $tag_manager = $GLOBALS['SQ_SYSTEM']->getTagManager();
265  $current_tag_links = $tag_manager->getTagLinks($owner->id);
266 
267  if (empty($current_tag_links)) return FALSE;
268 
269  $o->openSection(translate('content_tags'));
270  $o->openField('&nbsp;');
271  ?>
272  <table class="sq-backend-table">
273  <tr>
274  <td class="sq-backend-table-header">
275  <?php echo translate('content_tag'); ?>
276  </td>
277  <td class="sq-backend-table-header" align="right">
278  <?php echo translate('weight'); ?>
279  </td>
280  </tr>
281 
282  <?php
283  foreach ($current_tag_links as $key => $current_tag_link) {
284  ?>
285  <tr>
286  <td class="sq-backend-table-cell">
287  <?php
288  echo get_asset_tag_line($current_tag_link['minorid']);
289  ?>
290  </td>
291  <td class="sq-backend-table-cell" align="right">
292  <?php
293  echo $current_tag_link['value'];
294  ?>
295  </td>
296  </tr>
297  <?php
298  }
299  ?></table><?php
300  $o->closeField();
301  $o->closeSection();
302 
303  return TRUE;
304 
305 }//end paintInlineTagging()
306 
307 
320 function processTagging(&$owner, &$o, $ei)
321 {
322  // if we dont have write access, dont process anything
323  if (!$owner->writeAccess('menu')) return FALSE;
324  $prefix = $owner->getPrefix();
325 
326  $tag_manager = $GLOBALS['SQ_SYSTEM']->getTagManager();
327  $submitted_tags = array_get_index($_REQUEST, $prefix.'_tags', Array());
328  // are we cascading these tag changes to our children? If we are doing a manual cascade, that is handled later, after changes have been made to the parent asset.
329  $cascade_changes = FALSE;
330  if (isset($_POST[$prefix.'_tagging']['cascade_changes']) && $_POST[$prefix.'_tagging']['cascade_changes'] ) {
331 
332  $cascade_changes = TRUE;
333  }
334 
335 
336  // are we manually cascading?
337  $manual_cascade = FALSE;
338  if (isset($_POST[$prefix.'_tagging']['manual_cascade']) && $_POST[$prefix.'_tagging']['manual_cascade'] ) {
339  $manual_cascade = TRUE;
340  $cascade_changes = FALSE;
341  }
342 
343 
344  $tag_vars = Array();
345  // Build a 'to do' list for the hippo job with additions, deletions and weight changes
346  foreach ($submitted_tags as $key => $submitted_tag) {
347  $submitted_tag_ids[$key] = $submitted_tag['assetid'];
348  }
349 
350  $current_tag_links = $tag_manager->getTagLinks($owner->id);
351  $current_tag_ids = Array();
352  $current_weights = Array();
353 
354  foreach ($current_tag_links as $key => $current_tag_link) {
355  $current_tag_ids[$key] = $current_tag_link['minorid'];
356  $current_weights[$current_tag_link['linkid']] = $current_tag_link['value'];
357  }
358 
359 
360  // Insert any new tags
361  $new_tag_ids = array_diff($submitted_tag_ids, $current_tag_ids);
362  foreach ($new_tag_ids as $one_tag_id) {
363 
364  if (empty($one_tag_id)) continue;
365 
366  if ($cascade_changes) {
367  // cascading creation, so queue it up for the HIPO
368  $tag_vars[] = Array(
369  'tag_id' => $one_tag_id,
370  'action' => 'add',
371  'weight' => '1',
372  );
373  } else {
374  $tag_manager->setTag($owner->id, $one_tag_id, '1');
375  }
376  }
377 
378 
379  $submitted_weights = array_get_index($_REQUEST, $prefix.'_weights', Array());
380  foreach ($submitted_weights as $linkid => $weight) {
381  if (empty($weight)) $submitted_weights[$linkid] = 1;
382  }
383 
384  $modified_weights = array_diff_assoc($submitted_weights, $current_weights);
385  foreach ($modified_weights as $linkid => $weight) {
386  if ($cascade_changes) {
387  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($linkid);
388  // cascading add, so queue it up for the HIPO
389  $tag_vars[] = Array(
390  'tag_id' => $link['minorid'],
391  'action' => 'add',
392  'weight' => $weight,
393  );
394  } else {
395  $tag_manager->setTagWeight($linkid, $weight);
396  }
397  }
398 
399 
400  // delete any tags that haven't been resubmitted
401  $deleted_tag_ids = array_diff($current_tag_ids, $submitted_tag_ids);
402  foreach ($deleted_tag_ids as $one_tag_id) {
403  if (empty($one_tag_id)) continue;
404 
405  if ($cascade_changes) {
406  // cascading delete, so queue it up for the HIPO
407  $tag_vars[] = Array(
408  'tag_id' => $one_tag_id,
409  'action' => 'delete',
410  'weight' => NULL,
411  );
412  } else {
413  $tag_manager->deleteTag($owner->id, $one_tag_id);
414  }
415  }
416 
417 
418  // to perform a manual cascade, we make any changes necessary to the parent asset, then build a list of actions required to add all the tags of the parent to any children
419  if ($manual_cascade) {
420  $current_tag_links = $tag_manager->getTagLinks($owner->id);
421 
422  foreach ($current_tag_links as $link) {
423  $tag_vars[] = Array(
424  'tag_id' => $link['minorid'],
425  'action' => 'add',
426  'weight' => $link['value'],
427  );
428  }
429  }
430 
431 
432  if (!empty($tag_vars)) {
433  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
434  $vars = Array(
435  'assets' => Array(
436  $owner->id => Array(
437  'type_code' => $owner->type(),
438  ),
439  ),
440  'tag_changes' => $tag_vars,
441  );
442 
443  $hh->queueHipo('hipo_job_edit_tags', $vars);
444  }
445 
446  // no need to call saveAttributes
447  return FALSE;
448 
449 }//end processTagging()
450 
451 
452 ?>