Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
thesaurus_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page_edit_fns.inc';
19 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
20 
33 {
34 
35 
40  function Thesaurus_Edit_Fns()
41  {
42  $this->Asset_Edit_Fns();
43  unset($this->static_screens['tagging']);
44 
45  }//end constructor
46 
47 
58  function paintImportXML(&$asset, &$o, $prefix)
59  {
60  $access = $asset->writeAccess('attributes');
61 
62  if ($access) file_upload($prefix);
63 
64  return $access;
65 
66  }//end paintImportXML()
67 
68 
81  function processImportXML(&$asset, &$o, $prefix)
82  {
83  $info = get_file_upload_info($prefix);
84  $append = array_get_index($_REQUEST, $prefix.'_append');
85  if (empty($info)) return TRUE;
86 
87  // copy over the new uploaded file
88  if (is_uploaded_file($info['tmp_name'])) {
89 
90  // if the data directory is not created yet, make one!
91  // This would be the situation where a thesaurus asset is created from the import script
92  // since the run level is SQ_RUN_LEVEL_OPEN, _createAdditional() is not being called.
93  // Actually, do we need to create the data_path directory in _createAdditional() ?
94  if (!file_exists($asset->data_path)) {
95  if (!create_directory($asset->data_path)) {
96  trigger_localised_error('CORE0261', E_USER_WARNING, $asset->data_path);
97  return FALSE;
98  }
99  }
100 
101  // a normal uploaded file
102  if (!move_uploaded_file($info['tmp_name'], $asset->getXmlFilePath())) {
103  trigger_localised_error('CORE0235', E_USER_WARNING, $info['tmp_name'], $asset->getXmlFilePath());
104  return FALSE;
105  }
106  }
107 
108  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
109  $hh->queueHipo('hipo_job_import_thesaurus_xml', Array('thesaurus_id' => $asset->id, 'append' => $append));
110  return TRUE;
111 
112  }//end processImportXML()
113 
114 
125  function paintExampleXMLFile(&$asset, &$o, $prefix)
126  {
127  echo translate('thesaurus_example_xml_file', sq_web_path('data').'/asset_types/thesaurus/files/example.xml');
128 
129  // don't ever need a commit button
130  return FALSE;
131 
132  }//end paintExampleXMLFile()
133 
134 
145  function paintExportXMLfile(&$asset, &$o, $prefix)
146  {
147 
148  $write_access_allowed = $asset->writeAccess('attributes');
149 
150  if (is_readable($asset->getXmlFilePath())) {
151  if (!empty($_REQUEST[$prefix.'_process_export'])) {
152  $this->outputXmlDirect($asset);
153  }
154  // download
155  echo hidden_field($prefix.'_process_export', '0');
156  echo normal_button($prefix.'_export', translate('download_file'), 'this.form.'.$prefix.'_process_export.value = 1; this.form.submit(); SQ_FORM_SUBMITTED = false; this.form.'.$prefix.'_process_export.value = 0; return true;');
157  echo ' Generated on '.date ('r', filectime($asset->getXmlFilePath()));
158  echo '<br />';
159  }
160 
161 
162  if ($asset->countTerms()==0) {
163  echo translate('thesaurus_thesaurus_disabled');
164  } else if ($write_access_allowed) {
165  echo hidden_field($prefix.'_process_generate', '0');
166  echo normal_button($prefix.'_regenerate', translate('thesaurus_generate_file'), 'this.form.'.$prefix.'_process_generate.value = 1; this.form.submit(); SQ_FORM_SUBMITTED = false; this.form.'.$prefix.'_process_generate.value = 0; return true;');
167  } else if ($asset->attr('contents_changed')) {
168  // a new file needs to be generated and we don't have a lock
169  echo translate('thesaurus_thesaurus_not_generated');
170  }
171 
172  return $write_access_allowed;
173 
174  }//end paintExportXMLfile()
175 
176 
187  function processExportXMLfile(&$asset, &$o, $prefix)
188  {
189 
190  $write_access_allowed = $asset->writeAccess('attributes');
191  if (!$write_access_allowed) {
192  return $write_access_allowed;
193  }
194  // If the export form was submitted and the thesaurus contents have not changed then the paint function has
195  // already called outputXmlDirect for us. This is because we want outputXmlDirect to be available without acquiring lock.
196 
197  // If the export form was submitted and the thesaurus contents have changed, so rebuild the export file
198  if (!empty($_REQUEST[$prefix.'_process_generate'])) {
199  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
200  $hh->queueHipo('hipo_job_export_thesaurus_xml', Array('thesaurus_id' => $asset->id));
201  }
202 
203  return $write_access_allowed;
204 
205  }//end processExportXMLfile()
206 
207 
216  function outputXmlDirect(&$asset)
217  {
218  $filepath = $asset->getXmlFilePath();
219  $filepath = realpath($filepath);
220  if (is_readable($filepath)) {
221  header('Content-Type: application/xml');
222  // defining the file as attachment forces most browsers to offer it for download
223  header('Content-Disposition: attachment; filename='.basename($filepath).';');
224  header('Content-Length: '.@filesize($filepath));
225 
226  readfile($filepath);
227  @ob_flush();
228  }
229 
230  exit;
231 
232  }//end outputXmlDirect()
233 
234 
245  function paintRenameRelation(&$asset, &$o, $prefix)
246  {
247  $write_access_allowed = $asset->writeAccess('attributes');
248 
249  if (!$write_access_allowed) {
250  return $write_access_allowed;
251  }
252 
253  $relations[NULL] = '-- SELECT Relation to Rename --';
254  $relations += $asset->getActiveRelations();
255  combo_box($prefix.'_relation', $relations, FALSE, NULL);
256  label(translate('thesaurus_new_relation'),$prefix.'_new_relation');
257  text_box($prefix.'_new_relation', NULL, $size='30', $max='255');
258 
259  return $write_access_allowed;
260 
261  }//end paintRenameRelation()
262 
263 
274  function processRenameRelation(&$asset, &$o, $prefix)
275  {
276  $success = FALSE;
277  $write_access_allowed = $asset->writeAccess('attributes');
278  if (!$write_access_allowed) {
279  return $write_access_allowed;
280  }
281 
282  $relation = array_get_index($_REQUEST, $prefix.'_relation');
283  $new_relation = array_get_index($_REQUEST, $prefix.'_new_relation');
284 
285  if (!empty($relation)) {
286  $success = $asset->renameRelation($relation, $new_relation);
287  }
288 
289  return $success;
290 
291  }//end processRenameRelation()
292 
293 
304  function paintSynonymSupport(&$asset, &$o, $prefix)
305  {
306  $current_rel = $asset->attr('synonym_rel');
307  $relations = $asset->getActiveRelations();
308 
309  if (empty($current_rel)) $current_rel = NULL;
310 
311  if (!$asset->writeAccess('attributes')) {
312  $rel = array_get_index($relations, $current_rel);
313  if (is_null($current_rel)) {
314  $rel = '<em>Nothing Selected</em>';
315  } else if (empty($rel)) {
316  $rel = '<em>NULL</em>';
317  } else if (is_null($rel)) {
318  $rel = '<em style="color:red">Invalid Relation ID ('.$current_rel.')</em>';
319  }
320 
321  echo $rel;
322 
323 
324  } else {
325  if (!isset($relations[$current_rel]) && !is_null($current_rel)) {
326  echo 'Invalid Relation selected';
327  echo '<br />';
328  }
329 
330  $selectable_relations[NULL] = '-- Nothing Selected --';
331  $selectable_relations += $relations;
332  combo_box($prefix.'_synonym', $selectable_relations, FALSE, $current_rel);
333  }
334 
335  return TRUE;
336 
337  }//end paintSynonymSupport()
338 
339 
350  function processSynonymSupport(&$asset, &$o, $prefix)
351  {
352 
353  $synonym_rel = $asset->attr('synonym_rel');
354  if ($asset->writeAccess('attributes')) {
355  $selected_rel = array_get_index($_REQUEST, $prefix.'_synonym');
356  $selected_rel = trim($selected_rel);
357  if (empty($selected_rel)) $selected_rel = NULL;
358 
359  $asset->setAttrValue('synonym_rel', $selected_rel);
360  }
361 
362  return TRUE;
363 
364  }//end processSynonymSupport()
365 
366 
377  function paintAbbreviationSupport(&$asset, &$o, $prefix)
378  {
379  $current_rel = $asset->attr('abbreviation_rel');
380  $relations = $asset->getActiveRelations();
381 
382  if (empty($current_rel)) $current_rel = NULL;
383 
384  if (!$asset->writeAccess('attributes')) {
385  $rel = array_get_index($relations, $current_rel);
386  if (is_null($current_rel)) {
387  $rel = '<em>Nothing Selected</em>';
388  } else if (empty($rel)) {
389  $rel = '<em>NULL</em>';
390  } else if (is_null($rel)) {
391  $rel = '<em style="color:red">Invalid Relation ID ('.$current_rel.')</em>';
392  }
393 
394  echo $rel;
395 
396  } else {
397  if (!isset($relations[$current_rel]) && !is_null($current_rel)) {
398  echo 'Invalid Relation selected';
399  echo '<br />';
400  }
401 
402  $selectable_relations[NULL] = '-- Nothing Selected --';
403  $selectable_relations += $relations;
404  combo_box($prefix.'_abbrev', $selectable_relations, FALSE, $current_rel);
405  }
406 
407  return TRUE;
408 
409  }//end paintAbbreviationSupport()
410 
411 
422  function processAbbreviationSupport(&$asset, &$o, $prefix)
423  {
424  if ($asset->writeAccess('attributes')) {
425  $selected_rel = array_get_index($_REQUEST, $prefix.'_abbrev');
426  $selected_rel = trim($selected_rel);
427  if (empty($selected_rel)) $selected_rel = NULL;
428 
429  $asset->setAttrValue('abbreviation_rel', $selected_rel);
430  }
431 
432  return TRUE;
433 
434  }//end processAbbreviationSupport()
435 
436 
447  function paintDefaultRelation(&$asset, &$o, $prefix)
448  {
449  $current_rel = $asset->attr('default_rel');
450  $relations = $asset->getActiveRelations();
451 
452  if (empty($current_rel)) $current_rel = NULL;
453 
454  if (!$asset->writeAccess('attributes')) {
455  $rel = array_get_index($relations, $current_rel);
456  if (is_null($current_rel)) {
457  $rel = '<em>Nothing Selected</em>';
458  } else if (empty($rel)) {
459  $rel = '<em>NULL</em>';
460  } else if (is_null($rel)) {
461  $rel = '<em style="color:red">Invalid Relation ID ('.$current_rel.')</em>';
462  }
463 
464  echo $rel;
465 
466 
467  } else {
468  if (!isset($relations[$current_rel]) && !is_null($current_rel)) {
469  echo 'Invalid Relation selected';
470  echo '<br />';
471  }
472 
473  $selectable_relations[NULL] = '-- Nothing Selected --';
474  $selectable_relations += $relations;
475  combo_box($prefix.'_default', $selectable_relations, FALSE, $current_rel);
476  }
477 
478  return TRUE;
479 
480  }//end paintDefaultRelation()
481 
482 
493  function processDefaultRelation(&$asset, &$o, $prefix)
494  {
495  if ($asset->writeAccess('attributes')) {
496  $selected_rel = array_get_index($_REQUEST, $prefix.'_default');
497  $selected_rel = trim($selected_rel);
498  if (empty($selected_rel)) $selected_rel = NULL;
499 
500  $asset->setAttrValue('default_rel', $selected_rel);
501  }
502 
503  return TRUE;
504 
505  }//end processDefaultRelation()
506 
507 
518  function paintAddNewRelation(&$asset, &$o, $prefix)
519  {
520  if ($asset->writeAccess('attributes')) {
521  check_box($prefix.'_add_new_relation_check');
522  text_box($prefix.'_add_new_relation', NULL, $size='30', $max='255');
523  }
524 
525  return TRUE;
526 
527  }//end paintAddNewRelation()
528 
529 
540  function processAddNewRelation(&$asset, &$o, $prefix)
541  {
542  if ($asset->writeAccess('attributes')) {
543  $new_rel_check = array_get_index($_REQUEST, $prefix.'_add_new_relation_check');
544  if (is_null($new_rel_check)) return TRUE;
545 
546  $new_relation = array_get_index($_REQUEST, $prefix.'_add_new_relation');
547 
548  $new_relation = trim($new_relation);
549  $relation = $asset->getRelationIdByName($new_relation);
550 
551  if (is_null($relation)) {
552  $asset->addRelation($new_relation);
553  }
554  }
555 
556  return TRUE;
557 
558  }//end processAddNewRelation()
559 
560 
571  function paintDeleteRelations(&$asset, &$o, $prefix)
572  {
573  if ($asset->writeAccess('attributes')) {
574  $relations = $asset->getActiveRelations();
575  foreach ($relations as $rel_id => $rel_name) {
576  check_box($prefix.'_delete_relations['.$rel_id.']');
577  if (empty($rel_name)) $rel_name = '<em>NULL</em>';
578  echo $rel_name;
579  echo '<br />';
580  }
581  }
582 
583  return TRUE;
584 
585  }//end paintDeleteRelations()
586 
587 
598  function processDeleteRelations(&$asset, &$o, $prefix)
599  {
600  if ($asset->writeAccess('attributes')) {
601  $rel_to_delete = array_get_index($_REQUEST, $prefix.'_delete_relations');
602  if (empty($rel_to_delete)) return TRUE;
603 
604  foreach ($rel_to_delete as $rel_id => $junk) {
605  $asset->deleteRelation($rel_id);
606  }
607  }
608 
609  return TRUE;
610 
611  }//end processDeleteRelations()
612 
613 
624  function paintRelationRelflectionMap(&$asset, &$o, $prefix)
625  {
626  $all_relations = $asset->getActiveRelations();
627  $all_reflections = $asset->getReflections();
628  $all_available_relations = array_flip(array_keys($all_relations));
629  $available_relations = $all_available_relations;
630  echo '<table>';
631  foreach ($all_reflections as $start => $end) {
632  if (!isset($available_relations[$start])) continue;
633  $error = FALSE;
634  $start_name = array_get_index($all_relations, $start);
635  if (is_null($start_name)) {
636  if (!isset($all_available_relations[$start])) {
637  $start_name = '<em style="color:red">Unknown Relation. Invalid Reflection.</em>';
638  $error = TRUE;
639  } else {
640  $start_name = '<em>No Name</em>';
641  }
642  }
643 
644  $end_name = array_get_index($all_relations, $end);
645  if (is_null($end_name)) {
646  if (!isset($all_available_relations[$end])) {
647  $end_name = '<em style="color:red">Unknown Relation. Invalid Reflection.</em>';
648  $error = TRUE;
649  } else {
650  $end_name = '<em>No Name</em>';
651  }
652  }
653 
654  if ($error) {
655  $colour = '#F00';
656  } else if ($start_name == $end_name) {
657  $colour = '#090';
658  } else {
659  $colour = '#33F';
660  }
661 
662  echo '<tr>';
663  echo '<td align="right" style="color: '.$colour.'">';
664  echo $start_name;
665  echo '</td>';
666  echo '<td>';
667  echo ' / ';
668  echo '</td>';
669  echo '<td align="left" style="color: '.$colour.'">';
670  echo $end_name;
671  echo '</td>';
672  echo '<td>';
673  if ($asset->writeAccess('attributes')) {
674  check_box($prefix.'_delete_reflection['.$start.']');
675  echo 'Delete';
676  }
677  echo '</td></tr>';
678  unset($available_relations[$start]);
679  unset($available_relations[$end]);
680 
681  }//end foreach
682  echo '</table>';
683 
684  foreach ($all_relations as $id => $name) {
685  if (!isset($available_relations[$id])) {
686  unset($all_relations[$id]);
687  }
688  }
689 
690  if ($asset->writeAccess('attributes') && !empty($all_relations)) {
691  $relations[NULL] = '-- Select Relation --';
692  $relations += $all_relations;
693  combo_box($prefix.'_reflect_start', $relations, FALSE);
694  echo ' <- Reflects -> ';
695  combo_box($prefix.'_reflect_end', $relations, FALSE);
696  echo '<br />';
697  }
698 
699  return TRUE;
700 
701  }//end paintRelationRelflectionMap()
702 
703 
714  function processRelationRelflectionMap(&$asset, &$o, $prefix)
715  {
716  $all_reflexions = $asset->getReflections();
717  if ($asset->writeAccess('attributes')) {
718  $delete_reflections = array_get_index($_REQUEST, $prefix.'_delete_reflection', Array());
719  foreach ($delete_reflections as $start_id => $flag) {
720  $asset->deleteReflection($start_id);
721  }
722 
723  $start_id = array_get_index($_REQUEST, $prefix.'_reflect_start');
724  $end_id = array_get_index($_REQUEST, $prefix.'_reflect_end');
725 
726  if ($start_id !== '' && $end_id !== '') {
727  $asset->addReflection($start_id, $end_id);
728  }
729  }
730 
731  return TRUE;
732 
733  }//end processRelationRelflectionMap()
734 
735 
746  function paintReflectionAnsiCheck(&$asset, &$o, $prefix)
747  {
748  $relations = $asset->getActiveRelations();
749  $all_reflections = $asset->getReflections();
750 
751  foreach ($all_reflections as $rel_id => $reflect_id) {
752  unset ($relations[$rel_id]);
753  }
754 
755  if (!empty($relations)) {
756  echo '<span style="color: red">Configuration of this thesaurus is NOT ANSI/NISO Z39.19-2005 compliant<br />';
757  echo 'Below is a list of relations which have not been paired up<br /></span>';
758 
759  foreach ($relations as $rel_id => $relation_name) {
760  if (empty($relation_name)) {
761  $relation_name = '<em>NULL</em>';
762  }
763  echo $relation_name;
764  echo '<br />';
765  }
766 
767  } else {
768  echo 'configuration is ANSI compliant';
769  }
770 
771  return TRUE;
772 
773  }//end paintReflectionAnsiCheck()
774 
775 
786  function paintHierarchyRelation(&$asset, &$o, $prefix)
787  {
788 
789  $current_rel = $asset->attr('hierarchy_rel');
790  $relations = $asset->getActiveRelations();
791 
792  if (empty($current_rel)) $current_rel = NULL;
793 
794  if (!$asset->writeAccess('attributes')) {
795  $rel = array_get_index($relations, $current_rel);
796  if (is_null($current_rel)) {
797  $rel = '<em>Nothing Selected</em>';
798  } else if (empty($rel)) {
799  $rel = '<em>NULL</em>';
800  } else if (is_null($rel)) {
801  $rel = '<em style="color:red">Invalid Relation ID ('.$current_rel.')</em>';
802  }
803 
804  echo $rel;
805 
806 
807  } else {
808  if (!isset($relations[$current_rel]) && !is_null($current_rel)) {
809  echo 'Invalid Relation selected';
810  echo '<br />';
811  }
812 
813  $selectable_relations[NULL] = '-- Nothing Selected --';
814  $selectable_relations += $relations;
815  combo_box($prefix.'_hierarchy_relation', $selectable_relations, FALSE, $current_rel);
816  }
817 
818  return TRUE;
819 
820  }//end paintHierarchyRelation()
821 
822 
833  function processHierarchyRelation(&$asset, &$o, $prefix)
834  {
835  if ($asset->writeAccess('attributes')) {
836  $selected_rel = array_get_index($_REQUEST, $prefix.'_hierarchy_relation');
837  $selected_rel = trim($selected_rel);
838  if (empty($selected_rel)) $selected_rel = NULL;
839 
840  $asset->setAttrValue('hierarchy_rel', $selected_rel);
841  }
842 
843  return TRUE;
844 
845  }//end processHierarchyRelation()
846 
847 
848 }//end class
849 
850 ?>