Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
metadata_schema_edit_fns.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/folder/folder_edit_fns.inc';
18 
31 {
32 
33 
38  function __construct()
39  {
40  parent::__construct();
41  $this->static_screens['details']['force_unlock'] = FALSE;
42  $this->static_screens['details']['lock_type'] = 'content';
43 
44  }//end constructor
45 
46 
57  public function paintNewSection(Metadata_Schema $asset, Backend_Outputter $o, $prefix)
58  {
59  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
60  echo '<b>Section Name</b> ';
61  text_box($prefix.'_new_section', '', 20);
62  return TRUE;
63 
64  }//end paintNewSection()
65 
66 
77  public function processNewSection(Metadata_Schema $asset, Backend_Outputter $o, $prefix)
78  {
79  // need to have write access to make any changes
80  if (!$asset->writeAccess('links')) return FALSE;
81 
82  if (isset($_POST[$prefix.'_new_section']) && trim($_POST[$prefix.'_new_section'])) {
83  $new_section_name = trim($_POST[$prefix.'_new_section']);
84  $GLOBALS['SQ_SYSTEM']->am->includeAsset('metadata_section');
85  $new_section = new Metadata_Section();
86  $schema_link = Array('asset' => &$asset, 'link_type' => SQ_LINK_TYPE_2, 'sort_order' => -1, 'is_dependant' => 1);
87  $new_section->setAttrValue('name', $new_section_name);
88  if (!$new_section->create($schema_link)) return FALSE;
89  }
90 
91  return TRUE;
92 
93  }//end processNewSection()
94 
95 
106  public function paintRegenerate(Metadata_Schema $asset, Backend_Outputter $o, $prefix)
107  {
108  // need to have write access to make any changes
109  if ($asset->writeAccess('')) {
110  $o->note(translate('metadata_regenerate_for_currently_applied'));
111  check_box($prefix.'_regenerate');
112  label(translate('metadata_regenerate_files'));
113  } else {
114  $o->note(translate('metadata_cannot_regenerate'));
115  }
116 
117  return TRUE;
118 
119  }//end paintRegenerate()
120 
121 
132  public function processRegenerate(Metadata_Schema $asset, Backend_Outputter $o, $prefix)
133  {
134  // need to have write access to make any changes
135  if (!$asset->writeAccess('')) return FALSE;
136 
137  if (isset($_POST[$prefix.'_regenerate']) && $_POST[$prefix.'_regenerate']) {
138  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
139  $vars = Array('schemaids' => Array($asset->id,));
140  $hh->queueHipo('hipo_job_regenerate_metadata', $vars);
141  }
142 
143  return TRUE;
144 
145  }//end processRegenerate()
146 
147 
158  public function paintSections(Metadata_Schema $asset, Backend_Outputter $o, $prefix)
159  {
160  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
161  $write_access = $asset->writeAccess('links');
162 
163  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_section', FALSE, 'major', NULL, TRUE, FALSE);
164  foreach ($current_links as $link_data) {
165  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_data['minorid']);
166  if (is_null($section)) continue;
167 
168  $o->openSection($section->name);
169  if ($write_access) {
170  $o->openField(translate('delete_question'));
171  check_box($prefix.'_delete_sections['.$link_data['linkid'].']');
172  label(translate('delete_section-field_question'));
173  $o->closeField();
174  }
175  $edit_fns = $section->getEditFns();
176  if (!$edit_fns->paintInlineOptionInterface($section, $o)) {
177  return FALSE;
178  }
179  $o->closeSection();
180  }
181 
182  return TRUE;
183 
184  }//end paintSections()
185 
186 
197  public function processSections(Metadata_Schema $asset, Backend_Outputter $o, $prefix)
198  {
199  // need to have write access to make any changes
200  if (!$asset->writeAccess('links')) return FALSE;
201  $updated = FALSE;
202 
203  // process current steps
204  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_section', FALSE, 'major', NULL, TRUE, FALSE);
205  foreach ($current_links as $link_data) {
206  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_data['minorid']);
207  if (is_null($section)) continue;
208  $edit_fns = $section->getEditFns();
209  if ($edit_fns->processInlineOptionInterface($section, $o)) {
210  $updated = TRUE;
211  }
212  }
213 
214  // delete any sections that need deleting
215  if (isset($_POST[$prefix.'_delete_sections'])) {
216  $delete_sections = $_POST[$prefix.'_delete_sections'];
217  foreach ($delete_sections as $linkid => $on) {
218  if ($asset->deleteLink($linkid)) $updated = TRUE;
219  }
220  }
221 
222  return $updated;
223 
224  }//end processSections()
225 
226 
242  public function paintInlineValueInterface(Metadata_Schema $asset, Backend_Outputter $o, $values, $write_access, $print_cascade_values_option = TRUE, $type_code=NULL)
243  {
244  $o->openSection($asset->name);
245  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_section', FALSE, 'major', NULL, TRUE);
246  $res = FALSE;
247  foreach ($current_links as $link) {
248  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
249  if (is_null($section)) continue;
250  $edit_fns = $section->getEditFns();
251  if ($edit_fns->paintInlineValueInterface($section, $o, $values, $write_access, $print_cascade_values_option, $type_code)) {
252  $res = TRUE;
253  }
254  }
255  $o->closeSection();
256 
257  return $res;
258 
259  }//end paintInlineValueInterface()
260 
261 
274  public function processInlineValueInterface(Metadata_Schema $asset, Array &$new_values, Array &$schema_cascade_values_asset_ids, $type_code=NULL)
275  {
276  $res = FALSE;
277  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_section', FALSE, 'major', NULL, TRUE);
278  foreach ($current_links as $link) {
279  $section_cascade_values_asset_ids = Array();
280  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
281  if (is_null($section)) continue;
282  $edit_fns = $section->getEditFns();
283  if ($edit_fns->processInlineValueInterface($section, $new_values, $section_cascade_values_asset_ids, $type_code)) {
284  $res = TRUE;
285 
286  foreach($section_cascade_values_asset_ids as $asset_id => $val) {
287  $schema_cascade_values_asset_ids[$asset_id] = 1;
288  }
289  }
290  }
291 
292  return $res;
293 
294  }//end processInlineValueInterface()
295 
296 
307  public function requiredFieldsComplete(Metadata_Schema $asset, Array $values, $type_code=NULL)
308  {
309  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_section', FALSE, 'major', NULL, TRUE);
310  foreach ($current_links as $link) {
311  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
312  if (is_null($section)) continue;
313  $edit_fns = $section->getEditFns();
314  if (!$edit_fns->requiredFieldsComplete($section, $values, $type_code)) {
315  return FALSE;
316  }
317  }
318 
319  return TRUE;
320 
321  }//end requiredFieldsComplete()
322 
323 
336  public function generateMetadata(Metadata_Schema $asset, Array &$values, Array &$tag_values, Array &$keywords, $type_code=NULL)
337  {
338  $frontend_option = $asset->attr('frontend');
339 
340  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
341 
342  // Buffer the contents of any META tags
343  ob_start();
344 
345  echo '<!-- '.$mm->escapeMetadata($asset->name).' //-->'."\n\n";
346 
347  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'metadata_section', TRUE, 'major', NULL, TRUE);
348  $valid_sections = Array();
349  foreach ($current_links as $link) {
350  $section = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
351  if (is_null($section)) continue;
352  $valid_sections[] = $section->name;
353 
354  $edit_fns = $section->getEditFns();
355 
356  if (!$edit_fns->generateMetadata($section, $values, $tag_values, $keywords, $type_code)) {
357  ob_end_clean();
358  return FALSE;
359  }
360 
361  }
362 
363  // If printing on front-end, print the META tags, otherwise discard them
364  $contents = ob_get_clean();
365  if ($frontend_option) echo $contents;
366 
367  return TRUE;
368 
369  }//end generateMetadata()
370 
371 
372  //-- METADATA SCHEMA USAGE --//
373 
374 
375  public function paintUsage(Metadata_Schema $asset, Backend_Outputter $o, $prefix)
376  {
377  $page_size = 10;
378 
379  $o->openRaw();
380 
381  $schema_treeids_sql = 'SELECT
382  treeid, assetid, granted, cascades
383  FROM (sq_ast_lnk_tree ta
384  JOIN sq_ast_lnk la ON ta.linkid = la.linkid
385  JOIN sq_ast_mdata ma ON la.minorid = ma.assetid)
386  WHERE ma.schemaid = :schemaid
387  AND NOT EXISTS (
388  SELECT * FROM (sq_ast_lnk_tree tb
389  JOIN sq_ast_lnk lb ON tb.linkid = lb.linkid
390  JOIN sq_ast_mdata mb ON lb.minorid = mb.assetid)
391  WHERE mb.schemaid = ma.schemaid
392  AND length(ta.treeid) - LENGTH(tb.treeid) = :tree_size AND SUBSTR(ta.treeid, 1, LENGTH(ta.treeid) - :tree_size_1) = tb.treeid
393  AND ma.granted = mb.granted
394  AND ma.cascades = mb.cascades
395  ) ORDER BY length(treeid), treeid';
396 
397  $query = MatrixDAL::preparePdoQuery($schema_treeids_sql);
398  MatrixDAL::bindValueToPdo($query, 'schemaid', $asset->id);
399  MatrixDAL::bindValueToPdo($query, 'tree_size', SQ_CONF_ASSET_TREE_SIZE);
400  MatrixDAL::bindValueToPdo($query, 'tree_size_1', SQ_CONF_ASSET_TREE_SIZE);
401  $treeids = MatrixDAL::executePdoAssoc($query);
402 
403  if (count($treeids) > 0) {
404  $tree_tops = $this->_getTreeTops($asset, $treeids);
405 
406  $num_pages = ceil(count($tree_tops) / $page_size);
407  $page_num = array_get_index($_POST, $prefix.'_usage_page', 1);
408  $page_num = max($page_num, 1);
409  $page_num = min($page_num, $num_pages);
410 
411  if ($num_pages == 1) {
412  $page_tag = translate('page_number', $page_num, $num_pages);
413  } else {
414  $page_tag = translate('page_number_with_pager', $page_num, $num_pages, $prefix.'_usage_page');
415  }
416  $asset_count_tag = translate('item_range', ($page_num - 1) * $page_size + 1, min(count($tree_tops), $page_num * $page_size), count($tree_tops), strtolower(translate('trees')));
417 
418  $tree_tops = array_slice($tree_tops, ($page_num - 1) * $page_size, $page_size);
419 
420  $links = Array(
421  'first' => '&lt;&lt;',
422  'previous' => '&lt;',
423  'page' => $page_tag,
424  'next' => '&gt;',
425  'last' => '&gt;&gt;',
426  );
427 
428  if ($page_num > 1) {
429  $links['first'] = '<a title="'.translate('pagination_go_to_first').'" style="text-decoration: none; color: #fff" href="#" onClick="return sq_pager_jump(\''.$prefix.'_usage_page\', 1)">'.$links['first'].'</a>';
430  $links['previous'] = '<a title="'.translate('pagination_go_to_previous').'" style="text-decoration: none; color: #fff" href="#" onClick="return sq_pager_jump(\''.$prefix.'_usage_page\', '.($page_num - 1).')">'.$links['previous'].'</a>';
431  } else {
432  $links['first'] = '<span title="'.translate('pagination_cannot_go_further_back').'." style="color: #333">'.$links['first'].'</span>';
433  $links['previous'] = '<span title="'.translate('pagination_cannot_go_further_back').'." style="color: #333">'.$links['previous'].'</span>';
434  }
435 
436  if ($page_num < $num_pages) {
437  $links['last'] = '<a title="'.translate('pagination_go_to_last').'" style="text-decoration: none; color: #fff" href="#" onClick="return sq_pager_jump(\''.$prefix.'_usage_page\', '.$num_pages.')">'.$links['last'].'</a>';
438  $links['next'] = '<a title="'.translate('pagination_go_to_next').'" style="text-decoration: none; color: #fff" href="#" onClick="return sq_pager_jump(\''.$prefix.'_usage_page\', '.($page_num + 1).')">'.$links['next'].'</a>';
439  } else {
440  $links['last'] = '<span title="'.translate('pagination_cannot_go_further_forward').'." style="color: #333">'.$links['last'].'</span>';
441  $links['next'] = '<span title="'.translate('pagination_cannot_go_further_forward').'." style="color: #333">'.$links['next'].'</span>';
442  }
443 
444  hidden_field($prefix.'_usage_page', $page_num); ?>
445  <table class="sq-backend-table">
446  <thead>
447  <tr class="sq-backend-table-row">
448  <td class="sq-backend-table-header-header"><?php echo implode(' &nbsp; &nbsp; ', $links) ?></td>
449  <td class="sq-backend-table-header-header" colspan="2" style="text-align: right; font-weight: normal"><?php echo $asset_count_tag; ?></span></td>
450  </tr>
451  <tr class="sq-backend-table-row">
452  <th class="sq-backend-table-cell"><?php echo translate('asset'); ?></th>
453  <th style="width: 15%" class="sq-backend-table-cell"><?php echo translate('granted_question') ?></th>
454  <th style="width: 15ex" class="sq-backend-table-cell"><?php echo translate('cascade_question') ?></th>
455  </tr>
456  </thead>
457  <tbody>
458  <?php
459 
460  foreach ($tree_tops as $tt_assetid => &$tree_top_info) {
461  $root_tree = $tree_top_info;
462  unset($root_tree['sub_trees']);
463  $root_trees = array_merge(Array($root_tree), $tree_top_info['sub_trees']);
464 
465  foreach ($root_trees as $root_assetid => &$tree_info) {
466 
467  ?><tr class="sq-backend-table-row">
468  <td class="sq-backend-table-cell">
469  <?php
470 
471  $tag_line = get_asset_tag_line($tree_info['assetid'], 'details');
472 
473  echo ($tree_info['depth'] === 0 ? '' : '<span style="padding-left: '.($tree_info['depth']*5).'ex">');
474  echo translate('asset_and_child_count_0', $tag_line);
475  echo ($tree_info['depth'] === 0 ? '' : '</span>');
476 
477  ?></td>
478  <td class="sq-backend-table-cell">
479  <strong><span style="color: <?php echo (($tree_info['granted'] == '0') ? 'red' : 'green'); ?>"><?php echo (($tree_info['granted'] == '0') ? translate('denied') : translate('applied')) ?></span></strong>
480  </td>
481  <td class="sq-backend-table-cell">
482  <img src="<?php echo sq_web_path('lib'); ?>/web/images/<?php echo ($tree_info['cascades'] === '1' ? 'tick' : 'cross'); ?>.gif" width="15" height="15" style="vertical-align: middle;" alt="<?php echo translate($tree_info['cascades'] === '1' ? 'yes' : 'no'); ?>" />
483  </td>
484  </tr>
485  <?php
486  }//end foreach entry
487 
488  unset($tree_info);
489 
490  }//end foreach top tree
491  ?>
492 
493  </tbody>
494  </table>
495  <?php
496 
497  $o->note(translate('core_schema_usage_explanation'));
498 
499  } else {
500  $o->note(translate('core_schema_usage_no_assets'));
501  }
502 
503  $o->closeRaw();
504 
505  return FALSE;
506 
507  }//end paintUsage()
508 
509 
526  public function _getTreeTops(Metadata_Schema $asset, Array $treeids)
527  {
528  $used_assetids = Array();
529 
530  $all_treeids = Array();
531  foreach ($treeids as $treeid) {
532  $all_treeids[$treeid['treeid']] = $treeid;
533  }
534  unset($treeids);
535 
536  $res = Array();
537  foreach ($all_treeids as $tree_info) {
538  $treeid = $tree_info['treeid'];
539  $treeid_copy = $treeid;
540  $top_parent = $treeid;
541  $depth = 0;
542  $used_sub_tree = FALSE;
543 
544  while (empty($treeid_copy) === FALSE) {
545  if (isset($res[$treeid_copy]) === TRUE) {
546  if ($used_sub_tree === FALSE) {
547  $used_sub_tree = TRUE;
548  }
549  }
550 
551  if (($used_sub_tree === TRUE) && (array_key_exists($treeid_copy, $all_treeids) === TRUE)) {
552  $top_parent = $treeid_copy;
553  $depth++;
554  }
555 
556  $treeid_copy = substr($treeid_copy, 0, -SQ_CONF_ASSET_TREE_SIZE);
557  }
558 
559  $res[$treeid] = $tree_info;
560  $res[$treeid]['top_parent'] = $top_parent;
561  $res[$treeid]['depth'] = $depth;
562  $res[$treeid]['sub_trees'] = Array();
563 
564  // Have we seen this asset before? If so, let it go, but mark it
565  // to be dumped afterwards so we only have one of each asset
566  if (array_search($tree_info['assetid'], $used_assetids) === FALSE) {
567  $res[$treeid]['dupe'] = FALSE;
568  $used_assetids[] = $tree_info['assetid'];
569  } else {
570  $res[$treeid]['dupe'] = TRUE;
571  }
572  }
573 
574  foreach ($res as $key => &$res_item) {
575  if ($res_item['dupe'] === TRUE) {
576  unset($res[$key]);
577  } else {
578  if ($res_item['depth'] > 0) {
579  $res[$res_item['top_parent']]['sub_trees'][$res_item['treeid']] =& $res_item;
580  unset($res[$key]);
581  }
582  }
583  }
584 
585  // Sort each sub-tree top-to-bottom, to ensure correct tree order
586  foreach ($res as $key => &$res_item) {
587  uksort($res_item['sub_trees'], create_function('$key1,$key2', 'return strcmp($key1, $key2);'));
588  }
589 
590  unset($res_item);
591 
592  return $res;
593 
594  }//end _getTreeTops()
595 
596 
597 }//end class
598 
599 ?>