friendspicker.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * Elgg friends picker
  4. * Lists the friends picker
  5. *
  6. * @warning Below is the ugliest code in Elgg. It needs to be rewritten or removed
  7. *
  8. * @package Elgg
  9. * @subpackage Core
  10. *
  11. * @uses $vars['entities'] The array of ElggUser objects
  12. * @uses $vars['name']
  13. * @uses $vars['value']
  14. * @uses $vars['highlight']
  15. * @uses $vars['callback']
  16. */
  17. elgg_load_js('elgg.friendspicker');
  18. elgg_load_js('jquery.easing');
  19. $chararray = elgg_echo('friendspicker:chararray');
  20. // Initialise name
  21. if (!isset($vars['name'])) {
  22. $name = "friend";
  23. } else {
  24. $name = $vars['name'];
  25. }
  26. // Are we highlighting default or all?
  27. if (empty($vars['highlight'])) {
  28. $vars['highlight'] = 'default';
  29. }
  30. if ($vars['highlight'] != 'all') {
  31. $vars['highlight'] = 'default';
  32. }
  33. // Initialise values
  34. if (!isset($vars['value'])) {
  35. $vars['value'] = array();
  36. } else {
  37. if (!is_array($vars['value'])) {
  38. $vars['value'] = (int) $vars['value'];
  39. $vars['value'] = array($vars['value']);
  40. }
  41. }
  42. // Initialise whether we're calling back or not
  43. if (isset($vars['callback'])) {
  44. $callback = $vars['callback'];
  45. } else {
  46. $callback = false;
  47. }
  48. // We need to count the number of friends pickers on the page.
  49. if (!isset($vars['friendspicker'])) {
  50. global $friendspicker;
  51. if (!isset($friendspicker)) {
  52. $friendspicker = 0;
  53. }
  54. $friendspicker++;
  55. } else {
  56. $friendspicker = $vars['friendspicker'];
  57. }
  58. $users = array();
  59. $activeletters = array();
  60. // Are we displaying form tags and submit buttons?
  61. // (If we've been given a target, then yes! Otherwise, no.)
  62. if (isset($vars['formtarget'])) {
  63. $formtarget = $vars['formtarget'];
  64. } else {
  65. $formtarget = false;
  66. }
  67. // Sort users by letter
  68. if (is_array($vars['entities']) && sizeof($vars['entities'])) {
  69. foreach($vars['entities'] as $user) {
  70. $letter = elgg_strtoupper(elgg_substr($user->name, 0, 1));
  71. if (!elgg_substr_count($chararray, $letter)) {
  72. $letter = "*";
  73. }
  74. if (!isset($users[$letter])) {
  75. $users[$letter] = array();
  76. }
  77. $users[$letter][$user->guid] = $user;
  78. }
  79. }
  80. // sort users in letters alphabetically
  81. foreach ($users as $letter => $letter_users) {
  82. usort($letter_users, create_function('$a, $b', '
  83. return strcasecmp($a->name, $b->name);
  84. '));
  85. $users[$letter] = $letter_users;
  86. }
  87. if (!$callback) {
  88. ?>
  89. <div class="friends-picker-main-wrapper">
  90. <?php
  91. if (isset($vars['content'])) {
  92. echo $vars['content'];
  93. }
  94. ?>
  95. <div id="friends-picker_placeholder<?php echo $friendspicker; ?>">
  96. <?php
  97. }
  98. if (!isset($vars['replacement'])) {
  99. if ($formtarget) {
  100. ?>
  101. <?php //@todo JS 1.8: no ?>
  102. <script language="text/javascript">
  103. $(function() { // onload...do
  104. $('#collectionMembersForm<?php echo $friendspicker; ?>').submit(function() {
  105. var inputs = [];
  106. $(':input', this).each(function() {
  107. if (this.type != 'checkbox' || (this.type == 'checkbox' && this.checked != false)) {
  108. inputs.push(this.name + '=' + escape(this.value));
  109. }
  110. });
  111. jQuery.ajax({
  112. type: "POST",
  113. data: inputs.join('&'),
  114. url: this.action,
  115. success: function(){
  116. $('a.collectionmembers<?php echo $friendspicker; ?>').click();
  117. }
  118. });
  119. return false;
  120. })
  121. })
  122. </script>
  123. <!-- Collection members form -->
  124. <form id="collectionMembersForm<?php echo $friendspicker; ?>" action="<?php echo $formtarget; ?>" method="post"> <!-- action="" method=""> -->
  125. <?php
  126. echo elgg_view('input/securitytoken');
  127. echo elgg_view('input/hidden', array(
  128. 'name' => 'collection_id',
  129. 'value' => $vars['collection_id'],
  130. ));
  131. }
  132. ?>
  133. <div class="friends-picker-wrapper">
  134. <div id="friends-picker<?php echo $friendspicker; ?>">
  135. <div class="friends-picker-container">
  136. <?php
  137. // Initialise letters
  138. $chararray .= "*";
  139. $letter = elgg_substr($chararray, 0, 1);
  140. $letpos = 0;
  141. while (1 == 1) {
  142. ?>
  143. <div class="panel" title="<?php echo $letter; ?>">
  144. <div class="wrapper">
  145. <h3><?php echo $letter; ?></h3>
  146. <?php
  147. if (isset($users[$letter])) {
  148. ksort($users[$letter]);
  149. echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
  150. $col = 0;
  151. foreach($users[$letter] as $friend) {
  152. if ($col == 0) {
  153. echo "<tr>";
  154. }
  155. //echo "<p>" . $user->name . "</p>";
  156. $label = elgg_view_entity_icon($friend, 'tiny', array('use_hover' => false));
  157. $options[$label] = $friend->getGUID();
  158. if ($vars['highlight'] == 'all' && !in_array($letter,$activeletters)) {
  159. $activeletters[] = $letter;
  160. }
  161. if (in_array($friend->getGUID(),$vars['value'])) {
  162. $checked = "checked = \"checked\"";
  163. if (!in_array($letter,$activeletters) && $vars['highlight'] == 'default') {
  164. $activeletters[] = $letter;
  165. }
  166. } else {
  167. $checked = "";
  168. }
  169. ?>
  170. <td>
  171. <input type="checkbox" <?php echo $checked; ?> name="<?php echo $name; ?>[]" value="<?php echo $options[$label]; ?>" />
  172. </td>
  173. <td>
  174. <div style="width: 25px; margin-bottom: 15px;">
  175. <?php
  176. echo $label;
  177. ?>
  178. </div>
  179. </td>
  180. <td style="width: 200px; padding: 5px;">
  181. <?php echo $friend->name; ?>
  182. </td>
  183. <?php
  184. $col++;
  185. if ($col == 3){
  186. echo "</tr>";
  187. $col = 0;
  188. }
  189. }
  190. if ($col < 3) {
  191. echo "</tr>";
  192. }
  193. echo "</table>";
  194. }
  195. ?>
  196. </div>
  197. </div>
  198. <?php
  199. $substr = elgg_substr($chararray, elgg_strlen($chararray) - 1, 1);
  200. if ($letter == $substr) {
  201. break;
  202. }
  203. //$letter++;
  204. $letpos++;
  205. $letter = elgg_substr($chararray, $letpos, 1);
  206. }
  207. ?>
  208. </div>
  209. <?php
  210. if ($formtarget) {
  211. if (isset($vars['formcontents']))
  212. echo $vars['formcontents'];
  213. ?>
  214. <div class="clearfix"></div>
  215. <div class="friendspicker-savebuttons">
  216. <input type="submit" class="elgg-button elgg-button-submit" value="<?php echo elgg_echo('save'); ?>" />
  217. <input type="button" class="elgg-button elgg-button-cancel" value="<?php echo elgg_echo('cancel'); ?>" onclick="$('a.collectionmembers<?php echo $friendspicker; ?>').click();" />
  218. <br /></div>
  219. </form>
  220. <?php
  221. }
  222. ?>
  223. </div>
  224. </div>
  225. <?php
  226. } else {
  227. echo $vars['replacement'];
  228. }
  229. if (!$callback) {
  230. ?>
  231. </div>
  232. </div>
  233. <?php
  234. }
  235. if (!isset($vars['replacement'])) {
  236. ?>
  237. <?php //@todo JS 1.8: no ?>
  238. <script type="text/javascript">
  239. // initialise picker
  240. $("div#friends-picker<?php echo $friendspicker; ?>").friendsPicker(<?php echo $friendspicker; ?>);
  241. </script>
  242. <script type="text/javascript">
  243. $(document).ready(function () {
  244. // manually add class to corresponding tab for panels that have content
  245. <?php
  246. if (sizeof($activeletters) > 0)
  247. //$chararray = elgg_echo('friendspicker:chararray');
  248. foreach($activeletters as $letter) {
  249. $tab = elgg_strpos($chararray, $letter) + 1;
  250. ?>
  251. $("div#friends-picker-navigation<?php echo $friendspicker; ?> li.tab<?php echo $tab; ?> a").addClass("tabHasContent");
  252. <?php
  253. }
  254. ?>
  255. });
  256. </script>
  257. <?php
  258. }