Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_role.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
20 
33 {
34 
35 
55  public static function execute($settings, &$state)
56  {
57  $result = TRUE;
58  // if no settings, fail
59  if (empty($settings['roleid'])) return FALSE;
60 
61  if (!$GLOBALS['SQ_SYSTEM']->am->assetExists($settings['roleid'])) {
62  return FALSE;
63  }
64 
65  // check the optional settings, assign defaults
66  $roleid = array_get_index($settings, 'roleid', 0);
67  $role_action = array_get_index($settings, 'role_action', 'add');
68  $current_user = array_get_index($settings, 'use_current_user', FALSE);
69  $userids = array_get_index($settings, 'userid', Array());
70  $cascade = array_get_index($settings, 'cascade', FALSE);
71  $cascade_to_dependents = array_get_index($settings, 'cascade_to_dependents', FALSE);
72  $delete_all_before_adding = array_get_index($settings, 'delete_all_before_adding', FALSE);
73 
74  if (!is_array($userids)) $userids = Array ($userids);
75 
76  if($current_user) {
77  $userids = array_merge($userids, Array($GLOBALS['SQ_SYSTEM']->user->id));
78  }
79 
80  if (empty($state['asset'])) {
81  // grab the asset if assetid is given, but not the asset.
82  if (empty($state['assetid'])) {
83  return FALSE;
84  } else {
85  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
86  }
87  }
88 
89  $am =& $GLOBALS['SQ_SYSTEM']->am;
90 
91  $major = $am->getAsset($settings['roleid']);
92  $minor =& $state['asset'];
93 
94  $role_vars = Array();
95  switch ($role_action) {
96  case 'add':
97  $role_vars['add'][$roleid] = $userids;
98  break;
99 
100  case 'delete':
101  $role_vars['delete'][$roleid] = 0;
102  break;
103  }
104 
105  if($delete_all_before_adding) {
106  $current_roles = $GLOBALS['SQ_SYSTEM']->am->getRole($state['assetid']);
107  if(!empty($current_roles)) {
108  $GLOBALS['SQ_SYSTEM']->am->deleteRole($state['assetid'], $roleid);
109  }
110  }
111 
112  if (!empty($role_vars)) {
113  if ($cascade || $cascade_to_dependents) {
114  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
115  $vars = Array(
116  'assets' => Array(
117  $state['asset']->id => Array(
118  'type_code' => $state['asset']->type(),
119  ),
120  ),
121  'role_changes' => $role_vars,
122  );
123 
124  if (!$cascade) $vars['dependants_only'] = 1;
125  $errors = $hh->freestyleHIPO('hipo_job_edit_roles', $vars);
126  $result = empty($errors);
127  } else {
128  if (!empty($role_vars['add'])) {
129  foreach ($role_vars['add'] as $roleid => $userids) {
130  foreach ($userids as $userid) {
131  if (!$GLOBALS['SQ_SYSTEM']->am->setRole($state['assetid'], $roleid, $userid)) {
132  $result = FALSE;
133  break;
134  }
135  }
136  }
137  }
138 
139  if ($result && !empty($role_vars['delete'])) {
140  foreach ($role_vars as $roleid => $null) {
141  if (!$GLOBALS['SQ_SYSTEM']->am->deleteRole($state['assetid'], $roleid)) {
142  $result = FALSE;
143  break;
144  }
145  }
146  }
147 
148  }//end if ($cascade || $cascade_to_dependents)
149  }//end if (!empty($role_vars))
150 
151  if (!$result) {
152  return FALSE;
153  } else {
154  return Array(
155  'assetid' => $state['asset']->id,
156  'roleid' => $roleid,
157  'userids' => $userids,
158  'cascade' => $cascade,
159  'cascade_to_dependents' => $cascade_to_dependents,
160  );
161  }
162 
163  }//end execute()
164 
165 
176  public static function getInterface($settings, $prefix, $write_access=FALSE)
177  {
178  // check settings, set defaults if necessary
179  $selected_roleid = array_get_index($settings, 'roleid', 0);
180  $role_action = array_get_index($settings, 'role_action', 'add');
181  $selected_userids = array_get_index($settings, 'userid', Array());
182  $current_user = array_get_index($settings, 'use_current_user', FALSE);
183  $cascade = array_get_index($settings, 'cascade', FALSE);
184  $cascade_to_dependents = array_get_index($settings, 'cascade_to_dependents', FALSE);
185  $delete_all_before_adding = array_get_index($settings, 'delete_all_before_adding', FALSE);
186 
187  // check if the selected asset is valid
188  if ($selected_roleid && !$GLOBALS['SQ_SYSTEM']->am->assetExists($selected_roleid)) {
189  $selected_roleid = 0;
190  }
191 
192  ob_start();
193  if ($write_access) {
194  asset_finder($prefix.'[role]', $selected_roleid, Array('role' => 'I'));
195  } else {
196  if ($selected_roleid) {
197  $selected_role_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($selected_roleid, Array(), TRUE, 'name');
198  echo '<b>'.$selected_role_info[$selected_roleid].' (#'.$selected_roleid.')</b>';
199  } else {
200  echo '<b>['.translate('trigger_set_role_no_role_selected').']</b>';
201  }
202  }
203  $role_select_role_part = ob_get_contents();
204  ob_end_clean();
205 
206  ob_start();
207  if ($write_access) {
208  multiple_asset_finder($prefix.'[user_assetid]', $selected_userids, Array('user' => 'D', 'user_group' => 'D'));
209  echo '<i>AND / OR</i><br>';
210  echo check_box($prefix.'[use_current_user]', 1, $current_user);
211  label(translate('trigger_set_role_user_firing_trigger'), $prefix.'[use_current_user]');
212  echo '<br><br>';
213  } else {
214  if ($selected_userids || $current_user) {
215  if($selected_userids) {
216  $selected_assetname = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($selected_userids, Array(), TRUE, 'name');
217  echo '<ul>';
218  foreach ($selected_assetname as $assetid => $asset_name) {
219  echo '<li><b>'.$asset_name.' (#'.$assetid.')</b></li>';
220  }
221  echo '</ul>';
222  }
223  echo '<br />';
224  echo '<img src="'.sq_web_path('lib').'/web/images/'.($current_user ? 'tick' : 'cross').'.gif" alt="'.($current_user ? translate('yes') : translate('no')).'" /> ';
225  echo translate('trigger_set_role_user_firing_trigger');
226  echo '<br><br>';
227 
228  } else {
229  echo '<b>['.translate('trigger_set_role_no_users_selected').']</b>';
230  }
231  }
232  $role_select_user_part = ob_get_contents();
233  ob_end_clean();
234 
235  ob_start();
236  if ($write_access) {
237  check_box($prefix.'[cascade]', '1', $cascade);
238  label(translate('cascade_roles_option'), $prefix.'[cascade]');
239  echo '<br />';
240  check_box($prefix.'[cascade_to_dependents]', '1', $cascade_to_dependents);
241  label(translate('cascade_roles_to_dependents'), $prefix.'[cascade_to_dependents]');
242  } else {
243  echo '<img src="'.sq_web_path('lib').'/web/images/'.($cascade ? 'tick' : 'cross').'.gif" alt="'.($cascade ? translate('yes') : translate('no')).'" /> ';
244  echo translate('cascade_roles_option');
245 
246  echo '<br />';
247  echo '<img src="'.sq_web_path('lib').'/web/images/'.($cascade_to_dependents ? 'tick' : 'cross').'.gif" alt="'.($cascade_to_dependents ? translate('yes') : translate('no')).'" /> ';
248  echo translate('cascade_roles_to_dependents');
249  }
250  $role_cascade_part = ob_get_contents();
251  ob_end_clean();
252 
253  ob_start();
254  if ($write_access) {
255  check_box($prefix.'[delete_all_before_adding]', '1', $delete_all_before_adding);
256  label(translate('delete_all_before_adding'), $prefix.'[delete_all_before_adding]');
257  echo '<br />';
258  } else {
259  echo '<img src="'.sq_web_path('lib').'/web/images/'.($delete_all_before_adding ? 'tick' : 'cross').'.gif" alt="'.($delete_all_before_adding ? translate('yes') : translate('no')).'" /> ';
260  echo translate('delete_all_before_adding');
261  }
262  $role_delete_all_part = ob_get_contents();
263  ob_end_clean();
264 
265  $contents = ''.translate('select_a_role').'<br />'.
266  $role_select_role_part.'<br />'.
267  '<br />'.translate('select_users_for_role').'<br />'.
268  $role_select_user_part.'<br />'.
269  $role_cascade_part.'<br />'.
270  $role_delete_all_part;
271 
272  return $contents;
273 
274  }//end getInterface()
275 
276 
288  public static function processInterface(&$settings, $request_data)
289  {
290  $link_type_list = get_link_type_names();
291 
292  // make sure the assetid isn't blank
293  if (!empty($request_data['role']['assetid'])) {
294  $selected_roleid = $request_data['role']['assetid'];
295  } else {
296  return translate('no_roles_set');
297  }
298 
299  $raw_userids = $request_data['user_assetid'];
300  $userids = Array();
301  foreach ($raw_userids as $user) {
302  if (!empty($user['assetid'])) {
303  $userids[] = $user['assetid'];
304  }
305  }
306  $settings['use_current_user'] = array_get_index($request_data, 'use_current_user', FALSE);
307 
308  if (empty($userids) && !$settings['use_current_user']) {
309  return translate('select_users_for_role');
310  }
311 
312  // optional fields
313  $settings['role_action'] = array_get_index($request_data, 'role_action', 'add');
314  $settings['roleid'] = $selected_roleid;
315  $settings['userid'] = $userids;
316  $settings['cascade'] = array_get_index($request_data, 'cascade', FALSE);
317  $settings['delete_all_before_adding'] = array_get_index($request_data, 'delete_all_before_adding', FALSE);
318  if ($settings['cascade']) {
319  $settings['cascade_to_dependents'] = TRUE;
320  } else {
321  $settings['cascade_to_dependents'] = array_get_index($request_data, 'cascade_to_dependents', FALSE);
322  }
323 
324  return FALSE;
325 
326  }//end processInterface()
327 
328 
338  public static function getLocks($settings, &$state)
339  {
340  return Array(
341  $state['assetid'] => Array('all'),
342  );
343 
344  }//end getLocks()
345 
346 
347 }//end class
348 ?>