17 require_once dirname(__FILE__).
'/../../metadata_field/metadata_field.inc';
18 require_once SQ_FUDGE_PATH.
'/csv/csv.inc';
44 parent::__construct($assetid);
65 if ($name ===
'select_options') {
68 return parent::setAttrValue($name, $value);
81 require_once SQ_ATTRIBUTES_PATH.
'/selection/selection.inc';
84 $edit_params = $this->
attr(
'edit_params');
85 if (!empty($edit_params)) {
86 $selection->_edit_params = array_merge($selection->_edit_params, $edit_params);
90 $selection->_params[
'multiple'] = $this->
attr(
'multiple');
91 $selection->_params[
'allow_empty'] = $this->
attr(
'allow_empty');
93 $default = $this->
attr(
'default');
94 if ($this->
attr(
'multiple')) {
95 $default = explode(
'; ', $default);
97 if ($selection->validateValue($default)) {
98 $selection->setValue($default);
121 if ($this->
attr(
'visible_part') ===
'value') {
124 return str_replace(
'\\;',
';', parent::getMetadataValue($asset, $value_str));
140 $option_keys = explode(
'\\; ', $option_str);
141 if (empty($option_keys)) {
148 foreach ($option_keys as $key) {
149 $key = str_replace(
'\\;',
';', $key);
150 $key = str_replace(
'\\=',
'=', $key);
151 $key = str_replace(
'\\\\',
'\\', $key);
152 if (isset($options[$key])) {
153 $value_str .= $options[$key].
'; ';
156 if (strlen($value_str) == 0) {
162 $value_str = substr($value_str, 0, -2);
188 if (array_key_exists(
'cached_select_options', $this->_tmp) === FALSE) {
189 $this->_tmp[
'cached_select_options'] = Array();
192 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
194 if ($contextid === 0) {
197 return $this->
attr(
'select_options');
200 if (array_key_exists($contextid, $this->_tmp[
'cached_select_options']) === FALSE) {
202 $select_options = $this->
attr(
'select_options');
206 $default_options = $GLOBALS[
'SQ_SYSTEM']->am->getAttributeValuesByName(
'select_options',
'metadata_field_select', Array($this->
id), 0);
207 $default_options = unserialize($default_options[$this->
id]);
210 foreach ($select_options as $select_key => $select_value) {
211 if (array_key_exists($select_key, $default_options) === FALSE) {
212 unset($select_options[$select_key]);
218 foreach ($default_options as $default_key => &$default_value) {
219 if (array_key_exists($default_key, $select_options) === FALSE) {
220 $select_options[$default_key] = $default_value;
224 $this->_tmp[
'cached_select_options'][$contextid] = $select_options;
227 return $this->_tmp[
'cached_select_options'][$contextid];
246 if (array_key_exists(
'cached_select_options', $this->_tmp) === TRUE) {
247 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
249 if ($contextid === 0) {
250 $this->_tmp[
'cached_select_options'] = Array();
252 if (array_key_exists($contextid, $this->_tmp[
'cached_select_options']) === TRUE) {
253 unset($this->_tmp[
'cached_select_options'][$contextid]);
272 $option_keys = explode(
'\\; ', $option_str);
273 if (empty($option_keys))
return $option_keys;
276 $options = $this->
attr(
'select_options');
278 foreach ($option_keys as $key) {
279 foreach($options as $index => $element) {
280 if ($index === $key) {
281 $value_str .= $element.
'\\; ';
285 if (strlen($value_str) == 0) {
291 $value_str = substr($value_str, 0, -3);
323 if ($contextid === NULL) {
324 $contextid = $GLOBALS[
'SQ_SYSTEM']->getContextId();
327 switch ($additional_keyword) {
339 $sql =
'SELECT value FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_val ';
340 $where =
'assetid = :host_assetid AND fieldid = :fieldid AND contextid = :contextid';
341 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where);
345 $sub_sql =
'SELECT fieldid, contextid FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_val WHERE assetid = :host_assetid_1 ';
346 $sub_where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause(
'',
'ast_mdata_val');
348 $union_sql =
'SELECT default_val FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_mdata_dflt_val';
349 $union_where =
'%tuple_keyword%(assetid, contextid) NOT IN ('.$sub_sql.$sub_where.
') AND assetid = :fieldid_1 AND contextid = :contextid_1';
350 $union_where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($union_where,
'ast_mdata_dflt_val');
354 $union_where = str_replace(
'%tuple_keyword%',
'', $union_where);
356 $union_where = str_replace(
'%tuple_keyword%',
'ROW', $union_where);
358 $union_sql .= $union_where;
359 $sql =
'('.$sql.
') UNION ALL ('.$union_sql.
')';
363 'host_assetid' => (
string)$host_assetid,
364 'fieldid' => (
string)$this->
id,
365 'contextid' => (
int)$contextid,
366 'host_assetid_1' => (
string)$host_assetid,
367 'fieldid_1' => (
string)$this->
id,
368 'contextid_1' => (
int)$contextid,
372 foreach ($bind_vars as $bind_var => $bind_value) {
379 if ($key !== FALSE) {
380 if ($additional_keyword ===
'key') {
382 }
else if ($additional_keyword ===
'value') {
392 if (is_null($replacement)){
396 $value_components = $this->
attr(
'value_components');
418 $csv =
new CSV($filename);
421 if ($header == TRUE) {
424 $csv->import($start_line);
426 $values = $csv->values;
430 if (!empty($values)) {
433 $num_columns = count($values[0]);
436 foreach ($values as $row) {
439 if (trim($row[0]) ===
'')
continue;
440 if ($num_columns === 1) {
443 $value = htmlentities($row[0], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
444 $options[$index] = $value;
447 $key = htmlentities($row[0], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
448 $value = htmlentities($row[1], ENT_NOQUOTES, SQ_CONF_DEFAULT_CHARACTER_SET);
449 $options[$key] = $value;
451 if ($num_columns === 3) {
460 if ($num_columns === 3) {
461 if (!$this->
attr(
'multiple')) {
462 if (count($defaults) > 1) {
463 trigger_localised_error(
'CORE0309', E_USER_NOTICE);
465 if (count($defaults) > 0) {
466 $defaults = Array($defaults[0]);
472 $this->
setAttrValue(
'default', implode(
'; ', $defaults));
475 $current_default = $this->
attr(
'default');
476 if (!is_array($current_default)) {
477 $current_default = Array($current_default);
479 foreach ($current_default as $default_key) {
482 if (!isset($options[$default_key])) {
483 unset($current_default[$default_key]);
486 $this->
setAttrValue(
'default', implode(
'; ', $current_default));