Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_remove_metadata_schema.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 
30 {
31 
32 
52  public static function execute($settings, &$state)
53  {
54  $am =& $GLOBALS['SQ_SYSTEM']->am;
55  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
56  $master_list = Array();
57 
58  // check required settings
59  if (empty($settings['schemaid']) && (!isset($settings['removeall']) && !$settings['removeall'])) {
60  return FALSE;
61  }
62 
63  if (empty($state['asset'])) {
64  // grab the asset if assetid is given, but not the asset.
65  if (empty($state['assetid'])) {
66  return FALSE;
67  } else {
68  $state['asset'] = $am->getAsset($state['assetid']);
69  }
70  }
71 
72  if (is_null($state['asset'])) return FALSE;
73 
74  $remove_all = array_get_index($settings, 'removeall', FALSE);
75  $cascade = array_get_index($settings, 'cascade', FALSE);
76 
77  // Get the master list of metadata schema from the parent asset
78  if ($remove_all) {
79  $master_list = $mm->getSchemas($state['asset']->id);
80  $schema = NULL;
81  } else {
82  $schema = $am->getAsset($settings['schemaid']);
83  }//end if
84 
85  // Error checking
86  // Test 1 - No schema and no remove all schema ie. nothing to do!
87  // Test 2 - No schema and remove all with no metadata schemas ie. again nothing to do!
88  if ((is_null($schema) || $schema->type() != 'metadata_schema') && !$remove_all) {
89  return TRUE;
90  } else if (empty($master_list) && $remove_all && (is_null($schema) || $schema->type() != 'metadata_schema')) {
91  return TRUE;
92  }
93 
94  if (!$cascade) {
95  if ($remove_all) {
96  foreach ($master_list as $schema_to_delete => $grant_return) {
97  $result[] = $mm->deleteSchema($state['asset']->id, $schema_to_delete);
98  }
99  } else {
100  $result = $mm->deleteSchema($state['asset']->id, $schema->id);
101 
102  }//end if
103  } else {
104  if ($remove_all) {
105  foreach ($master_list as $schema_to_delete => $grant_return) {
106  $schema_vars[] = Array(
107  'assetids' => Array($state['asset']->id),
108  'granted' => -1,
109  'schemaid' => $schema_to_delete,
110  'previous_access' => NULL,
111  'cascades' => TRUE,
112  );
113  }//end foreach
114  $vars = Array(
115  'assets' => Array(
116  $state['asset']->id => Array(
117  'type_code' => $state['asset']->type(),
118  ),
119  ),
120  'schema_changes' => $schema_vars,
121  );
122 
123  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
124  $errors = $hh->freestyleHIPO('hipo_job_edit_metadata_schemas', $vars);
125  $result = empty($errors);
126  } else {
127  $schema_vars[] = Array(
128  'assetids' => Array($state['asset']->id),
129  'granted' => -1,
130  'schemaid' => $settings['schemaid'],
131  'previous_access' => NULL,
132  'cascades' => TRUE,
133  );
134  $vars = Array(
135  'assets' => Array(
136  $state['asset']->id => Array(
137  'type_code' => $state['asset']->type(),
138  ),
139  ),
140  'schema_changes' => $schema_vars,
141  );
142 
143  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
144  $errors = $hh->freestyleHIPO('hipo_job_edit_metadata_schemas', $vars);
145  $result = empty($errors);
146  }//end if
147  }//end else
148 
149  // Hmm result is an array, let me double check
150  if (is_array($result)) {
151  $trigger_result = TRUE;
152  foreach ($result as $result_value) {
153  if (!$result_value) $trigger_result = FALSE;
154  }//end foreach
155 
156  // Return the result, so the trigger can finish
157  $result = $trigger_result;
158  }//end if
159 
160  if (!$result) {
161  return FALSE;
162  } else {
163  return Array(
164  'assetid' => $state['asset']->id,
165  'schemaid' => $settings['schemaid'],
166  'remove_all' => $remove_all,
167  'cascade' => $cascade,
168  );
169  }
170 
171  }//end execute()
172 
173 
184  public static function getInterface($settings, $prefix, $write_access=FALSE)
185  {
186  $current_schemaid = array_get_index($settings, 'schemaid', 0);
187  $remove_all = array_get_index($settings, 'removeall', 0);
188  $cascade = array_get_index($settings, 'cascade', 0);
189 
190  ob_start();
191  if ($write_access) {
192  echo translate('trigger_schema_remove_schema_message');
193  asset_finder($prefix.'[schemaid]', $current_schemaid, Array('metadata_schema' => 'I'));
194  echo '<br />';
195  check_box($prefix.'[removeall]', '1', $remove_all, 'if (this.checked == true) { if (!confirm("'.translate('trigger_schema_remove_all_schema_confirm_message').'")) { this.checked = false; } }');
196  label(translate('trigger_schema_remove_all_schema_message'));
197  echo '<br />';
198  check_box($prefix.'[cascade]', '1', $cascade);
199  label(translate('cascade_schema_changes'), $prefix.'[cascade]');
200 
201  } else {
202  if (!empty($current_schemaid)) {
203  $schema_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($current_schemaid));
204  echo translate('trigger_schema_remove_schema_message').' ';
205  echo '<b>'.$schema_info[$current_schemaid]['name'].' (#'.$current_schemaid.')</b>';
206  } else {
207  echo translate('trigger_schema_remove_schema_message').' ';
208  echo '<b>'.translate('trigger_schema_no_schema_message').'</b>';
209  }
210  echo '<br />';
211  echo '<img src="'.sq_web_path('lib').'/web/images/'.($remove_all ? 'tick' : 'cross').'.gif" alt="'.($remove_all ? translate('yes') : translate('no')).'" /> ';
212  echo translate('trigger_schema_remove_all_schema_message');
213  echo '<br />';
214  echo '<img src="'.sq_web_path('lib').'/web/images/'.($cascade ? 'tick' : 'cross').'.gif" alt="'.($cascade ? translate('yes') : translate('no')).'" /> ';
215  echo translate('cascade_schema_changes');
216  }
217 
218  $output = ob_get_contents();
219  ob_end_clean();
220 
221  return $output;
222 
223  }//end getInterface()
224 
225 
237  public static function processInterface(&$settings, $request_data)
238  {
239  $schema_info = array_get_index($request_data, 'schemaid', Array('assetid' => 0));
240  $settings['schemaid'] = array_get_index($schema_info, 'assetid', 0);
241  $settings['removeall'] = array_get_index($request_data, 'removeall', FALSE);
242  $settings['cascade'] = array_get_index($request_data, 'cascade', FALSE);
243  return FALSE;
244 
245  }//end processInterface()
246 
247 
257  public static function getLocks($settings, &$state)
258  {
259  return Array($state['assetid'] => Array('metadata'));
260 
261  }//end getLocks()
262 
263 
264 }//end class
265 
266 ?>