Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_user_group_manager.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
20 
34 {
35 
36  var $bodycopies = Array(
37  'logged_in' => 'Logged In Layout',
38  'not_logged_in' => 'Not Logged In Layout',
39  );
40 
41  var $keywords = Array(
42  'logged_in' => Array(
43  'error_messages_list',
44  'success_messages_list',
45  'location_picker',
46  'user_name',
47  'user_short_name',
48  'user_email',
49  'commit_button',
50  ),
51  'not_logged_in' => Array(
52  'login_form',
53  ),
54  );
55 
56  var $errors;
57 
58  var $messages;
59 
60 
67  function __construct($assetid=0)
68  {
69  $this->_ser_attrs = TRUE;
70  parent::__construct($assetid);
71 
72  }//end constructor
73 
74 
85  function _createAdditional(&$link)
86  {
87  if (!parent::_createAdditional($link)) return FALSE;
88 
89  // add a bodycopy for the Page contents, and the format of locations
90  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bodycopy');
91 
92  foreach ($this->bodycopies as $value => $name) {
93  $copy_link = Array(
94  'asset' => &$this,
95  'link_type' => SQ_LINK_TYPE_2,
96  'is_dependant' => 1,
97  'is_exclusive' => 1,
98  'value' => $value,
99  );
100 
101  $bodycopy = new Bodycopy();
102  $bodycopy->setAttrValue('name', $name);
103  $args = Array('content' => $this->_getDefaultBodycopyContents($value));
104  if (!$bodycopy->create($copy_link, $args)) {
105  return FALSE;
106  }
107  }
108  return TRUE;
109 
110  }//end _createAdditional()
111 
112 
122  {
123  switch ($code) {
124  case 'logged_in':
125  return '<h2>'.translate('cms_user_group_manager_title', '%user_name%').'</h2>'."\n".
126  '%error_messages_list%%success_messages_list%'."\n".
127  '<p>%location_picker%%commit_button%</p>';
128  case 'not_logged_in':
129  return translate('cms_user_group_manager_must_be_logged_in');
130  }
131  return '';
132 
133  }//end _getDefaultBodycopyContents()
134 
135 
142  function printBody()
143  {
144  // if the page is being submitted, a slow DB replication could mean that
145  // the printed bodycopy contents may not immediately match the newly
146  // committed data if done on db1... so tying the process and print
147  // together, IF the form is being submitted
148  if (isset($_REQUEST['submitted'])) {
149  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
150  }
151 
152  // Process any operations. Deleting, New links etc.
153  $this->processForm();
154  ?>
155  <form id="<?php echo $this->getPrefix(); ?>" action="<?php echo current_url(); ?>" method="post">
156  <input type="hidden" name="submitted" value="1" />
157  <?php
158  // Print the form and details again
159  echo $this->printBodycopyContents();
160  ?></form><?php
161 
162  if (isset($_REQUEST['submitted'])) {
163  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
164  }
165 
166  }//end printBody()
167 
168 
175  function processForm()
176  {
177  $user_groups = Array();
178  $link_type = $this->attr('link_type');
179 
180  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($GLOBALS['SQ_SYSTEM']->currentUserId(), $link_type, '', FALSE, 'minor');
181  // only allow links we have write access to
182  $valid_nodes = array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($this->attr('root_node'), 'user_group', FALSE, NULL, NULL, SQ_PERMISSION_WRITE));
183 
184  $current_groups = Array();
185  foreach ($current_links as $id => $link) {
186  if (in_array($link['majorid'], $valid_nodes)) {
187  $current_groups[] = $link['majorid'];
188  }
189  }
190 
191  if (isset($_REQUEST['submitted'])) {
192  if (isset($_REQUEST[$this->getPrefix().'_location_selection'])) {
193  $user_groups = $_REQUEST[$this->getPrefix().'_location_selection'];
194 
195  if ($this->attr('allow_multiple') == FALSE) {
196  // Change the single entry into an array
197  $user_groups = !empty($user_groups) ? Array($user_groups) : Array();
198  }
199 
200  $this->_tmp['selected'] = $user_groups;
201  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
202  $done = $this->processMinorLinks($GLOBALS['SQ_SYSTEM']->currentUserId(), $current_groups, $user_groups, $link_type);
203  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
204 
205  $this->generateMessages($done);
206  }
207  }
208 
209  return TRUE;
210 
211  }//end processForm()
212 
213 
226  function processMinorLinks($minor_id, $old_links=Array(), $new_links=Array(), $link_type, $link_value=NULL)
227  {
228  $cannot_create = Array();
229  $cannot_delete = Array();
230  $to_delete = array_diff($old_links, $new_links);
231  $to_create = array_diff($new_links, $old_links);
232  $minor = $GLOBALS['SQ_SYSTEM']->am->getAsset($minor_id);
233 
234  $done = Array();
235 
236  // loop through and link our minor under all majors
237  foreach ($to_create as $majorid) {
238  $major = $GLOBALS['SQ_SYSTEM']->am->getAsset($majorid);
239  if (!$GLOBALS['SQ_SYSTEM']->am->createAssetLink($major, $minor, $link_type)) {
240  $done['add_fail'][] = $major->id;
241  } else {
242  $done['add_success'][] = $major->id;
243  }
244 
245  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($major);
246  }
247 
248  // handle links marked to be removed
249  foreach ($to_delete as $majorid) {
250  $curr_link = $GLOBALS['SQ_SYSTEM']->am->getLinkByAsset($majorid, $minor_id, $link_type, $link_value);
251  if (!empty($curr_link)) {
252  $link_id = $curr_link['linkid'];
253 
254  if (!$GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($link_id)) {
255  $done['delete_fail'][] = $majorid;
256  } else {
257  $done['delete_success'][] = $majorid;
258  }
259  }
260  }
261 
262  // cleanup
263  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($minor);
264 
265  return $done;
266 
267  }//end processMinorLinks()
268 
269 
279  function generateMessages($fail_success)
280  {
281  foreach ($fail_success as $message_type => $assetids) {
282  switch ($message_type) {
283  case 'add_fail':
284  $this->errors[] = 'Unable to add this user to the following groups: '.$this->_getCommaSeparatedNamesByAssetid($assetids);
285  break;
286  case 'add_success':
287  $this->messages[] = 'Successfully added this user to the following groups: '.$this->_getCommaSeparatedNamesByAssetid($assetids);
288  break;
289  case 'delete_fail':
290  $this->errors[] = 'Unable to remove this user from the following groups: '.$this->_getCommaSeparatedNamesByAssetid($assetids);
291  break;
292  case 'delete_success':
293  $this->messages[] = 'Successfully removed this user from the following groups: '.$this->_getCommaSeparatedNamesByAssetid($assetids);
294  break;
295  }
296  }
297 
298  }//end generateMessages()
299 
300 
309  function printAvailableLocations($suffix='_location_selection')
310  {
311  // populate our list
312  $type_codes = Array('user_group' => '1');
313  $full_types = Array();
314  foreach ($type_codes as $type => $inherit) {
315  $full_types[] = $type;
316  if ($inherit == '1') {
317  $full_types += $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants($type);
318  }
319  }
320  $prefix = $this->getPrefix();
321  ob_start();
322  $root_node = $this->attr('root_node');
323  $default = array_get_index($this->_tmp, 'selected', Array());
324  // if it is a public user, this function should return locations that public user can read
325  // else this function should return locations that current user has write permissions
326  $current_user = $GLOBALS['SQ_SYSTEM']->user;
327  if ($current_user instanceof Public_User) {
328  $_permission = SQ_PERMISSION_READ;
329  } else {
330  $_permission = SQ_PERMISSION_WRITE;
331  }
332  structured_drop_down($prefix.$suffix, $root_node, $full_types, $default, $this->attr('structured_width'), $this->attr('structured_height'), $this->attr('max_depth'), $this->attr('allow_multiple'), $this->attr('seperator'), $this->attr('include_root_node'), '', $this->attr('structured_style'), $this->attr('statuses'), TRUE, Array(), FALSE, $_permission);
333  $selector = ob_get_contents();
334  ob_end_clean();
335  return $selector;
336 
337  }//end printAvailableLocations()
338 
339 
347  function _getAllowedLinks()
348  {
349  return Array(SQ_LINK_TYPE_2 => Array('bodycopy' => Array('card' => 'M', 'exclusive' => FALSE)));
350 
351  }//end _getAllowedLinks()
352 
353 
366  function &getBodycopyContents($name, $replacements=Array())
367  {
368  $bodycopy = $this->getBodycopy($name);
369  if (is_null($bodycopy)) return '';
370 
371  $bodycopy->setKeywordReplacements($replacements);
372 
373  ob_start();
374  $bodycopy->printBody();
375  $html = ob_get_contents();
376  ob_end_clean();
377 
378  return $html;
379 
380  }//end getBodycopyContents()
381 
382 
393  function &getBodycopy($name)
394  {
395  if (array_key_exists($name, $this->bodycopies)) {
396  $am = $GLOBALS['SQ_SYSTEM']->am;
397  $link = $am->getLink($this->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE, $name, 'major', '1');
398  $bodycopy = $am->getAsset($link['minorid'], $link['minor_type_code']);
399  return $bodycopy;
400  } else {
401  return NULL;
402  }
403 
404  }//end getBodycopy()
405 
406 
417  function onRequestKeywords(&$broadcaster, $vars=Array())
418  {
419  $vars['keywords'] = isset($vars['keywords']) ? $vars['keywords'] : Array();
420 
421  // Get any bodycopy parents of the broadcaster
422  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($broadcaster->id, 'bodycopy', TRUE);
423  $bodycopy_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_2, 'bodycopy');
424  $chosen_bodycopy = '';
425  foreach ($bodycopy_links as $id => $link) {
426  if (in_array($link['minorid'], array_keys($parents))) {
427  $chosen_bodycopy = $link['value'];
428  break;
429  }
430  }
431  $keys = Array();
432  if (!empty($chosen_bodycopy)) {
433  foreach ($this->keywords[$chosen_bodycopy] as $key_word) {
434  $keys[$key_word] = ucwords(str_replace('_', ' ', $key_word));
435  }
436  }
437  $vars['keywords'] = array_merge($vars['keywords'], $keys);
438 
439  }//end onRequestKeywords()
440 
441 
450  {
451  $public_user = $GLOBALS['SQ_SYSTEM']->getPublicUser();
452  if ($GLOBALS['SQ_SYSTEM']->currentUser($public_user)) {
453  $bodycopy = $this->getBodycopy('not_logged_in');
454  } else {
455  $bodycopy = $this->getBodycopy('logged_in');
456  }
457 
458  $keywords = $bodycopy->getKeywords();
459  $replacements = Array();
460  foreach ($keywords as $name) {
461  $replacements[$name] = $this->getKeywordReplacement($name);
462  }
463  $bodycopy->setKeywordReplacements($replacements);
464 
465  ob_start();
466  $bodycopy->printBody();
467  $html = ob_get_contents();
468  ob_end_clean();
469 
470  return $html;
471 
472  }//end printBodycopyContents()
473 
474 
485  {
486  $string = '';
487  $names = Array();
488  foreach ($assetids as $assetid) {
489  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
490  $names[] = $asset->name;
491  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
492  }
493  $string .= implode(', ', $names);
494 
495  return $string;
496 
497  }//end _getCommaSeparatedNamesByAssetid()
498 
499 
507  {
508  return $this->printAvailableLocations();
509 
510  }//end getLocationPickerKeywordReplacement()
511 
512 
520  {
521  $user = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($GLOBALS['SQ_SYSTEM']->currentUserId()));
522  return $user[$GLOBALS['SQ_SYSTEM']->currentUserId()]['name'];
523 
524  }//end getUserNameKeywordReplacement()
525 
526 
534  {
535  $user = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($GLOBALS['SQ_SYSTEM']->currentUserId()));
536  return $user[$GLOBALS['SQ_SYSTEM']->currentUserId()]['short_name'];
537 
538  }//end getUserShortNameKeywordReplacement()
539 
540 
548  {
549  $current_user = $GLOBALS['SQ_SYSTEM']->user;
550  $current_user_email = $current_user->attr('email');
551  return $current_user_email;
552 
553  }//end getUserEmailKeywordReplacement()
554 
555 
564  {
565  return $GLOBALS['SQ_SYSTEM']->currentUserId();
566 
567  }//end getUserIdKeywordReplacement()
568 
569 
578  {
579  ob_start();
580  submit_button($this->getPrefix().'_submit_button', $this->attr('submit-button'));
581  $button = ob_get_contents();
582  ob_end_clean();
583  return $button;
584 
585  }//end getCommitButtonKeywordReplacement()
586 
587 
595  {
596  return $this->_generateMessageList($this->errors, '_errors');
597 
598  }//end getErrorMessagesListKeywordReplacement()
599 
600 
608  {
609  return $this->_generateMessageList($this->messages, '_success');
610 
611  }//end getSuccessMessagesListKeywordReplacement()
612 
613 
623  function _generateMessageList($messages, $suffix)
624  {
625  $string = '';
626  if (!empty($messages)) {
627  $string .= '<ul>';
628  foreach ($messages as $message) {
629  $string .= '<li class="'.$this->getPrefix().'_'.$suffix.'">'.$message.'</li>';
630  }
631  $string .= '</ul>';
632  }
633  return $string;
634 
635  }//end _generateMessageList()
636 
637 
645  {
646  ob_start();
647  ?>
648  <input type="hidden" name="SQ_LOGIN_KEY" value="<?php echo $GLOBALS['SQ_SYSTEM']->generateLoginKey(); ?>" />
649  <input type="hidden" name="SQ_LOGIN_REFERER" value="" />
650 
651  <table border="0">
652  <tr>
653  <td valign="top"><?php echo translate('username'); ?></td>
654  <td valign="top"><?php echo text_box('SQ_LOGIN_USERNAME', (isset($_POST['SQ_LOGIN_USERNAME'])) ? $_POST['SQ_LOGIN_USERNAME'] : 'Enter your username here', 25, 0, (isset($_POST['SQ_LOGIN_USERNAME'])) ? FALSE : TRUE, 'title="Username"'); ?></td>
655  </tr>
656  <tr>
657  <td valign="top"><?php echo translate('password'); ?></td>
658  <td valign="top"><?php echo password_box('SQ_LOGIN_PASSWORD', '', 25, 0, 'title="Password"'); ?></td>
659  </tr>
660  <tr>
661  <td valign="top">&nbsp;</td>
662  <td valign="top">
663  <input type="hidden" name="SQ_ACTION" value="" id="SQ_ACTION" />
664  <?php echo submit_button('SQ_LOGIN_SUBMIT', 'Login', 'document.getElementById(\'SQ_ACTION\').value = \'login\'; document.getElementById(\''.$this->id.'ASSET_BUILDER_ACTION\').value = \'login\';'); ?>
665  </td>
666  </tr>
667  </table>
668 
669  <?php
670  $login_form = ob_get_contents();
671  ob_end_clean();
672  return $login_form;
673 
674  }//end getLoginFormKeywordReplacement()
675 
676 
677 }//end class
678 
679 ?>