snippet.personalize.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Personalize snippet for MODX Revolution
  4. *
  5. * Personalize is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option) any
  8. * later version.
  9. *
  10. * Personalize is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * Personalize; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * @package personalize
  19. */
  20. /**
  21. * MODX Personalize Snippet
  22. *
  23. *
  24. * @package personalize
  25. *
  26. * Properties
  27. *
  28. * @property yesChunk string (REQUIRED) Name of chunk or
  29. * inline HTML to show for LOGGED-in users
  30. *
  31. * @property noChunk string (optional) Name of chunk or
  32. * inline HTML to show for NOT logged-in users
  33. *
  34. * @property ph string (optional) Placeholder for placing
  35. * the username
  36. * ATTENTION!: ph placeholder will *not* be set in noChunk!
  37. *
  38. * @property fullName boolean (optional) Use full name
  39. * instead of username in placeholder
  40. *
  41. * @property ifIds string (optional) comma separated
  42. * list of users ids; yesChunk will only be shown
  43. * to users in the list
  44. *
  45. * @property allowedGroups string (optional) comma separated
  46. * list of allowed groups; yesChunk will only only shows
  47. * users are in one of these groups
  48. *
  49. * @property context (optional) context the user must be
  50. * logged in to to see the yesChunk; defaults to
  51. * current context.
  52. *
  53. *
  54. */
  55. /* Personalize Snippet for Revolution */
  56. /* ::::::::::::::::::::::::::::::::::::::::
  57. * Snippet Name: Personalize
  58. * Short Desc: returns a chunk if the user is logged in, otherwise calls another
  59. *::::::::::::::::::::::::::::::::::::::::
  60. * Description:
  61. * Checks to see if users are logged in and displays yesChunk if the user
  62. * is logged or noChunk if user is not logged. Insert only the chunk name as
  63. * param, without any tags. Can use a placeholder to output the username.
  64. *
  65. * TESTED: can be used more than once per page.
  66. * TESTED: chunks can contain snippets.
  67. *
  68. * Example Usage:
  69. *
  70. * [[!Personalize?
  71. * &yesChunk=`HelloUser`
  72. * &noChunk=`Register`
  73. * &ph=`name`
  74. * ]]
  75. *
  76. * Create Chunks named HelloUser and Register, the first will be
  77. * shown to a user logged on in the current context,
  78. * the second to other users.
  79. *
  80. * ADDED in 3.3.1 by Vasia123:
  81. *
  82. * 1. &noChunk=`@CODE:<b>Please login!</b>` - inline snippets
  83. * 2. &ifIds=`1,3` - additional check for users ids. yesChunk will
  84. * only be shown to logged-in users in the list.
  85. *
  86. * ADDED in 3.6.0 by Vasia123:
  87. *
  88. * 1. &allowedGroups=`Editor,Administrator` - check if user is
  89. * in any allowed group
  90. *
  91. *
  92. * Placeholder [[+name]] will show the user's name in the yesChunk or
  93. * elsewhere on the page.
  94. *
  95. *
  96. *:::::::::::::::::::::::::::::::::::::::: */
  97. /* @var $modx modX */
  98. /* @var $profile modUserProfile */
  99. /* Prepare params and variables */
  100. $output = '';
  101. $sp =& $scriptProperties;
  102. $yesChunk = $modx->getOption('yesChunk',$sp, null);
  103. $noChunk = $modx->getOption('noChunk',$scriptProperties, null);
  104. $fullName = (bool) $modx->getOption('fullName', $sp, false);
  105. $firstName = (bool) $modx->getOption('firstName', $sp, false);
  106. $ph = $modx->getOption('ph',$sp, null);
  107. $ifIds = $modx->getOption('ifIds',$sp, null);
  108. $allowedGroups = $modx->getOption('allowedGroups',$sp, null);
  109. $context = $modx->getOption('context', $sp, $modx->context->get('key') );
  110. if( $fullName || $firstName ) {
  111. $profile = $modx->user->getOne('Profile');
  112. }
  113. /* check if user logged in */
  114. $is_logged_in = $modx->user->hasSessionContext($context);
  115. /* Set $ifIds to true if user id is allowed or ifIds is not set */
  116. $ifIds = array_filter(array_map('trim',explode(',',$ifIds)));
  117. $ifIds = (!empty($ifIds)) ? in_array($modx->user->get('id'), $ifIds) : true;
  118. // Set $groups to true is use in in allowed group or $groups is not set
  119. $groups = array_filter(array_map('trim',explode(',',$allowedGroups)));
  120. if (!empty($groups)) {
  121. $inGroups = false;
  122. foreach($groups as $group) {
  123. if ($modx->user->isMember($group)) $inGroups = true;
  124. }
  125. } else {
  126. $inGroups = true;
  127. }
  128. /* Do the work */
  129. if ($is_logged_in && $ifIds && $inGroups) {
  130. if (preg_match('/^@CODE:/',$yesChunk)) {
  131. $output = substr($yesChunk, 6);
  132. } else {
  133. $output = $modx->getChunk($yesChunk, $scriptProperties);
  134. }
  135. if (! empty($ph)) {
  136. if ($fullName && $profile) {
  137. $name = trim($profile->get('fullname'));
  138. } elseif ($firstName && $profile) {
  139. $full = trim($profile->get('fullname'));
  140. $pos = strpos($full, ' ');
  141. $name = ($pos === false) ? $full : substr($full, 0, $pos);
  142. } else {
  143. $name = $modx->user->get('username');
  144. }
  145. if (empty($name)) {
  146. $name = $modx->user->get('username');
  147. }
  148. $modx->setPlaceholder($ph, $name);
  149. }
  150. } elseif( !empty ($noChunk) ) {
  151. if (preg_match('/^@CODE:/',$noChunk)) {
  152. $output = substr($noChunk, 6);
  153. } else {
  154. $output = $modx->getChunk($noChunk);
  155. }
  156. }
  157. return empty($output)? '': $output;