Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
lookupValues.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 
39 function paintLookupValues(&$owner, &$o, &$ei, $keywords=NULL)
40 {
41 
42  $prefix = $owner->getPrefix();
43  $lookups = $owner->getLookups('url');
44  $lookup_values = $owner->getLookupValues(TRUE);
45 
46  $am =& $GLOBALS['SQ_SYSTEM']->am;
47 
48  $lookup_value_types = $am->getTypeDescendants('lookup_value');
49  $lookup_value_details = Array();
50 
51  // need to group the values by their value type
52  $values = Array();
53  foreach ($lookup_value_types as $type_code) {
54  $am->includeAsset($type_code);
55  eval ('$name_prefix = '.$type_code.'::namePrefix();');
56  eval ('$title = '.$type_code.'::lookupValueName();');
57  $lookup_value_details[$type_code] = Array(
58  'name_prefix' => $name_prefix,
59  'title' => $title,
60  );
61  $values[$name_prefix] = Array();
62  }
63 
64  uasort($lookup_value_details, create_function('$a,$b','return strcmp(strtolower($a[\'title\']), strtolower($b[\'title\']));'));
65 
66  foreach ($lookup_values as $url => $data) {
67  foreach ($data as $value_name => $value_data) {
68  $matches = Array();
69  if (!preg_match('/^([^:]+)(::|$)/', $value_name, $matches)) {
70  continue;
71  }
72  if (!isset($values[$matches[1]])) continue; // unknown prefix..
73  if (!isset($values[$matches[1]][$url])) {
74  $values[$matches[1]][$url] = Array();
75  }
76  $values[$matches[1]][$url][$value_name] = $value_data;
77  }
78  }
79  unset($lookup_values);
80 
81  $editing_fields = FALSE;
82 
83  for (reset($lookup_value_details); NULL !== ($type_code = key($lookup_value_details)); next($lookup_value_details)) {
84  $o->openSection($lookup_value_details[$type_code]['title']);
85  $name_prefix = $lookup_value_details[$type_code]['name_prefix'];
86  eval('$editing_fields |= '.$type_code.'::paintInterface($owner, $o, $lookups, $values[$name_prefix], $prefix.\'_lookup_values_\'.$name_prefix);');
87  $o->closeSection();
88 
89  }// end foreach
90 
91  return $editing_fields;
92 
93 }//end paintLookupValues()
94 
95 
107 function processLookupValues(&$owner, &$o, &$ei)
108 {
109  $prefix = $owner->getPrefix();
110  $lookups = $owner->getLookups('url');
111  // get only the lookup values that we have set specifically for ourselves
112  $lookup_values = $owner->getLookupValues(FALSE);
113 
114  $am =& $GLOBALS['SQ_SYSTEM']->am;
115 
116  $lookup_value_types = $am->getTypeDescendants('lookup_value');
117 
118  $layouts_changed = FALSE;
119  $layouts_ok = TRUE;
120 
121  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
122  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
123  foreach ($lookup_value_types as $type_code) {
124  $am->includeAsset($type_code);
125  eval('$name_prefix = '.$type_code.'::namePrefix();');
126  $values = Array();
127  eval('$success = '.$type_code.'::processInterface($owner, $o, $lookups, $values, $prefix.\'_lookup_values_\'.$name_prefix);');
128 
129  if (!$success) {
130  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
131  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
132  return FALSE;
133  }
134 
135  foreach ($values as $resource => $values_data) {
136  foreach ($values_data as $value_name => $data) {
137  if ($resource === 'asset') {
138  // asset layout definition
139  $layout_name = $value_name.'::system::frontend';
140  $existing_layout_link = $GLOBALS['SQ_SYSTEM']->am->getLink($owner->id, SQ_LINK_NOTICE, 'paint_layout_page', TRUE, $layout_name);
141  $create_new = $delete_existing = FALSE;
142  if (empty($data['value'])) {
143  $delete_existing = TRUE;
144  } else if (empty($existing_layout_link)) {
145  // a layout notice link does not currently exist so create it
146  $create_new = TRUE;
147  } else {
148  // notice link currently exists, only create a new notice link if necessary
149  if ($existing_layout_link['minorid'] !== $data['value']) {
150  $delete_existing = TRUE;
151  $create_new = TRUE;
152  }
153  }
154 
155  if ($delete_existing) {
156  if ($existing_layout_link) {
157  if (!$owner->deleteLink($existing_layout_link['linkid'])) {
158  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
159  $layouts_ok = FALSE;
160  break;
161  } else {
162  $layouts_changed = TRUE;
163  }
164 
165  // also need to delete any other URLs which use the same value
166  // deleting an asset based value means all other URLs which use the same value are also deleted
167  if (!$new_values = $owner->deleteLookupValue($layout_name, $existing_layout_link['minorid'])) {
168  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
169  $layouts_ok = FALSE;
170  break;
171  } else {
172  $lookup_values = $new_values;
173  unset($new_values);
174  }
175  }
176  }
177 
178  if ($create_new) {
179  // create new notice link
180  $layout = $GLOBALS['SQ_SYSTEM']->am->getAsset($data['value']);
181  if (is_null($layout)) {
182  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
183  $layouts_ok = FALSE;
184  break;
185  }
186  if (!$owner->createLink($layout, SQ_LINK_NOTICE, $layout_name)) {
187  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
188  $layouts_ok = FALSE;
189  break;
190  } else {
191  $layouts_changed = TRUE;
192  }
193  }
194 
195  continue;
196  }//end if
197 
198  // delete if no value
199  if (empty($data['value'])) {
200  if (isset($lookup_values[$resource][$value_name])) {
201  unset($lookup_values[$resource][$value_name]);
202  }
203  // if value is deleted, run updateLookups() so the asset based definition can take effect if there is one defined
204  $layouts_changed = TRUE;
205  } else {
206  if (!isset($lookup_values[$resource])) {
207  $lookup_values[$resource] = Array();
208  }
209  $lookup_values[$resource][$value_name] = Array('value' => $data['value']);
210  }
211  }
212  }
213 
214  }// end foreach
215 
216  if (!$owner->setLookupValues($lookup_values)) {
217  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
218  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
219  return FALSE;
220  }
221  if ($layouts_ok) {
222  if ($layouts_changed) {
223  if (!$owner->updateLookups()) {
224  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
225  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
226  return FALSE;
227  }
228  }
229  }
230 
231  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
232  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
233 
234  return TRUE;
235 
236 }//end processLookupValues()
237 
238 
239 ?>