17 require_once SQ_FUDGE_PATH.
'/db_extras/db_extras.inc';
81 var $components = Array();
91 if ((get_class($this) !== __CLASS__) && (get_parent_class($this) !== __CLASS__)) {
92 trigger_localised_error(
'SYS0274', E_USER_ERROR, get_class($this));
97 $this->_am = $GLOBALS[
'SQ_SYSTEM']->am;
115 $class = get_class($this);
116 return strtolower(substr($class, 0, strlen($class) - 11));
134 $asset_dir = SQ_SYSTEM_ROOT.
'/'.$this->_pm->getTypeInfo($type_code,
'dir');
135 require_once $asset_dir.
'/'.$type_code.
'.inc';
136 require_once $asset_dir.
'/'.$type_code.
'_edit_fns.inc';
138 $asset_xml_parent_type = $this->_pm->getTypeInfo($type_code,
'parent_type');
139 $asset_parent_type = strtolower(get_parent_class($type_code));
140 $asset_edit_fns_parent_type = preg_replace(
'/_edit_fns$/',
'', strtolower(get_parent_class($type_code.
'_edit_fns')));
142 if (($asset_xml_parent_type != $asset_parent_type) || ($asset_xml_parent_type != $asset_edit_fns_parent_type)) {
143 trigger_localised_error(
'SYS0254', E_USER_WARNING, $type_code, $type_code, $type_code);
148 if ($asset_xml_parent_type !=
'asset' && !$this->_am->installed($asset_xml_parent_type)) {
149 trigger_localised_error(
'SYS0253', E_USER_WARNING, $type_code, $asset_xml_parent_type);
154 if ($this->_am->installed($type_code)) {
155 if (!$this->
_upgrade($this->_am->getTypeInfo($type_code,
'version'))) {
159 if (!$this->
_install())
return false;
168 $from_icon_path = SQ_SYSTEM_ROOT.
'/'.$this->_pm->getTypeInfo($type_code,
'dir').
'/icon.png';
169 $to_icon_path = SQ_DATA_PATH.
'/public/asset_types/'.$type_code.
'/icon.png';
170 if (!is_file($from_icon_path)) {
172 $parent_type = $this->_pm->getTypeInfo($type_code,
'parent_type');
173 if ($parent_type ==
'asset') {
175 $from_icon_path = SQ_INCLUDE_PATH.
'/asset_edit/icon.png';
179 $from_icon_path = SQ_DATA_PATH.
'/public/asset_types/'.$parent_type.
'/icon.png';
183 if (!is_file($from_icon_path)) {
184 trigger_localised_error(
'CORE0068', E_USER_WARNING, $type_code);
186 if (!copy_file($from_icon_path, $to_icon_path)) {
206 if (empty($file_list))
return true;
208 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
210 $to_dir_path = SQ_DATA_PATH.
'/public/asset_types/'.$this->
getAssetType();
211 $from_dir_path = SQ_SYSTEM_ROOT.
'/'.$this->_pm->getTypeInfo($this->
getAssetType(),
'dir');
215 create_directory($to_dir_path);
218 for (reset($file_list); null !== ($k = key($file_list)); next($file_list)) {
219 $file = $file_list[$k];
220 $from = $from_dir_path.
'/'.$file;
221 $to = $to_dir_path.
'/'.$file;
225 if (!copy_file($from, $to))
return false;
243 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
244 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
247 $level = count($this->_pm->getTypeAncestors($type_code));
248 $info = $this->_pm->getTypeInfo($type_code);
252 $sql =
'INSERT INTO sq_ast_typ
253 (type_code, version, name, description, instantiable, allowed_access, parent_type, lvl, dir, customisation)
255 (:type_code, :version, :name, :description, :instantiable, :allowed_access, :parent_type, :lvl, :dir, :customisation)';
271 throw new Exception(
'Unable to add information for new asset type "'.$type_code.
'" due to database error: '.$e->getMessage());
275 $this->_am->refreshAssetType($type_code);
279 for (reset($all_vars); null !== ($k = key($all_vars)); next($all_vars)) {
280 $name = $all_vars[$k];
284 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
285 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
302 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
303 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
309 $level = count($this->_pm->getTypeAncestors($asset_type));
310 $info = $this->_pm->getTypeInfo($asset_type);
312 $sql =
'UPDATE sq_ast_typ
316 description = :description,
317 instantiable = :instantiable,
318 allowed_access = :allowed_access,
319 parent_type = :parent_type,
323 type_code = :type_code';
337 }
catch (Exception $e) {
338 throw new Exception(
'Unable to update type details while upgrading asset type "'.$asset_type.
'" to version '.$current_version.
', due to database error: ', $e->getMessage());
342 $this->_am->refreshAssetType($asset_type);
347 WHERE type_code = :type_code';
353 }
catch (Exception $e) {
354 throw new Exception(
'Unable to get current attributes while upgrading asset type "'.$asset_type.
'" to version '.$current_version.
', due to database error: ', $e->getMessage());
359 foreach ($all_vars as $name) {
361 if (in_array($name, $current_vars)) {
370 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
371 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
390 $sql =
'SELECT DISTINCT atr.name
391 FROM sq_ast_typ_inhd i, sq_ast_attr atr
392 WHERE i.type_code = :type_code_1
393 AND i.inhd_type_code <> :type_code_2
394 AND i.inhd_type_code = atr.type_code';
401 }
catch (Exception $e) {
402 throw new Exception(
'Unable to get attribute list for asset type "'.$asset_type.
'" due to database error: '.$e->getMessage());
406 $vars = array_values(array_unique(array_merge($parent_vars, array_keys($this->vars))));
450 $attr_info = Array();
452 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
453 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
457 $sql =
'SELECT COUNT(*) as count, MIN(at.lvl) as min, MAX(at.lvl) as max
458 FROM sq_ast_typ_inhd i, sq_ast_attr atr, sq_ast_typ at
459 WHERE i.type_code = :type_code
460 AND i.inhd_type_code <> :inhd_type_code
461 AND i.inhd_type_code = atr.type_code
463 AND atr.type_code = at.type_code';
471 $parent_info = $result[0];
473 }
catch (Exception $e) {
474 echo
'Unable to search for matching parent attribute while adding attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage();
475 throw new Exception(
'Unable to search for matching parent attribute while adding attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
479 if (empty($parent_info[
'count'])) {
482 $sql =
'SELECT a.type_code, a.type, a.attrid
483 FROM sq_ast_typ_inhd i, sq_ast_attr a
484 WHERE i.inhd_type_code = :type_code
485 AND i.type_code <> :inhd_type_code
486 AND i.type_code = a.type_code
494 }
catch (Exception $e) {
495 throw new Exception(
'Unable to search for matching child attribute while adding attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
499 if (!empty($children)) {
500 echo
'*** Attribute "'.$name.
'" is being moved from child types:'.
"\n";
501 foreach ($children as $child) {
503 if ($child[
'type'] != $this->vars[$name][
'type']) {
504 trigger_localised_error(
'SYS0233', E_USER_WARNING, $name, $type_code, implode(
'","', $children));
508 echo
'* '.$child[
'type_code'].
"\n";
511 $temp_attr_ids = implode(
', ', $temp_attr_ids);
514 $sql =
'UPDATE sq_ast_attr
515 SET owning_type_code = :type_code
516 WHERE attrid IN ('.$temp_attr_ids.
')';
521 }
catch (Exception $e) {
522 throw new Exception(
'Unable to update attribute\'s owning type while adding attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
525 echo
'*** Done'.
"\n\n";
529 $attr_info[
'owning_type_code'] = $type_code;
530 $attr_info[
'type'] = $this->vars[$name][
'type'];
531 $attr_info[
'default_type_code'] = $type_code;
532 $attr_info[
'default_val'] = $this->vars[$name][
'default'];
533 $attr_info[
'uniq'] = empty($this->vars[$name][
'uniq']) ? 0 : 1;
534 $attr_info[
'is_admin'] = isset($this->vars[$name][
'is_admin']) ? (int) $this->vars[$name][
'is_admin'] : 1;
535 $attr_info[
'is_contextable'] = isset($this->vars[$name][
'is_contextable']) ? (int) $this->vars[$name][
'is_contextable'] : 0;
536 $attr_info[
'parameters_type_code'] = $type_code;
538 if (isset($this->vars[$name][
'parameters'])) {
539 $attr_info[
'parameters'] = $this->vars[$name][
'parameters'];
541 $attr_info[
'parameters'] = Array();
544 $attr_info[
'description'] = empty($this->vars[$name][
'description']) ?
'' : $this->vars[$name][
'description'];
559 $sql =
'SELECT atr.type_code, atr.owning_type_code, atr.type, atr.uniq, atr.parameters_val,
560 atr.parameters_type_code, atr.default_type_code, atr.default_val, atr.description, atr.is_admin, atr.is_contextable
561 FROM sq_ast_typ_inhd i, sq_ast_attr atr, sq_ast_typ at
562 WHERE i.type_code = :type_code
563 AND i.inhd_type_code <> :inhd_type_code
564 AND i.inhd_type_code = atr.type_code
566 AND atr.type_code = at.type_code
569 $min_sql = $sql.
':min';
578 $attr_owner = $result[0];
580 }
catch (Exception $e) {
581 throw new Exception(
'Unable to search for matching parent attribute while adding attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
584 $attr_info[
'owning_type_code'] = $attr_owner[
'owning_type_code'];
585 $attr_info[
'type'] = $attr_owner[
'type'];
586 $attr_info[
'uniq'] = $attr_owner[
'uniq'];
587 $attr_info[
'is_admin'] = (int) $attr_owner[
'is_admin'];
588 $attr_info[
'is_contextable'] = (int) $attr_owner[
'is_contextable'];
589 $attr_info[
'description'] = $attr_owner[
'description'];
592 if (isset($this->vars[$name][
'default'])) {
593 $attr_info[
'default_type_code'] = $type_code;
594 $attr_info[
'default_val'] = $this->vars[$name][
'default'];
597 $max_sql = $sql.
':max';
605 $attr_default = $result[0];
607 }
catch (Exception $e) {
608 throw new Exception(
'Unable to search for matching parent attribute while adding attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
611 $attr_info[
'default_type_code'] = $attr_default[
'default_type_code'];
612 $attr_info[
'default_val'] = $attr_default[
'default_val'];
616 if (isset($this->vars[$name][
'parameters'])) {
617 $attr_info[
'parameters_type_code'] = $type_code;
618 $attr_info[
'parameters'] = $this->vars[$name][
'parameters'];
621 $max_sql = $sql.
':max';
629 $attr_default = $result[0];
631 }
catch (Exception $e) {
632 throw new Exception(
'Unable to search for matching parent attribute while adding attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
635 $attr_info[
'parameters_type_code'] = $attr_default[
'parameters_type_code'];
636 $attr_info[
'parameters'] = unserialize($attr_default[
'parameters_val']);
642 if (!$this->_am->validAttributeType($attr_info[
'type'])) {
643 trigger_localised_error(
'SYS0096', E_USER_WARNING, $attr_info[
'type'], $this->
getAssetType());
648 trigger_localised_error(
'SYS0130', E_USER_WARNING, $name, $this->
getAssetType());
655 'type_code' => $type_code,
656 'owning_type_code' => $attr_info[
'owning_type_code'],
658 'type' => $attr_info[
'type'],
659 'uniq' => $attr_info[
'uniq'],
660 'parameters_type_code' => $attr_info[
'parameters_type_code'],
661 'default_type_code' => $attr_info[
'default_type_code'],
662 'default_val' => $attr_info[
'default_val'],
663 'parameters_val' => serialize($attr_info[
'parameters']),
664 'is_admin' => $attr_info[
'is_admin'] ? 1 : 0,
665 'is_contextable' => $attr_info[
'is_contextable'] ? 1 : 0,
666 'description' => $attr_info[
'description'],
670 throw new Exception(
'Unable to add attribute "'.$name.
'" to asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
673 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
674 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
696 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
697 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
700 $sql =
'SELECT attrid FROM sq_ast_attr WHERE owning_type_code = :type_code AND name = :name';
706 if (is_null($attributeid)) {
707 trigger_localised_error(
'SYS0050', E_USER_WARNING, $typecode, $name);
712 $sql =
'DELETE FROM sq_ast_attr_val WHERE attrid = :attrid';
718 $sql =
'DELETE FROM sq_ast_attr WHERE attrid = :attrid';
724 if (isset($this->vars[$name])) {
725 unset($this->vars[$name]);
728 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
729 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
749 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
750 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
753 if (!empty($this->vars[$name][
'update_fn'])) {
754 $fn = $this->vars[$name][
'update_fn'];
755 if (!$this->$fn($current_version))
return false;
760 attrid, type_code, owning_type_code, name, type, uniq, parameters_val, parameters_type_code, default_type_code, default_val
764 type_code = :type_code
772 $curr_attr = $result[0];
774 }
catch (Exception $e) {
775 throw new Exception(
'Unable to load attribute info for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
779 if (!isset($this->_tmp[
'parent_asset_type'])) {
780 $parents = $this->_pm->getTypeAncestors($type_code);
781 $this->_tmp[
'parent_asset_type'] = array_shift($parents);
783 $parent = $this->_tmp[
'parent_asset_type'];
786 $sql =
'SELECT attrid, owning_type_code
788 WHERE type_code = :type_code
796 $parent_check_data = $result[0];
798 }
catch (Exception $e) {
799 throw new Exception(
'Unable to load parent type attribute info for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
805 $owning_type_code = null;
806 if (!empty($parent_check_data) && ($parent_check_data[
'owning_type_code'] != $curr_attr[
'owning_type_code'])) {
807 $owning_type_code = $parent_check_data[
'owning_type_code'];
808 }
else if (empty($parent_check_data) && ($curr_attr[
'owning_type_code'] != $type_code)) {
809 $owning_type_code = $type_code;
812 if (!empty($owning_type_code)) {
813 echo
'* Fixing invalid owner definition of attribute "'.$name.
'"'.
"\n";
815 $sql =
'UPDATE sq_ast_attr
816 SET owning_type_code = :owning_type_code
817 WHERE type_code = :type_code AND name = :name';
825 }
catch (Exception $e) {
826 throw new Exception(
'Unable to update owning type code info for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
829 $curr_attr[
'owning_type_code'] = $owning_type_code;
834 if ($curr_attr[
'owning_type_code'] == $type_code) {
844 $sql =
'SELECT owning_type_code, parameters_type_code, parameters_val, default_type_code, default_val
846 WHERE type_code = :type_code
854 $parent_attr = $result[0];
856 }
catch (Exception $e) {
857 throw new Exception(
'Unable to load parent type attribute info for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
864 if (isset($this->vars[$name][
'parameters'])) {
865 $parameters_value = serialize($this->vars[$name][
'parameters']);
866 $parameters_type_code = $type_code;
869 }
else if (!empty($parent_attr)) {
870 $parameters_value = $parent_attr[
'parameters_val'];
871 $parameters_type_code = $parent_attr[
'parameters_type_code'];
875 $parameters_value = serialize(Array());
876 $parameters_type_code = $type_code;
879 if ($parameters_type_code != $curr_attr[
'parameters_type_code'] || $parameters_value != $curr_attr[
'parameters_val']) {
880 if (!$this->
_updateCustomAttribute($curr_attr[
'attrid'], $name,
'parameters', $curr_attr[
'parameters_type_code'], $parameters_type_code, $parameters_value)) {
889 if (isset($this->vars[$name][
'default'])) {
890 $default_value = $this->vars[$name][
'default'];
891 $default_type_code = $type_code;
893 $parameters = unserialize($parameters_value);
895 trigger_localised_error(
'SYS0130', E_USER_WARNING, $name, $this->
getAssetType());
900 }
else if (!empty($parent_attr)) {
901 $default_value = $parent_attr[
'default_val'];
902 $default_type_code = $parent_attr[
'default_type_code'];
907 $default_type_code = $type_code;
910 if ($default_type_code != $curr_attr[
'default_type_code'] || $default_value != $curr_attr[
'default_val']) {
911 if (!$this->
_updateCustomAttribute($curr_attr[
'attrid'], $name,
'default', $curr_attr[
'default_type_code'], $default_type_code, $default_value)) {
920 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
921 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
939 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
940 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
946 if (!$this->_am->validAttributeType($this->vars[$name][
'type'])) {
947 trigger_localised_error(
'SYS0096', E_USER_WARNING, $this->vars[$name][
'type'], $this->
getAssetType());
951 $description = empty($this->vars[$name][
'description']) ?
'' : $this->vars[$name][
'description'];
954 $sql =
'UPDATE sq_ast_attr
957 is_admin = :is_admin,
958 is_contextable = :is_contextable,
959 description = :description
960 WHERE owning_type_code = :owning_type_code
968 MatrixDAL::bindValueToPdo($query,
'is_admin', isset($this->vars[$name][
'is_admin']) ? (
int) $this->vars[$name][
'is_admin'] : 1, PDO::PARAM_INT);
969 MatrixDAL::bindValueToPdo($query,
'is_contextable', isset($this->vars[$name][
'is_contextable']) ? (
int) $this->vars[$name][
'is_contextable'] : 0, PDO::PARAM_INT);
974 }
catch (Exception $e) {
975 throw new Exception(
'Unable to update owned attribute info for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
978 if (!$this->
_validateAttributeDefault($this->vars[$name][
'default'], $this->vars[$name][
'type'], (empty($this->vars[$name][
'parameters']) ? Array() : $this->vars[$name][
'parameters']))) {
979 trigger_localised_error(
'SYS0130', E_USER_WARNING, $name, $this->
getAssetType());
984 $sql =
'UPDATE sq_ast_attr
985 SET default_val = :val
986 WHERE default_type_code = :type_code
996 }
catch (Exception $e) {
997 throw new Exception(
'Unable to update attribute defaults for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
1000 if (!empty($this->vars[$name][
'parameters'])) {
1002 $sql =
'UPDATE sq_ast_attr
1003 SET parameters_val = :val
1004 WHERE parameters_type_code = :type_code
1013 }
catch (Exception $e) {
1014 throw new Exception(
'Unable to update attribute parameter info for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
1018 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
1019 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
1038 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
1039 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
1046 $sql =
'UPDATE sq_ast_attr
1047 SET '.$field.
'_type_code = :new_type_code,
1048 '.$field.
'_val = :value
1049 WHERE attrid = :attrid
1052 FROM sq_ast_typ_inhd i, sq_ast_attr atr
1053 WHERE i.inhd_type_code = :new_type_code_1
1054 AND i.type_code <> :new_type_code_2
1055 AND i.type_code = atr.type_code
1056 AND atr.'.$field.
'_type_code = :old_type_code
1057 AND atr.name = :name
1070 }
catch (Exception $e) {
1071 throw new Exception(
'Unable to update custom attribute value info for attribute "'.$name.
'" of asset type "'.$type_code.
'", due to database error: '.$e->getMessage());
1074 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
1075 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
1097 require_once SQ_ATTRIBUTES_PATH.
'/'.$attr_type.
'/'.$attr_type.
'.inc';
1098 $class =
'Asset_Attribute_'.$attr_type;
1099 $attr =
new $class();
1100 foreach ($parameters as $k => $v) {
1101 $attr->_params[$k] = $v;
1103 return $attr->validateValue($default);
1122 if (!isset($GLOBALS[
'SQ_ASSET_MANAGEMENT_ASSET_INFO'])) {
1123 $GLOBALS[
'SQ_ASSET_MANAGEMENT_ASSET_INFO'] = Array();
1126 if (!isset($GLOBALS[
'SQ_ASSET_MANAGEMENT_ASSET_INFO'][$file])) {
1129 $info =
new SimpleXMLElement($file, NULL, TRUE);
1130 }
catch (Exception $e) {
1131 throw new Exception(
'Could not parse asset definition file "'.$file.
'": '.$e->getMessage());
1134 $GLOBALS[
'SQ_ASSET_MANAGEMENT_ASSET_INFO'][$file] = Array(
1135 'type_code' => strtolower((
string)$info->type_code),
1136 'package' => (
string)$info->package,
1137 'name' => (
string)$info->name,
1138 'version' => (
string)$info->version,
1139 'description' => (
string)$info->description,
1140 'instantiable' => ((
string)$info->instantiable ===
'yes') ? 1 : 0,
1141 'allowed_access' => (
string)$info->allowed_access,
1142 'parent_type' => (
string)$info->parent_type,
1143 'flash_menu_path' => (isset($info->flash_menu_path)) ? (
string)$info->flash_menu_path :
'',
1148 return $GLOBALS[
'SQ_ASSET_MANAGEMENT_ASSET_INFO'][$file];
1162 $old_path = ini_get(
'include_path');
1163 ini_set(
'include_path', SQ_LIB_PATH);
1164 require_once SQ_LIB_PATH.
'/MatrixDAL/MatrixDALBaker.inc';
1166 if ($queries_found) {
1169 ini_set(
'include_path', $old_path);