]> projects.mako.cc - scuttle/blob - templates/dynamictags.inc.php
initial patch to cause scuttle to work with PHP 8.2
[scuttle] / templates / dynamictags.inc.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2005 - 2010 Marcus Campbell
4 http://scuttle.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ***************************************************************************/
20
21 $sf = new ServiceFactory();
22 $tagservice  =& $sf->getServiceInstance('TagService');
23 $userservice =& $sf->getServiceInstance('UserService');
24
25 $logged_on_userid = $userservice->getCurrentUserId();
26
27 $userPopularTags        =& $tagservice->getPopularTags($logged_on_userid, 25, $logged_on_userid);
28 $userAllTags            =& $tagservice->getPopularTags($logged_on_userid, 10000, $logged_on_userid);
29 $userPopularTagsCloud   =& $tagservice->tagCloud($userPopularTags, 5, 90, 175);
30 $userPopularTagsCount   = count($userPopularTags);
31
32 if ($userPopularTagsCount > 0) {
33 ?>
34
35 <script type="text/javascript">
36 Array.prototype.contains = function (ele) {
37     for (var i = 0; i < this.length; i++) {
38         if (this[i] == ele) {
39             return true;
40         }
41     }
42     return false;
43 };
44
45 Array.prototype.remove = function (ele) {
46     var arr = new Array();
47     var count = 0;
48     for (var i = 0; i < this.length; i++) {
49         if (this[i] != ele) {
50             arr[count] = this[i];
51             count++;
52         }
53     }
54     return arr;
55 };
56
57 function addonload(addition) {
58     var existing = window.onload;
59     window.onload = function () {
60         existing;
61         addition;
62     }
63 }
64
65 addonload(
66     function () {
67         var taglist = document.getElementById('tags');
68         var tags = taglist.value.split(', ');
69         
70         var populartags = document.getElementById('popularTags').getElementsByTagName('span');
71         
72         for (var i = 0; i < populartags.length; i++) {
73             if (tags.contains(populartags[i].innerHTML)) {
74                 populartags[i].className = 'selected';
75             }
76         }
77     }
78 );
79
80 function addTag(ele) {
81     var thisTag = ele.innerHTML;
82     var taglist = document.getElementById('tags');
83     var tags = taglist.value.split(', ');
84     
85     // If tag is already listed, remove it
86     if (tags.contains(thisTag)) {
87         tags = tags.remove(thisTag);
88         ele.className = 'unselected';
89         
90     // Otherwise add it
91     } else {
92         tags.splice(0, 0, thisTag);
93         ele.className = 'selected';
94     }
95     
96     taglist.value = tags.join(', ');
97     
98     document.getElementById('tags').focus();
99 }
100
101 document.write('<div class="collapsible">');
102 document.write('<h3><?php echo T_('Popular Tags'); ?><\/h3>');
103 document.write('<p id="popularTags" class="tags">');
104
105 <?php
106 $taglist = '';
107 foreach(array_keys($userPopularTagsCloud) as $key) {
108     $row =& $userPopularTagsCloud[$key];
109     $entries = T_ngettext('bookmark', 'bookmarks', $row['bCount']);
110     $taglist .= '<span title="'. $row['bCount'] .' '. $entries .'" style="font-size:'. $row['size'] .'" onclick="addTag(this)">'. filter($row['tag']) .'<\/span> ';
111 }
112 ?>
113
114 document.write('<?php echo $taglist ?>');
115 document.write('<\/p>');
116 document.write('<\/div>');
117
118 var availableTags = [
119 <?php
120
121 foreach(array_keys($userAllTags) as $key) {
122   print '"'.$userAllTags[$key]['tag'].'",'."\n";
123 }
124 ?>
125 ];
126 function split( val ) {
127   return val.split( /,\s*/ );
128 }
129 function extractLast( term ) {
130   return split( term ).pop();
131 }
132
133 $( "#tags" )
134   // don't navigate away from the field on tab when selecting an item
135   .bind( "keydown", function( event ) {
136     if ( event.keyCode === $.ui.keyCode.TAB &&
137         $( this ).data( "autocomplete" ).menu.active ) {
138       event.preventDefault();
139     }
140   })
141   .autocomplete({
142     minLength: 2,
143     delay: 50,
144     source: function( request, response ) {
145       // if the term is >2 in legth delegate back to autocomplete
146       if (extractLast ( request.term ).length < 3) {
147         response( [] );
148       } else {
149         response( $.ui.autocomplete.filter(
150           availableTags, extractLast( request.term ) ) );
151       }
152     },
153     focus: function() {
154       // prevent value inserted on focus
155       return false;
156     },
157     select: function( event, ui ) {
158       var terms = split( this.value );
159       // remove the current input
160       terms.pop();
161       // add the selected item
162       var newterm = ui.item.value;
163       terms.push( newterm );
164
165       $("#popularTags").children().each(function() {
166         if (this.innerHTML == newterm) {
167           this.className = 'selected';
168         }
169       });
170       // add placeholder to get the comma-and-space at the end
171       terms.push( "" );
172       this.value = terms.join( ", " );
173       return false;
174     }
175 });
176
177
178 </script>
179
180 <?php } ?>

Benjamin Mako Hill || Want to submit a patch?