17 require_once SQ_FUDGE_PATH.
'/general/text.inc';
56 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
57 if (is_null($asset))
return TRUE;
63 $schemas = $this->
getSchemas($asset->id, TRUE);
64 if (empty($schemas))
return TRUE;
68 foreach ($schemas as $schemaid) {
69 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid);
70 if (is_null($schema))
continue;
72 $edit_fns = $schema->getEditFns();
73 if (!$edit_fns->requiredFieldsComplete($schema, $values, $asset->type())) {
93 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
94 if (is_null($asset))
return FALSE;
95 $edit_fns = $asset->getEditFns();
96 return isset($edit_fns->static_screens[
'metadata']);
118 $sql =
' SELECT DISTINCT schemaid, granted, cascades
119 FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata ';
120 $where =
'assetid = :assetid';
121 if (!is_null($granted)) {
122 $where .=
' AND granted = :granted';
124 if (!is_null($cascades)) {
125 $where .=
' AND cascades = :cascades';
127 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where);
132 if (!is_null($granted)) {
135 if (!is_null($cascades)) {
139 }
catch (Exception $e) {
140 throw new Exception(
'Unable to get applied schemas for asset ID #'.$assetid.
' due to database error: '.$e->getMessage());
144 foreach ($result as $data) {
145 if (is_null($granted)) {
146 $schemas[$data[
'schemaid']] = $data[
'granted'];
148 $schemas[] = $data[
'schemaid'];
165 $sql =
' SELECT DISTINCT schemaid, granted, cascades
166 FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata ';
167 $where =
'assetid = :assetid';
168 if (!is_null($schemaid)) {
169 $where .=
' AND schemaid = :schemaid';
171 if (!is_null($cascades)) {
172 $where .=
' AND cascades = :cascades';
174 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where);
179 if (!is_null($schemaid)) {
182 if (!is_null($cascades)) {
186 }
catch (Exception $e) {
187 throw new Exception(
'Unable to get asset metadata schemas of asset ID #'.$assetid.
' due to database error: '.$e->getMessage());
191 foreach ($result as $data) {
192 if ($include_cascades) {
193 $schemas[$data[
'schemaid']] = Array(
194 'granted' => $data[
'granted'],
195 'cascades' => $data[
'cascades'],
198 $schemas[$data[
'schemaid']] = $data[
'granted'];
203 if (!is_null($schemaid)) {
204 if (array_key_exists($schemaid, $schemas)) {
205 $schemas = $schemas[$schemaid];
228 function setSchema($assetid, $schemaid, $granted, $cascades=TRUE, $force_set=FALSE)
230 $db_action =
'insert';
231 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid);
232 if (is_null($schema)) {
233 trigger_localised_error(
'SYS0047', E_USER_WARNING, $schemaid);
238 trigger_localised_error(
'SYS0045', E_USER_WARNING, $schema->name, $schemaid);
242 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
243 if (is_null($asset)) {
244 trigger_localised_error(
'SYS0046', E_USER_WARNING, $assetid);
248 if (!$asset->adminAccess(
'metadata')) {
249 trigger_localised_error(
'SYS0031', E_USER_WARNING, $asset->name);
254 $ms = $GLOBALS[
'SQ_SYSTEM']->getMessagingService();
261 if (!empty($schema_info)) {
263 if ((
bool)$schema_info[
'granted'] == $granted) {
265 if ((
bool)$schema_info[
'cascades'] == $cascades) {
270 $db_action =
'update';
274 $db_action =
'update';
277 $new_access = ($granted) ?
'apply' :
'deny';
278 $current_access = ($granted) ?
'denied' :
'applied';
280 trigger_localised_error(
'SYS0032', E_USER_WARNING, $new_access, $schema->name, $asset->name, $current_access);
286 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
287 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
291 'asset_name' => $asset->name,
292 'metadata_schema' => $schema->name,
294 $msg_type =
'asset.metadata.schema.set.fulllog';
295 $message = $ms->newMessage(Array(), $msg_type, $msg_reps);
296 $message->parameters[
'assetid'] = $asset->id;
297 $ms->logMessage($message);
300 switch ($db_action) {
304 'assetid' => $asset->id,
305 'schemaid' => (
string) $schemaid,
306 'granted' => (
string) ($granted ?
'1' :
'0'),
307 'cascades' => (
string) ($cascades ?
'1' :
'0'),
314 'assetid' => $asset->id,
315 'schemaid' => (
string) $schemaid,
316 'granted' => (
string) ($granted ?
'1' :
'0'),
317 'cascades' => (
string) ($cascades ?
'1' :
'0'),
323 }
catch (Exception $e) {
324 throw new Exception(
'Unable to '.$message.
' metadata schema id #'.$schemaid.
' on assetid #'.$asset->id.
' due to the following database error:'.$e->getMessage());
329 if (!$asset->shouldFastTrack(
'metadata_manager_generate_content_file', $schemaid)) {
330 $all_contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
331 foreach ($all_contexts as $all_contextid => $context_data) {
335 $em = $GLOBALS[
'SQ_SYSTEM']->getEventManager();
338 $fast_tracked_schemas = $asset->getFaskTrackedTaskAssetids(
'metadata_manager_generate_content_file');
339 if (!empty($fast_tracked_schemas)) {
340 foreach($fast_tracked_schemas as $ft_schemaid) {
341 $em->broadcastEvent($asset,
'MetadataUpdate', Array(
'schemaid' => $ft_schemaid));
344 $em->broadcastEvent($asset,
'MetadataUpdate', Array(
'schemaid' => $schemaid));
345 $GLOBALS[
'SQ_SYSTEM']->broadcastTriggerEvent(
'trigger_event_metadata_updated', $asset);
349 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
350 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
369 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
370 if (is_null($asset)) {
371 trigger_localised_error(
'SYS0036', E_USER_WARNING, $assetid);
375 if (!$asset->adminAccess(
'metadata')) {
376 trigger_localised_error(
'SYS0029', E_USER_WARNING, $asset->name);
381 $ms = $GLOBALS[
'SQ_SYSTEM']->getMessagingService();
384 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
385 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
388 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid);
390 'asset_name' => $asset->name,
391 'metadata_schema' => $schema->name,
393 $msg_type =
'asset.metadata.schema.delete.fulllog';
394 $message = $ms->newMessage(Array(), $msg_type, $msg_reps);
395 $message->parameters[
'assetid'] = $asset->id;
396 $ms->logMessage($message);
400 'schemaid' => $schemaid,
401 'assetid' => $asset->id,
405 }
catch (Exception $e) {
406 throw new Exception(
'Unable to delete schemaid #'.$schemaid.
' on assetid #'.$asset->id.
' due to the following database error:'.$e->getMessage());
411 $all_contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
412 foreach ($all_contexts as $all_contextid => $context_data) {
413 if ($all_contextid === 0) {
417 $context_metadata = $this->
getMetadata($assetid, 0, $all_contextid);
418 if (empty($context_metadata) === FALSE) {
424 $em = $GLOBALS[
'SQ_SYSTEM']->getEventManager();
425 $em->broadcastEvent($asset,
'MetadataDeleted', Array(
'schemaid' => $schemaid));
426 $GLOBALS[
'SQ_SYSTEM']->broadcastTriggerEvent(
'trigger_event_metadata_updated', $asset);
428 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
429 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
447 $sql =
'SELECT DISTINCT
450 '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata ';
452 $where =
'schemaid = :schemaid';
453 if (!is_null($granted)) {
454 $where .=
' AND granted = :granted';
456 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where);
461 if (!is_null($granted)) {
465 }
catch (Exception $e) {
466 throw new Exception(
'Unable to get assetid with schemaid #'.$schemaid.
' applied due to the following database error:'.$e->getMessage());
470 foreach ($result as $data) {
471 if (is_null($granted)) {
472 $assets[$data[
'assetid']] = $data[
'granted'];
474 $assets[] = $data[
'assetid'];
494 if (!is_array($schemaids)) {
495 return $GLOBALS[
'SQ_SYSTEM']->am->getChildren($schemaids,
'metadata_field', FALSE);
498 foreach ($schemaids as $schemaid) {
499 $field_children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($schemaid,
'metadata_field', FALSE);
500 foreach ($field_children as $fieldid => $field_type) {
501 $fieldids[$fieldid] = $field_type;
530 if ($contextid === NULL) {
531 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
534 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
535 if (is_null($asset)) {
536 trigger_localised_error(
'SYS0043', E_USER_WARNING, $assetid);
543 if (!in_array($schemaid, $schemas))
return Array();
545 $schemas = Array($schemaid);
548 if (empty($schemas))
return Array();
553 mv.fieldid, a.name, mv.value
555 '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_val mv
557 '.SQ_TABLE_RUNNING_PREFIX.
'ast a ON mv.fieldid = a.assetid';
559 $where =
' mv.assetid = :assetid
560 AND mv.contextid = :contextid
563 $bind_vars = Array();
564 foreach ($schemas as $schemaid) {
565 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid);
566 if (is_null($schema)) {
567 trigger_localised_error(
'SYS0044', E_USER_WARNING, $schemaid);
571 $bind_prefix =
'gc_'.$schemaid.
'_';
572 $ret_val = $GLOBALS[
'SQ_SYSTEM']->am->generateGetChildrenQuery($schema,
'metadata_field', FALSE, NULL, NULL, NULL, TRUE, FALSE, NULL, NULL, TRUE, NULL, $bind_prefix);
573 $sql_array = $ret_val[
'sql_array'];
574 $bind_vars = array_merge($bind_vars, $ret_val[
'bind_vars']);
576 mv.fieldid IN ('.implode(
' ', $sql_array).
') OR ';
580 $where = trim($where,
' OR ').
'
583 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'mv');
584 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'a');
590 foreach ($bind_vars as $bind_var => $bind_value) {
594 }
catch (Exception $e) {
596 throw new Exception(translate_error(
'SYS0338', $asset->id, $contextid, $e->getMessage()));
615 $contextids = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
616 $assetid_cond =
'assetid = '.MatrixDAL::quote((
string)$assetid);
617 $fieldid_conds = Array();
618 $bind_vars = Array();
620 foreach ($this->
getSchemas($assetid, TRUE) as $schemaid) {
621 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid,
'metadata_schema');
623 $bind_prefix =
'gc_'.$schemaid.
'_';
624 $ret_val = $GLOBALS[
'SQ_SYSTEM']->am->generateGetChildrenQuery($schema,
'metadata_field', FALSE, NULL, NULL, NULL, TRUE, FALSE, NULL, NULL, TRUE, NULL, $bind_prefix);
625 $sql_array = $ret_val[
'sql_array'];
626 $bind_vars = array_merge($bind_vars, $ret_val[
'bind_vars']);
628 $fieldid_conds[] =
'(fieldid NOT IN ('.implode(
' ', $sql_array).
'))';
631 $sql =
'DELETE FROM sq_ast_mdata_val
633 $where =
'WHERE '.$assetid_cond;
635 if (!empty($fieldid_conds)) {
636 $where .=
' AND (('.implode(
"\n\tAND ", $fieldid_conds).
') OR (contextid NOT IN ('.implode(
', ', array_keys($contextids)).
')))';
643 foreach ($bind_vars as $bind_var => $bind_value) {
647 }
catch (Exception $e) {
648 throw new Exception(translate_error(
'SYS0342', $assetid, $e->getMessage()));
666 if ($contextid === NULL) {
667 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
672 static $cached_field_names = Array();
674 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
676 $schema = $am->getAsset($schemaid);
677 if (is_null($schema)) {
678 trigger_localised_error(
'SYS0040', E_USER_WARNING, $schemaid);
684 $default_values = Array();
685 $field_names = Array();
686 $fields_to_check = Array();
688 foreach ($fields as $fieldid => $type_code) {
689 if (isset($cached_field_names[$fieldid])) {
690 $field_names[$fieldid] = $cached_field_names[$fieldid];
692 $fields_to_check[] = $fieldid;
695 $new_field_names = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($fields_to_check,
'metadata_field', FALSE,
'name');
696 $field_names += $new_field_names;
698 foreach ($fields as $fieldid => $type_code) {
699 if (!isset($cached_field_names[$fieldid])) {
700 $cached_field_names[$fieldid] = $field_names[$fieldid];
703 $default_values[$fieldid] = Array(
704 'name' => $field_names[$fieldid],
709 return $default_values;
726 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
727 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
731 include $metadata_filename;
734 $metadata[
'values'] = $metadata_values;
735 $metadata[
'value_components'] = $metadata_value_components;
736 $metadata[
'warnings'] = $metadata_warnings;
757 if ($contextid === NULL) {
758 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
761 if ($contextid === 0) {
762 $metadata_basename =
'metadata.php';
764 $metadata_basename =
'metadata.'.$contextid.
'.php';
769 $GLOBALS[
'SQ_SYSTEM']->changeContext($contextid);
771 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
772 $asset = $am->getAsset($assetid);
773 $field_values = Array();
775 if (is_null($asset)) {
776 trigger_localised_error(
'SYS0038', E_USER_WARNING, $assetid);
777 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
778 return $field_values;
781 $schemas = $this->
getSchemas($asset->id, TRUE);
782 if (empty($schemas)) {
783 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
784 return $field_values;
791 $_metadata_filename = $asset->data_path.
'/'.$metadata_basename;
793 if (!is_file($_metadata_filename)) {
794 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
795 return $field_values;
800 if (isset($metadata[
'values'])) {
801 $metadata_values = $metadata[
'values'];
805 if (!empty($field_names)) {
806 foreach ($field_names as $key => $value) {
807 if (isset($metadata_values[$value])) {
808 $field_values[$value] = $metadata_values[$value];
809 }
else if (isset($metadata_values[str_replace(
'_',
' ', $value)])) {
810 $field_values[$value] = $metadata_values[str_replace(
'_',
' ',$value)];
812 $field_values[$value] =
'';
816 $field_values = $metadata_values;
820 $am->forgetAsset($asset);
823 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
825 return $field_values;
842 function setMetadata($assetid, $metadata, $contextid=NULL, $update_asset=TRUE, $broadcast_all_fields=FALSE, $ignore_editable=FALSE)
844 if ($contextid === NULL) {
845 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
848 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
849 if (is_null($asset)) {
850 trigger_localised_error(
'SYS0043', E_USER_WARNING, $assetid);
854 if (!$asset->writeAccess(
'metadata')) {
855 trigger_localised_error(
'SYS0030', E_USER_WARNING, $asset->name);
860 $ms = $GLOBALS[
'SQ_SYSTEM']->getMessagingService();
863 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
864 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
866 $contextable_fields = Array(
872 foreach (array_keys($metadata) as $fieldid) {
873 $flag_match_found = FALSE;
874 $field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($fieldid);
875 if (is_null($field) || !($ignore_editable || $field->attr(
'editable'))) {
876 unset($metadata[$fieldid]);
880 $current_links = $GLOBALS[
'SQ_SYSTEM']->am->getLinks($field->id, SQ_LINK_TYPE_2,
'metadata_section', FALSE,
'minor', NULL, TRUE);
881 foreach ($current_links as $link) {
882 $section = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($link[
'majorid'], $link[
'major_type_code']);
883 if (is_null($section))
continue;
884 $restrictions = $section->attr(
'restrict');
885 if (!empty($restrictions)) {
886 foreach ($restrictions as $type_code_restricted => $inherit_it) {
887 if ($inherit_it && !$flag_match_found) {
888 $asset_type_with_parents = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeInfo(Array($asset->id));
889 foreach($asset_type_with_parents[$asset->id] as $index => $asset_type) {
890 if (array_key_exists($asset_type[0], $restrictions) && !$flag_match_found) {
891 $flag_match_found = TRUE;
894 }
else if (array_key_exists($asset->type(), $restrictions)){
895 $flag_match_found = TRUE;
899 if (!$flag_match_found) {
900 unset($metadata[$fieldid]);
907 if (array_key_exists($fieldid, $metadata)) {
910 $contextable_value = (!$field->attr(
'is_contextable') || ($field instanceof
Metadata_Field_Select)) ? 0 : 1;
911 $contextable_fields[$contextable_value][] = (string)$fieldid;
919 $metadata_updated = Array();
920 foreach ($metadata as $fieldid => $value) {
922 $mdata_name =
'Unknown Metadata Field';
926 $mdata_field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($fieldid);
927 $mdata_name = $mdata_field->name;
933 foreach ($mdata_values as $old_key => $old_val) {
934 if ($old_key == $mdata_name) {
935 $old_value = $old_val;
940 if (!isset($value[0][
'value'])) {
945 if (!($old_value == $value[0][
'value'])) {
947 $metadata_updated[$fieldid] = $value;
951 'asset_name' => $asset->name,
952 'metadata_field' => $mdata_name,
953 'metadata_old' => $old_value,
954 'metadata_new' => $value[0][
'value'],
956 $msg_type =
'asset.metadata.set.fulllog';
957 $message = $ms->newMessage(Array(), $msg_type, $msg_reps);
958 $message->parameters[
'assetid'] = $asset->id;
959 $ms->logMessage($message);
964 $delete_fieldids = Array();
965 $remaining = Array();
966 foreach ($metadata as $fieldid => $details) {
967 if ($details[0][
'value'] === NULL) {
969 $delete_fieldids[] = (string) $fieldid;
971 $remaining[] = $fieldid;
978 $delete_fieldids = array_merge($delete_fieldids, $contextable_fields[0]);
980 $delete_fieldids = array_unique($delete_fieldids);
982 if (!empty($delete_fieldids)) {
986 if (count(array_intersect($delete_fieldids, $contextable_fields[1])) > 0) {
988 'assetid' => $assetid,
989 'fieldid' => array_intersect($delete_fieldids, $contextable_fields[1]),
990 'contextid' => $contextid,
996 if (count(array_intersect($delete_fieldids, $contextable_fields[0])) > 0) {
998 'assetid' => $assetid,
999 'fieldid' => array_intersect($delete_fieldids, $contextable_fields[0]),
1000 'contextid' => NULL,
1004 }
catch (Exception $e) {
1006 throw new Exception(translate_error(
'SYS0341', implode(
', ', $delete_fieldids), $asset->id, $contextid, $e->getMessage()));
1010 if (!empty($remaining)) {
1015 '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_val';
1016 $where =
' assetid = :assetid AND contextid = :contextid';
1017 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where);
1024 }
catch (Exception $e) {
1026 throw new Exception(translate_error(
'SYS0338', $asset->id, $contextid, $e->getMessage()));
1030 foreach ($remaining as $fieldid) {
1033 if (array_search($fieldid, $contextable_fields[0]) !== FALSE) {
1034 $do_contextid = NULL;
1036 $do_contextid = $contextid;
1039 $bind_vars = Array (
1040 'assetid' => $asset->id,
1041 'fieldid' => $fieldid,
1042 'value' => $metadata[$fieldid][0][
'value'],
1043 'contextid' => $do_contextid,
1046 if (!in_array($fieldid, $updates)) {
1048 if(!isset($do_contextid)){
1051 }
catch (Exception $e) {
1052 throw new Exception(translate_error(
'SYS0339', $fieldid, $metadata[$fieldid][0][
'value'], $asset->id, $contextid, $e->getMessage()));
1058 }
catch (Exception $e) {
1060 throw new Exception(translate_error(
'SYS0339', $fieldid, $metadata[$fieldid][0][
'value'], $asset->id, $contextid, $e->getMessage()));
1067 }
catch (Exception $e) {
1069 throw new Exception(translate_error(
'SYS0340', $fieldid, $metadata[$fieldid][0][
'value'], $asset->id, $contextid, $e->getMessage()));
1078 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
1079 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
1082 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
1083 if ($update_asset) $asset->_updated();
1084 $em = $GLOBALS[
'SQ_SYSTEM']->getEventManager();
1086 $updated_fields = Array(
'fieldids' => array_keys($metadata_updated));
1087 $em->broadcastEvent($asset,
'MetadataUpdate', $broadcast_all_fields ? Array(
'all') : Array(
'fieldids' => array_keys($metadata)));
1088 $GLOBALS[
'SQ_SYSTEM']->broadcastTriggerEvent(
'trigger_event_metadata_updated', $asset, $updated_fields);
1115 if ($contextid === NULL) {
1116 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
1119 if ($contextid === 0) {
1120 $metadata_filename =
'metadata.php';
1122 $metadata_filename =
'metadata.'.$contextid.
'.php';
1126 if ($contextid ===
'default') {
1127 $regenerate = FALSE;
1128 $GLOBALS[
'SQ_SYSTEM']->changeContext(0);
1130 $GLOBALS[
'SQ_SYSTEM']->changeContext($contextid);
1133 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
1134 if (is_null($asset)) {
1135 trigger_localised_error(
'SYS0034', E_USER_WARNING, $assetid);
1136 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1140 $schemas = $this->
getSchemas($asset->id, TRUE);
1141 if (empty($schemas)) {
1142 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1147 $tag_values = Array();
1150 $keywords = Array();
1154 if ($contextid ===
'default') {
1155 $metadata = Array();
1161 foreach ($schemas as $schemaid) {
1162 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid);
1163 if (is_null($schema)) {
1164 trigger_localised_error(
'SYS0035', E_USER_WARNING, $schemaid);
1165 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1170 trigger_localised_error(
'SYS0033', E_USER_WARNING, $schema->name, $schemaid);
1171 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1175 $edit_fns = $schema->getEditFns();
1176 $edit_fns->generateMetadata($schema, $metadata, $tag_values, $keywords, $asset->type());
1178 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($schema);
1181 $metadata_str = ob_get_contents();
1185 echo
'<'.
'?php'.
"\n";
1186 if (!empty($keywords)) {
1187 require_once SQ_FUDGE_PATH.
'/general/text.inc';
1188 echo
'$am =& $GLOBALS[\'SQ_SYSTEM\']->am;'.
"\n";
1189 echo
'$metadata_asset = $am->getAsset('.$asset->id.
', \'\', TRUE);'.
"\n\n";
1190 echo
'/* 1 */',
"\n";
1191 echo
'$metadata_replacements = Array();'.
"\n";
1192 foreach ($keywords as $keyword) {
1193 if (!preg_match(
'/^metadata_field_/', $keyword)) {
1194 echo
'$metadata_replacements["'.addslashes($keyword).
'"] = $metadata_asset->getKeywordReplacement("'.addslashes($keyword).
'");'.
"\n";
1197 echo
'/* 2 */',
"\n";
1198 replace_keywords($tag_values, $replacements);
1199 echo
'/* 3 */',
"\n";
1215 $field_list = $tag_values;
1216 $replaced_list = Array();
1218 $field_replacements = Array();
1222 $fields_modified = FALSE;
1223 foreach ($field_list as $n => $v) {
1226 $value_keywords = retrieve_keywords_replacements($v);
1229 $metadata_keywords = Array();
1230 foreach ($value_keywords as $keyword) {
1231 if (preg_match(
'/^metadata_field_/', $keyword)) {
1232 $metadata_keywords[] = $keyword;
1235 $metadata_keywords = array_unique($metadata_keywords);
1238 if (empty($metadata_keywords)) {
1240 $replaced_list[$n] = $v;
1241 $field_replacements[
'metadata_field_'.$n] = $v;
1243 unset($field_list[$n]);
1245 $fields_modified = TRUE;
1249 foreach($metadata_keywords as $full_keyword) {
1250 if (!isset($field_replacements[$full_keyword])) {
1252 $part_keyword = parse_keyword($full_keyword, $modifiers);
1253 if ($part_keyword != $full_keyword && isset($field_replacements[$part_keyword])) {
1254 $keyword_value = $field_replacements[$part_keyword];
1255 apply_keyword_modifiers($keyword_value, $modifiers, Array(
'assetid' => $assetid));
1256 $field_replacements[$full_keyword] = $keyword_value;
1264 $pre_replacement = $field_list[$n];
1265 replace_keywords($field_list[$n], $field_replacements);
1268 if ($pre_replacement != $field_list[$n]) {
1271 $tag_values[$n] = $field_list[$n];
1272 $fields_modified = TRUE;
1277 }
while ($fields_modified);
1280 echo
'$metadata_values = Array('.
"\n";
1281 foreach ($tag_values as $n => $v) {
1285 $v2 = str_replace(
"'",
"\'", str_replace(
"\\",
"\\\\", $v2));
1289 $v2 = preg_replace(
'/%([^%\' ]+)%/',
'\'.(isset($metadata_replacements[\
'\1\']) ? $metadata_replacements[\'\1\'] : \'%\1%\' ).\'', $v2);
1290 echo
"'$n' => '$v2',\n";
1295 echo
'$metadata_value_components = Array('.
"\n";
1296 foreach ($tag_values as $n => $v) {
1301 echo
"'$n' => Array(";
1305 foreach ($vc as $vc_key => $vc_value) {
1308 echo
"'$vc_key' => '".$vc_value.
"', ";
1318 echo
'$mm = $GLOBALS[\'SQ_SYSTEM\']->getMetadataManager();'.
"\n";
1321 echo
'$metadata_warnings = Array();'.
"\n";
1324 if (!empty($field_list)) {
1325 echo
'$metadata_warnings[\'keyword_circular_references\'] = Array('.
"\n";
1326 foreach ($field_list as $n => $v) {
1334 $php_str = ob_get_contents();
1338 $metadata_str = $php_str.
"\n\n".$metadata_str;
1340 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
1341 if (!create_directory($asset->data_path)) {
1342 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1346 if (!string_to_file($metadata_str, $asset->data_path.
'/'.$metadata_filename)) {
1347 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1353 if (isset($asset->_tmp[
'mm_replacements'])) {
1354 unset($asset->_tmp[
'mm_replacements']);
1358 $GLOBALS[
'SQ_SYSTEM']->broadcastTriggerEvent(
'trigger_event_after_metadata_updated', $asset);
1364 $result = $this->
setMetadata($assetid, $metadata, $contextid, $update_asset, TRUE);
1368 $result = $this->_generateMetadataFieldFile($asset);
1371 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1392 private function _generateMetadataFieldFile(
Asset &$metadata_asset)
1394 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
1396 if ($contextid === 0) {
1397 $filename =
'metadata_field_values.php';
1399 $filename =
'metadata_field_values.'.$contextid.
'.php';
1402 $schemas = $this->
getSchemas($metadata_asset->id, TRUE);
1403 if (empty($schemas))
return FALSE;
1405 $metadata_field_values = $this->
getMetadata($metadata_asset->id);
1407 foreach ($schemas as $schema_id) {
1411 foreach ($default_values as $field_id => $field) {
1412 if (!isset($metadata_field_values[$field_id])) {
1413 $metadata_field_values[$field_id][0] = NULL;
1419 echo serialize($metadata_field_values);
1420 $metadata_str = ob_get_contents();
1423 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
1424 if (!create_directory($metadata_asset->data_path))
return FALSE;
1425 if (!string_to_file($metadata_str, $metadata_asset->data_path.
'/'.$filename)) {
1453 $metadata_replacements = Array();
1454 $keywords = array_unique($keywords);
1456 if (!empty($keywords) && $generating) {
1457 echo
'$metadata_replacements = Array();'.
"\n";
1460 $thumbnail_generated = FALSE;
1462 foreach ($keywords as $keyword) {
1465 $metadata_replacements[$keyword] =
'$metadata_replacements['.var_export($keyword, 1).
']';
1468 echo
'$am =& $GLOBALS[\'SQ_SYSTEM\']->am;'.
"\n";
1476 'asset_status_changed',
1479 foreach ($prefix as $name) {
1480 if (strpos($keyword, $name) === 0) {
1484 if ($keyword == $name) {
1485 $default_format =
'Y-m-d H:i:s';
1486 if ($keyword ==
'asset_published') {
1487 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = (is_null($metadata_asset->published)) ? \'Never\' : date(\''.$default_format.
'\', $metadata_asset->published);
'."\n";
1489 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = date(\
''.$default_format.
'\', $metadata_asset->
'.substr($keyword,6).');
'."\n";
1493 // Does the keyword match a certain date format? If so, replace
1494 // here; if not, then it will gets replaced further down (using date()
1496 $date_formats = get_date_formats();
1497 $format_name = substr($keyword, strlen($name.'_
'));
1498 if (array_key_exists($format_name, $date_formats)) {
1499 $format_expr = str_replace('/
', '
1500 if ($keyword ==
'asset_published') {
1501 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = (is_null($metadata_asset->published)) ? \'Never\' : format_date($metadata_asset->published, \''.$format_name.
'\');
'."\n";
1503 if ($format_name == 'iso8601
') {
1504 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = date(\
''.$format_expr.
'\', $metadata_asset->
'.substr($keyword, strlen('asset_
'), strlen($keyword) - strlen('_
'.$format_name) - strlen('asset_
')).').substr(date(\
'O\'),0,3).\':\'.substr(date(\'O\'),-2);'.
"\n";
1506 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = format_date($metadata_asset->'.substr($keyword, strlen(
'asset_'), strlen($keyword) - strlen(
'_'.$format_name) - strlen(
'asset_')).
', \''.$format_name.
'\');
'."\n";
1513 }//end foreach which loops through $prefix array
1515 $base_contextid = NULL;
1516 $full_keyword = $keyword;
1517 if (0 === strpos($keyword, 'asset_url_
')) {
1518 // get the context id required from the name specified in the keyword
1519 // if there is a '_
' in the context name, take it as a space. so, 'Default Context
' and 'Default_Context
' are treated as the same.
1520 $context_name = substr($keyword, 10);
1521 $context_name = str_replace('_
', ' ', $context_name);
1522 $context_data = MatrixDAL::executeAll('core
', 'getContextByName
', Array('name
' => Array($context_name)));
1524 // set the base contextid used later to get a correct URL and re-set the keyword without the context name to retrieve the replacement value later
1525 if (!empty($context_data)) {
1526 $base_contextid = $context_data[0]['contextid
'];
1527 $keyword = 'asset_url
';
1533 case 'asset_assetid
' :
1534 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = $metadata_asset->id;
'."\n";
1537 case 'asset_short_name
' :
1538 case 'asset_version
' :
1539 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = $metadata_asset->
'.substr($keyword,6).';
'."\n";
1542 // version cases: if the version is undefined (eg. shadow assets), then make it expand
1543 // something that would result in all version components returning a blank
1544 case 'asset_version_major
' :
1545 echo 'list($metadata_replacements[
'.var_export($keyword, 1).'],,) = explode(\
'.\',empty($metadata_asset->version) ? \'..\' : $metadata_asset->version);'.
"\n";
1548 case 'asset_version_minor' :
1549 echo
'list(,$metadata_replacements['.var_export($keyword, 1).
'],) = explode(\'.\',empty($metadata_asset->version) ? \'..\' : $metadata_asset->version);'.
"\n";
1552 case 'asset_version_micro' :
1553 echo
'list(,,$metadata_replacements['.var_export($keyword, 1).
']) = explode(\'.\',empty($metadata_asset->version) ? \'..\' : $metadata_asset->version);'.
"\n";
1557 echo
'$metadata_replacements['.var_export($full_keyword, 1).
'] = $metadata_asset->getURL(NULL, FALSE, $base_contextid);'.
"\n";
1560 case 'asset_name_linked' :
1561 echo
'$url = $metadata_asset->getURL();'.
"\n";
1562 echo
'$keyword = "";'.
"\n";
1563 echo
'if (!empty($url)) $keyword = "<a href=\"".$url."\">".$metadata_asset->name."</a>";'.
"\n";
1564 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = $keyword;'.
"\n";
1567 case 'asset_short_name_linked' :
1568 echo
'$url = $metadata_asset->getURL();'.
"\n";
1569 echo
'$keyword = "";'.
"\n";
1570 echo
'if (!empty($url) && isset($metadata_asset->short_name)) $keyword = "<a href=\"".$url."\">".$metadata_asset->short_name."</a>";'.
"\n";
1571 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = $keyword;'.
"\n";
1575 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = $metadata_asset->getHref();'.
"\n";
1578 case 'asset_thumbnail' :
1579 case 'asset_thumbnail_url' :
1580 case 'asset_thumbnail_width' :
1581 case 'asset_thumbnail_height' :
1582 case 'asset_thumbnail_alt' :
1583 case 'asset_thumbnail_caption' :
1584 case 'asset_thumbnail_assetid' :
1585 case 'asset_thumbnail_title' :
1586 case 'asset_thumbnail_name' :
1587 if (!$thumbnail_generated) {
1588 echo
'$thumbnail = null;';
1589 echo
'if ($metadata_asset->useSystemVersion()) {'.
"\n";
1590 echo
' $notice_links = unserialize(file_to_string($metadata_asset->data_path.\'/.sq_notice_links\'));'.
"\n";
1591 echo
' foreach ($notice_links as $link) {'.
"\n";
1592 echo
' if ($link[\'value\'] == \'thumbnail\') {'.
"\n";
1593 echo
' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\']);'.
"\n";
1594 echo
' break;'.
"\n";
1597 echo
'} else {'.
"\n";
1598 echo
' if ($metadata_asset->id) {';
1599 echo
' $link = $GLOBALS[\'SQ_SYSTEM\']->am->getLink($metadata_asset->id, SQ_LINK_NOTICE, \'image\', FALSE, \'thumbnail\');'.
"\n";
1600 echo
' if (!empty($link)) {'.
"\n";
1601 echo
' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\'], $link[\'minor_type_code\']);'.
"\n";
1607 case 'asset_thumbnail' :
1608 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = (is_null($thumbnail)) ? \'\' : $thumbnail->printImageTag(\'\', \'\', TRUE);'.
"\n";
1610 case 'asset_thumbnail_url' :
1611 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = (is_null($thumbnail)) ? \'\' : $thumbnail->getURL();'.
"\n";
1613 case 'asset_thumbnail_width' :
1614 case 'asset_thumbnail_height' :
1615 case 'asset_thumbnail_alt' :
1616 case 'asset_thumbnail_caption' :
1617 case 'asset_thumbnail_title' :
1618 case 'asset_thumbnail_name' :
1619 $sub_keyword = substr($keyword, 16);
1620 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = (is_null($thumbnail)) ? \'\' : $thumbnail->attr(\''.$sub_keyword.
'\');
'."\n";
1622 case 'asset_thumbnail_assetid
' :
1623 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = (is_null($thumbnail)) ? \
'\' : $thumbnail->id;
'."\n";
1628 case 'asset_created_by_assetid
' :
1629 case 'asset_created_by_url
' :
1630 case 'asset_created_by_name
' :
1631 case 'asset_updated_by_name
' :
1632 case 'asset_status_changed_by_name
':
1633 $pattern = '/^asset_(.*?)_by_(.*)$/
';
1634 preg_match($pattern, $keyword, $matches);
1635 $field_name = $matches[1];
1636 $user_member = $matches[2];
1637 switch ($user_member) {
1639 $user_member = 'id';
1642 $user_member = 'getURL()
';
1645 echo 'if (!isset($metadata_
'.$field_name.'_by_user) && !empty($metadata_asset->
'.$field_name.'_userid)) {
'."\n";
1646 echo ' $metadata_
'.$field_name.'_by_user = $GLOBALS[\
'SQ_SYSTEM\']->am->getAsset($metadata_asset->'.$field_name.
'_userid, \'\', TRUE);'.
"\n";
1648 echo
'if (!empty($metadata_'.$field_name.
'_by_user)) {'.
"\n";
1649 echo
' $metadata_replacements['.var_export($keyword, 1).
'] = $metadata_'.$field_name.
'_by_user->'.$user_member.
";\n";
1650 echo
'} else {'.
"\n";
1651 echo
' $metadata_replacements['.var_export($keyword, 1).
'] = \''.translate(
'unknown').
'\';
'."\n";
1655 case 'asset_created_by_first_name
' :
1656 case 'asset_created_by_last_name
' :
1657 case 'asset_created_by_email
' :
1658 case 'asset_updated_by_first_name
' :
1659 case 'asset_updated_by_last_name
' :
1660 case 'asset_updated_by_email
' :
1661 echo 'if (!isset($metadata_
'.substr($keyword,6,7).'_by_user) && !empty($metadata_asset->
'.substr($keyword,6,7).'_userid)) {
1662 $metadata_
'.substr($keyword,6,7).'_by_user = $GLOBALS[\
'SQ_SYSTEM\']->am->getAsset($metadata_asset->'.substr($keyword,6,7).
'_userid,\'\', TRUE);'.
"\n}\n";
1663 echo
'if (!empty($metadata_'.substr($keyword,6,7).
'_by_user)) { ';
1664 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = $metadata_'.substr($keyword,6,7).
'_by_user->attr(\''.substr($keyword,17).
'\');
'."\n";
1665 echo '}
else {
'."\n";
1666 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = \
''.translate(
'unknown').
'\';
'."\n";
1671 // can't handle
'last published' stuff in the same way as created/updated because
1673 case 'asset_published_by_name' :
1674 echo
'if (empty($metadata_asset->published_userid)) {'.
"\n";
1675 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = \'Never Published\';'.
"\n";
1676 echo
'} else { if (!isset($metadata_published_by_user)) {
1677 $metadata_published_by_user = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($metadata_asset->published_userid, \'\', TRUE);'.
"\n}\n";
1678 echo
'if (!empty($metadata_published_by_user)) { ';
1679 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = $metadata_published_by_user->name;'.
"\n";
1680 echo
'} else {'.
"\n";
1681 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = \''.translate(
'unknown').
'\';
'."\n";
1685 case 'asset_published_by_first_name
' :
1686 case 'asset_published_by_last_name
' :
1687 case 'asset_published_by_email
' :
1688 echo 'if (empty($metadata_asset->published_userid)) {
'."\n";
1689 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = \
'\';
'."\n";
1690 echo '}
else {
if (!isset($metadata_published_by_user)) {
1691 $metadata_published_by_user = $GLOBALS[\
'SQ_SYSTEM\']->am->getAsset($metadata_asset->published_userid, \'\', TRUE);'.
"\n}\n";
1692 echo
'if (!empty($metadata_published_by_user)) { ';
1693 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = $metadata_published_by_user->attr(\''.substr($keyword,19).
'\');
'."\n";
1694 echo '}
else {
'."\n";
1695 echo '$metadata_replacements[
'.var_export($keyword, 1).'] = \
''.translate(
'unknown').
'\';
'."\n";
1700 case 'asset_read_permission
' :
1701 case 'asset_write_permission
' :
1702 case 'asset_admin_permission
' :
1703 case 'asset_read_permission_assetid
' :
1704 case 'asset_write_permission_assetid
' :
1705 case 'asset_admin_permission_assetid
' :
1706 case 'asset_read_permission_email
' :
1707 case 'asset_write_permission_email
' :
1708 case 'asset_admin_permission_email
' :
1709 case 'asset_read_permission_email_linked
' :
1710 case 'asset_write_permission_email_linked
' :
1711 case 'asset_admin_permission_email_linked
' :
1712 $perm_type = preg_match('/asset_([^_]*)_.*$/
', $keyword, $matches);
1713 $perm_type = $matches[1];
1714 echo '$metadata_
'.$perm_type.'_perms = $am->getPermission($metadata_asset->id, SQ_PERMISSION_
'.strtoupper($perm_type).', TRUE, FALSE, TRUE);
'."\n";
1715 echo '$metadata_
'.$perm_type.'_perm_info = Array();
'."\n";
1716 echo 'foreach ($metadata_
'.$perm_type.'_perms as $userid) {
'."\n";
1717 echo ' $user = $am->getAsset($userid);
'."\n";
1718 echo ' if (is_null($user) || !$am->isTypeDecendant($user->type(), \
'user\')) {'.
"\n";
1719 echo
' continue;'.
"\n";
1721 if (preg_match(
'/.*_assetid$/', $keyword)) {
1722 echo
'$metadata_'.$perm_type.
'_perm_info[] = $user->id;'.
"\n";
1724 else if (preg_match(
'/.*_email$/', $keyword)) {
1725 echo
'$metadata_'.$perm_type.
'_perm_info[] = $user->attr(\'email\');'.
"\n";
1726 }
else if (preg_match(
'/.*_email_linked/', $keyword)) {
1727 echo
'$email = $user->attr(\'email\');'.
"\n";
1728 echo
'$metadata_'.$perm_type.
'_perm_info[] = \'<a href="mailto:\'.$email.\'">\'.$email.\'</a>\';'.
"\n";
1730 echo
'$metadata_'.$perm_type.
'_perm_info[] = $user->name;'.
"\n";
1733 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = implode(\', \', $metadata_'.$perm_type.
'_perm_info);'.
"\n";
1735 case 'system_name' :
1736 case 'system_owner' :
1737 case 'default_email' :
1739 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = SQ_CONF_'.strtoupper($keyword).
';'.
"\n";
1743 if (substr($keyword, 0, 11) ==
'asset_role_') {
1744 echo
'$metadata_roles_info = Array();'.
"\n";
1745 if (preg_match(
'/asset_role_([0-9]+)(.*)$/', $keyword, $matches)) {
1746 $fetch_global_roles = (SQ_CONF_ENABLE_GLOBAL_ROLES ==
'1');
1747 $role_assetid = $matches[1];
1748 echo
'$metadata_roles = $am->getRole($metadata_asset->id, '.$role_assetid.
', NULL, FALSE, '.(($fetch_global_roles)?
'TRUE':
'FALSE').
', TRUE);'.
"\n";
1749 echo
'if (!empty($metadata_roles)) {'.
"\n";
1750 echo
' foreach ($metadata_roles['.$role_assetid.
'] as $role_userid) {'.
"\n";
1751 echo
' $user = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($role_userid);'.
"\n";
1752 echo
' if (is_null($user) || !$am->isTypeDecendant($user->type(), \'user\')) {'.
"\n";
1753 echo
' continue;'.
"\n";
1755 echo
' $email = $user->attr(\'email\');'.
"\n";
1756 if (!empty($matches[2]) && $matches[2] ==
'_email') {
1757 echo
' $metadata_roles_info[] = $email;'.
"\n";
1758 }
else if (!empty($matches[2]) && $matches[2] ==
'_email_linked') {
1759 echo
' $metadata_roles_info[] = \'<a href="mailto:\'.$email.\'">\'.$email.\'</a>\';'.
"\n";
1761 echo
' $metadata_roles_info[] = $user->name;'.
"\n";
1763 echo
' $am->forgetAsset($user);'.
"\n";
1767 echo
' $metadata_replacements['.var_export($keyword, 1).
'] = implode(\', \', $metadata_roles_info);'.
"\n";
1770 }
else if (substr($keyword, 0, 15) ==
'metadata_field_') {
1773 $metadata_replacements[$keyword] =
'%'.$keyword.
'%';
1775 }
else if (substr($keyword, 0, 18) ==
'asset_thumbnail_v_') {
1777 $variety_name = substr($keyword, 18);
1778 if (!$thumbnail_generated) {
1779 echo
'$thumbnail = null;'.
"\n";
1780 echo
'if ($metadata_asset->useSystemVersion()) {'.
"\n";
1781 echo
' $notice_links = unserialize(file_to_string($metadata_asset->data_path.\'/.sq_notice_links\'));'.
"\n";
1782 echo
' foreach ($notice_links as $link) {'.
"\n";
1783 echo
' if ($link[\'value\'] == \'thumbnail\') {'.
"\n";
1784 echo
' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\']);'.
"\n";
1785 echo
' break;'.
"\n";
1788 echo
'} else {'.
"\n";
1789 echo
' if ($metadata_asset->id) {'.
"\n";
1790 echo
' $link = $GLOBALS[\'SQ_SYSTEM\']->am->getLink($metadata_asset->id, SQ_LINK_NOTICE, \'image\', FALSE, \'thumbnail\');'.
"\n";
1791 echo
' if (!empty($link)) {'.
"\n";
1792 echo
' $thumbnail = $GLOBALS[\'SQ_SYSTEM\']->am->getAsset($link[\'minorid\'], $link[\'minor_type_code\']);'.
"\n";
1798 echo
'$metadata_replacements['.var_export($keyword, 1).
'] = (is_null($thumbnail)) ? \'\' : $thumbnail->getKeywordReplacement(\'image_v_'.$variety_name.
'\');
';
1801 // searching for special date format keywords
1802 $original = $keyword;
1803 $keyword = substr($keyword, 6);
1804 $key = substr($keyword, 0, strpos($keyword, '_
'));
1805 $extras = substr($keyword, strpos($keyword, '_
') +1);
1807 if (strpos($extras, '_
') === FALSE) {
1811 list ($format, $offset) = explode('_
', $extras);
1813 $format = str_replace('~
', ' ', $format);
1819 if (empty($offset)) {
1820 $date = '$metadata_asset->
'.$key;
1822 $date = 'strtotime(\
''.$offset.
'\', $metadata_asset->
'.$key.')
';
1824 echo '$metadata_replacements[\
''.$original.
'\'] = date(\
''.$format.
'\',
'.$date.');
'."\n";
1833 eval(ob_get_contents());
1838 return $metadata_replacements;
1840 }//end generateKeywordReplacements()
1857 function regenerateMetadata($assetid, $contextid=NULL, $update_asset=TRUE)
1859 if ($contextid === NULL) {
1860 $contextid = $GLOBALS['SQ_SYSTEM
']->getContextId();
1863 if ($contextid === 'all
') {
1864 $all_contexts = $GLOBALS['SQ_SYSTEM
']->getAllContexts();
1865 $last_contextid = end(array_keys($all_contexts));
1868 // Since we're doing
this for all contexts, and search indexing events should ideally
1874 foreach ($all_contexts as $contextid => $context_data) {
1877 $GLOBALS[
'SQ_SYSTEM']->changeContext($contextid);
1878 $temp_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
1881 if ($contextid != $last_contextid) {
1882 $temp_asset->fastTrack(Array(
'search_manager_reindex_metadata',
'trigger_event_metadata_updated'));
1888 if ($contextid != $last_contextid) {
1889 $temp_asset->unFastTrack(Array(
'search_manager_reindex_metadata',
'trigger_event_metadata_updated'));
1892 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
1913 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
1914 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
1916 $bind_vars = Array (
1917 'assetid' => $assetid,
1921 }
catch (Exception $e) {
1922 throw new Exception(
'Unable to purge metadata schema for assetid #'.$assetid.
' due to the following database error:'.$e->getMessage());
1927 }
catch (Exception $e) {
1928 throw new Exception(
'Unable to purge metadata value for assetid #'.$assetid.
' due to the following database error:'.$e->getMessage());
1931 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
1932 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
1958 if (empty($assetid))
return NULL;
1963 if (!$mute_errors) {
1964 trigger_localised_error(
'SYS0308', E_USER_WARNING, $field_assetid);
1969 if ($contextid === NULL) {
1970 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
1974 $schemaid = $GLOBALS[
'SQ_SYSTEM']->am->getParents($field_assetid,
'metadata_schema');
1977 $schemas = $this->
getSchemas($assetid, TRUE);
1979 if (array_intersect($schemas, array_keys($schemaid)) == Array()) {
1981 if (!$mute_errors) {
1983 trigger_localised_error(
'SYS0307', E_USER_WARNING, $assetid, $field_assetid);
1991 '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_val mdv';
1992 $where =
'mdv.assetid = :assetid
1993 AND mdv.fieldid = :field_assetid
1994 AND mdv.contextid = :contextid';
1995 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'mdv');
2003 }
catch (Exception $e) {
2005 throw new Exception(translate_error(
'SYS0343', $field_assetid, $assetid, $contextid, $e->getMessage()));
2010 if ($value === FALSE) {
2042 if ($contextid === NULL) {
2043 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
2046 if ($contextid === 0) {
2047 $metadata_basename =
'metadata.php';
2049 $metadata_basename =
'metadata.'.$contextid.
'.php';
2056 if ($replace_nested_keyword) {
2059 $am =& $GLOBALS[
'SQ_SYSTEM']->am;
2060 $asset = $am->getAsset($assetid);
2061 $_metadata_filename = $asset->data_path.
'/'.$metadata_basename;
2064 if (is_file($_metadata_filename)) {
2067 if (isset($metadata[
'values'])) {
2068 $metadata_values = $metadata[
'values'];
2069 $field_names = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo(Array($field_assetid),
'metadata_field', FALSE,
'name');
2070 $value = $metadata_values[$field_names[$field_assetid]];
2075 if ($value ===
'') {
2080 if (!$has_errors && is_null($value)) {
2102 static $cached_tests = Array();
2104 if (!isset($cached_tests[$field_assetid])) {
2109 JOIN '.SQ_TABLE_RUNNING_PREFIX.
'ast a ON ati.type_code = a.type_code';
2110 $where =
'a.assetid = :field_assetid
2111 AND ati.inhd_type_code = :inhd_type_code';
2112 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'a');
2119 }
catch (Exception $e) {
2120 throw new Exception(
'Unable to determine whether the asset is a metadata field due to the following database error:'.$e->getMessage());
2123 $cached_tests[$field_assetid] = ($count > 0);
2126 return $cached_tests[$field_assetid];
2143 if ($contextid === NULL) {
2144 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
2149 trigger_localised_error(
'SYS0308', E_USER_WARNING, $field_assetid);
2155 if ($contextid != 0) {
2157 $asset = $GLOBALS[
'SQ_SYSTEM']->am->_asset_cache->get($contextid.
'\\'.$field_assetid);
2158 if (empty($asset)) {
2160 $deja_vu = $GLOBALS[
'SQ_SYSTEM']->getDejaVu();
2162 $asset = $deja_vu->recall(SQ_DEJA_VU_ASSET, $field_assetid);
2166 if (!empty($asset)) {
2167 $is_contextable = (boolean)$asset->attr(
'is_contextable');
2169 $is_contextable = $GLOBALS[
'SQ_SYSTEM']->am->getAttributeValuesByName(
'is_contextable',
'metadata_field', Array($field_assetid));
2170 $is_contextable = (boolean)$is_contextable[$field_assetid];
2173 if ($is_contextable === FALSE) {
2178 static $cached_defaults = Array();
2179 $recache_value = TRUE;
2180 if (in_array($contextid, array_keys($cached_defaults)) === TRUE) {
2181 if (in_array($field_assetid, array_keys($cached_defaults[$contextid])) === TRUE) {
2182 $recache_value = FALSE;
2187 if ($recache_value === TRUE) {
2191 '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_dflt_val mdv';
2192 $where =
'mdv.assetid = :field_assetid AND mdv.contextid = :contextid';
2193 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'mdv');
2200 }
catch (Exception $e) {
2201 throw new Exception(translate_error(
'SYS0335', $field_assetid, $contextid, $e->getMessage()));
2204 $cached_defaults[$contextid][$field_assetid] = $default_val;
2207 return $cached_defaults[$contextid][$field_assetid];
2225 if ($contextid === NULL) {
2226 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
2231 trigger_localised_error(
'SYS0308', E_USER_WARNING, $field_assetid);
2237 if ($contextid != 0) {
2239 $asset = $GLOBALS[
'SQ_SYSTEM']->am->_asset_cache->get($contextid.
'\\'.$field_assetid);
2240 if (empty($asset)) {
2242 $deja_vu = $GLOBALS[
'SQ_SYSTEM']->getDejaVu();
2244 $asset = $deja_vu->recall(SQ_DEJA_VU_ASSET, $field_assetid);
2248 if (!empty($asset)) {
2249 $is_contextable = (boolean)$asset->attr(
'is_contextable');
2251 $is_contextable = $GLOBALS[
'SQ_SYSTEM']->am->getAttributeValuesByName(
'is_contextable',
'metadata_field', Array($field_assetid));
2252 $is_contextable = (boolean)$is_contextable[$field_assetid];
2255 if ($is_contextable === FALSE) {
2263 '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_dflt_val mdv';
2264 $where =
'mdv.assetid = :field_assetid
2265 AND mdv.contextid = :contextid';
2266 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where,
'mdv');
2273 if (isset($row[0]) && !empty($row[0])) {
2276 }
catch (Exception $e) {
2278 throw new Exception(translate_error(
'SYS0336', $field_assetid, $contextid, $e->getMessage()));
2281 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
2282 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
2286 if (is_null($row) || empty($row)) {
2288 $message =
'SYS0336';
2289 $bind_vars = Array (
2290 'field_assetid' => $field_assetid,
2291 'contextid' => $contextid,
2297 $message =
'SYS0337';
2298 $bind_vars = Array (
2299 'field_assetid' => $field_assetid,
2300 'contextid' => $contextid,
2301 'default_val' => $value,
2305 }
catch (Exception $e) {
2306 throw new Exception(translate_error($message, $field_assetid, $contextid, $e->getMessage()));
2310 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
2311 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
2330 if ($escape_single) {
2331 $value = str_replace(
"'",
"\'", $value);
2336 $value = str_replace(
',',
',', $value);
2339 $value = htmlspecialchars($value);
2347 if (strtolower(SQ_CONF_DEFAULT_CHARACTER_SET) !=
'utf-8') {
2349 for ($i = 0; $i < strlen($value); ++$i) {
2350 $ord = ord($value[$i]);
2352 $result .=
'&#'.$ord.
';';
2354 $result .= $value[$i];
2359 return $result ? $result : $value;
2381 assert_valid_assetid($assetid);
2382 assert_true(is_string($target_field_name));
2386 $schemaids = $this->
getSchemas($assetid, TRUE);
2389 $field_assetids = array_keys($fields);
2390 $field_names = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($field_assetids,
'metadata_field', FALSE,
'name');
2395 $assetid = array_search($target_field_name, $field_names);
2420 $schemas = $this->
getSchemas($assetid, TRUE);
2421 if (empty($schemas))
return Array();
2423 $metadata_field_type = strtolower($metadata_field_type);
2424 $field_values = Array();
2426 foreach($schemas as $schemaid) {
2429 foreach($fields as $fieldid => $field_type) {
2430 if ($field_type[0][
'type_code'] != $metadata_field_type)
continue;
2431 $field_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($fieldid);
2432 if (empty($field_info))
continue;
2434 $field_values[$field_info[$fieldid][
'name']] = $this->
getMetadataValueByAssetid($assetid, $fieldid, FALSE, FALSE, $contextid);
2438 return $field_values;