/[drupal]/contributions/modules/jsnippets/jsnippets.module
ViewVC logotype

Contents of /contributions/modules/jsnippets/jsnippets.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Mon Mar 2 19:58:08 2009 UTC (8 months, 4 weeks ago) by karthik
Branch: MAIN
CVS Tags: DRUPAL-6--1-1-ALPHA1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.7: +23 -26 lines
File MIME type: text/x-php
D6 migration: Get JS working.
1 <?php
2 // $Id: jsnippets.module,v 1.7 2009/03/02 18:47:14 karthik Exp $
3
4 /* TODO Automatically add Drupal.settings.basePath
5 In Drupal 5, you would have to add the base path to Drupal.settings yourself
6 if you needed it (it's needed for just about every AHAH/AJAX enabled module
7 if you did it right). Now in Drupal 6, it's added automatically. You can always
8 find it at Drupal.settings.basePath (actually, as soon as drupal_add_js() is
9 called at least once, so this is similar to the way we automatically add
10 drupal.js and jquery.js. */
11
12 /**
13 * @file
14 * Manage jsnippets.
15 */
16
17 /**
18 * Implementation of hook_help().
19 */
20 function jsnippets_help($path, $arg) {
21 switch ($path) {
22 case 'admin/help#jsnippets':
23 $output = t('<p>The JSnippet module allows the storage and later, the
24 insertion of text snippets or templates into textareas of your choice.
25 This eases mundane copy and paste jobs as well as making available
26 consistent templates for your use.</p>');
27
28 return $output;
29 }
30 }
31
32 /**
33 * Implementation of hook_menu().
34 */
35 function jsnippets_menu() {
36 $access = array('administer jsnippets');
37
38 $items['admin/build/jsnippets'] = array(
39 'title' => 'JSnippets',
40 'description' => 'Create and manage text snippets.',
41 'page callback' => 'jsnippets_overview',
42 'access arguments' => $access,
43 'file' => 'jsnippets.admin.inc'
44 );
45 $items['admin/build/jsnippets/overview'] = array(
46 'title' => 'Overview',
47 'page callback' => 'jsnippets_overview',
48 'access arguments' => $access,
49 'weight' => -1,
50 'type' => MENU_DEFAULT_LOCAL_TASK,
51 'file' => 'jsnippets.admin.inc'
52 );
53
54 $items['admin/build/jsnippets/section/add'] = array(
55 'title' => 'Add section',
56 'page callback' => 'drupal_get_form',
57 'page arguments' => array('jsnippets_section_edit'),
58 'access arguments' => $access,
59 'type' => MENU_LOCAL_TASK,
60 'file' => 'jsnippets.admin.inc'
61 );
62 $items['admin/build/jsnippets/section/edit/%'] = array(
63 'title' => 'Edit section',
64 'page callback' => 'drupal_get_form',
65 'page arguments' => array('jsnippets_section_edit', 5),
66 'access arguments' => $access,
67 'type' => MENU_CALLBACK,
68 'file' => 'jsnippets.admin.inc'
69 );
70 $items['admin/build/jsnippets/section/delete/%'] = array(
71 'title' => 'Delete section',
72 'page callback' => 'drupal_get_form',
73 'page arguments' => array('jsnippets_section_delete', 5),
74 'access arguments' => $access,
75 'type' => MENU_CALLBACK,
76 'file' => 'jsnippets.admin.inc'
77 );
78 $items['admin/build/jsnippets/section/view/%'] = array(
79 'title' => 'View section',
80 'page callback' => 'jsnippets_section_view',
81 'page arguments' => array(5),
82 'access arguments' => $access,
83 'type' => MENU_CALLBACK,
84 'file' => 'jsnippets.admin.inc'
85 );
86
87 $items['admin/build/jsnippets/add'] = array(
88 'title' => 'Add snippet',
89 'page callback' => 'drupal_get_form',
90 'page arguments' => array('jsnippets_jsnippet_edit'),
91 'access arguments' => $access,
92 'type' => MENU_LOCAL_TASK,
93 'file' => 'jsnippets.admin.inc',
94 'weight' => 1
95 );
96 $items['admin/build/jsnippets/edit/%'] = array(
97 'title' => 'Edit snippet',
98 'page callback' => 'drupal_get_form',
99 'page arguments' => array('jsnippets_jsnippet_edit', 4),
100 'access arguments' => $access,
101 'type' => MENU_CALLBACK,
102 'file' => 'jsnippets.admin.inc'
103 );
104 $items['admin/build/jsnippets/delete/%'] = array(
105 'title' => 'Delete snippet',
106 'page callback' => 'drupal_get_form',
107 'page arguments' => array('jsnippets_jsnippet_delete', 4),
108 'access arguments' => $access,
109 'type' => MENU_CALLBACK,
110 'file' => 'jsnippets.admin.inc'
111 );
112 $items['admin/build/jsnippets/view/%'] = array(
113 'title' => 'View snippet',
114 'page callback' => 'drupal_get_form',
115 'page arguments' => array('jsnippets_jsnippet_view', 4),
116 'access arguments' => $access,
117 'type' => MENU_CALLBACK,
118 'file' => 'jsnippets.admin.inc'
119 );
120
121 // XMLHTTP snippet retrieval.
122 $items['jsnippets/retrieve/%'] = array(
123 'title' => 'Retrieve snippet',
124 'page callback' => 'jsnippets_retrieve',
125 'page arguments' => array(2),
126 'access arguments' => array('use jsnippets'),
127 'type' => MENU_CALLBACK
128 );
129
130 return $items;
131 }
132
133 /**
134 * Implementation of hook_perm().
135 */
136 function jsnippets_init() {
137 drupal_add_css(drupal_get_path('module', 'jsnippets') .'/jsnippets.css');
138 }
139
140 /**
141 * Implementation of hook_perm().
142 */
143 function jsnippets_perm() {
144 return array('administer jsnippets', 'use jsnippets');
145 }
146
147 /**
148 * Implementation of hook_form_alter().
149 */
150 function jsnippets_form_alter(&$form, &$form_state, $form_id) {
151 if (user_access('use jsnippets')) {
152 _jsnippets_add_jsnippets_by_id($form_id);
153
154 // @TODO: This block of code used to live in hook_footer. Might have issues
155 // with cached pages, besides multiple form_ids.
156 $jsnippets = _jsnippets_js_array();
157 if (!empty($jsnippets)) {
158 // The autocomplete library is used to retrieve jsnippets from the DB.
159 drupal_add_js('misc/autocomplete.js');
160 drupal_add_js(drupal_get_path('module', 'jsnippets') .'/jsnippets.js');
161
162 $jsnippets = drupal_to_js($jsnippets);
163 $js = 'function jsnippets_get_all() {
164 return '. $jsnippets .';
165 }';
166 $js = '<script type="text/javascript">'. $js .'</script>';
167 drupal_set_html_head($js);
168 }
169 }
170 }
171
172 /**
173 * Respond to XMLHTTP request; return snippet associated with $snippet_id (JSON).
174 */
175 function jsnippets_retrieve($snippet_id) {
176 $jsnippet = _jsnippets_get_jsnippet($snippet_id);
177 $matches = array($snippet_id => $jsnippet['value']);
178 // Convert to JSON.
179 print drupal_to_js($matches);
180 exit();
181 }
182
183 /**
184 * Add / update a snippet.
185 *
186 * @param Array $snippet
187 * An array containing snippet information formatted for drupal_write_record.
188 * @param Array $update
189 * In case of an update, add the key, else leave empty.
190 * @param Boolean $notify
191 * Displays status messages to the user only if set to TRUE.
192 * @return $status
193 * Status variable as per drupal_write_record.
194 */
195 function jsnippets_snippet_add($snippet, $update = array(), $notify = TRUE) {
196 $status = drupal_write_record('jsnippets', $snippet, $update);
197
198 switch ($status) {
199 case SAVED_NEW:
200 $message = 'Snippet %name added.';
201 break;
202 case SAVED_UPDATED:
203 $message = 'Snippet %name updated.';
204 break;
205 }
206
207 $vars = array('%name' => $snippet['name']);
208 watchdog('JSnippets', $message, $vars);
209 if ($notify) {
210 drupal_set_message(t($message, $vars));
211 }
212
213 return $status;
214 }
215
216 /**
217 * Add / update a section.
218 *
219 * @param Array $section
220 * An array containing section information formatted for drupal_write_record.
221 * @param Array $update
222 * In case of an update, add the key, else leave empty.
223 * @param Boolean $notify
224 * Displays status messages to the user only if set to TRUE.
225 * @return $section
226 * The section object.
227 */
228 function jsnippets_section_add($section, $update = array(), $notify = TRUE) {
229 $status = drupal_write_record('jsnippets_sections', $section, $update);
230
231 switch ($status) {
232 case SAVED_NEW:
233 $message = 'Section %name added.';
234 break;
235 case SAVED_UPDATED:
236 $message = 'Section %name updated.';
237 break;
238 }
239
240 $vars = array('%name' => $section['name']);
241 watchdog('JSnippets', $message, $vars);
242 if ($notify) {
243 drupal_set_message(t($message, $vars));
244 }
245
246 return $section;
247 }
248
249 /**
250 * Retrieve a single section.
251 */
252 function _jsnippets_get_section($section_id) {
253 return db_fetch_array(db_query("SELECT * FROM {jsnippets_sections} WHERE section_id = %d", $section_id));
254 }
255
256 /**
257 * Retrieve all sections.
258 *
259 * @param $all
260 * Controls the return format:
261 * $all = TRUE will return array('id' => array(all section_values ...), ...)
262 * $all = FALSE will return array('id' => 'section_name', ...);
263 */
264 function _jsnippets_get_sections($all = FALSE) {
265 $result = db_query("SELECT ss.*, COUNT(s.section_id) AS jsnippets FROM {jsnippets_sections} ss LEFT JOIN {jsnippets} s ON (ss.section_id = s.section_id) GROUP BY ss.section_id ORDER BY ss.name");
266
267 $sections = array();
268 while ($section = db_fetch_array($result)) {
269 if ($all) {
270 $sections[$section['section_id']] = $section;
271 }
272 else {
273 $sections[$section['section_id']] = $section['name'];
274 }
275 }
276
277 return $sections;
278 }
279
280 /**
281 * Retrieve all sections based on form_id.
282 */
283 function _jsnippets_add_jsnippets_by_id($form_id) {
284 $result = db_query("SELECT * FROM {jsnippets_sections} WHERE form_id = '%s'", $form_id);
285
286 $sections = array();
287 while ($section = db_fetch_array($result)) {
288 $result1 = db_query("SELECT * FROM {jsnippets} WHERE section_id = '%s'", $section['section_id']);
289 $jsnippets = array();
290 while ($jsnippet = db_fetch_array($result1)) {
291 $jsnippets[$jsnippet['snippet_id']] = str_repeat('&nbsp;', 4) . $jsnippet['name'];
292 }
293 // Store jsnippets array for subsequent retrieval.
294 if (!empty($jsnippets)) {
295 _jsnippets_js_array('insert', $section['element_id'], $jsnippets);
296 }
297 }
298 }
299
300 /**
301 * Retrieve a single snippet.
302 */
303 function _jsnippets_get_jsnippet($jsnippet_id) {
304 return db_fetch_array(db_query("SELECT * FROM {jsnippets} WHERE snippet_id = %d", $jsnippet_id));
305 }
306
307 /**
308 * Retrieve all snippets belonging to a section.
309 */
310 function _jsnippets_get_jsnippets($section_id) {
311 $result = db_query("SELECT * FROM {jsnippets} WHERE section_id = %d", $section_id);
312
313 $jsnippets = array();
314 while ($jsnippet = db_fetch_array($result)) {
315 $jsnippets[] = $jsnippet;
316 }
317
318 return $jsnippets;
319 }
320
321 /**
322 * Stores jsnippet values for each form on a page for subsequent retrieval via
323 * hook_form_alter().
324 */
325 function _jsnippets_js_array($op = 'extract', $id = '', $values = array()) {
326 static $jsnippets = array();
327
328 if ($op == 'insert') {
329 $jsnippets[$id] = array(t('none') => str_repeat('&nbsp;', 4) . t('Insert a snippet')) + $values;
330 }
331 else {
332 return $jsnippets;
333 }
334 }

  ViewVC Help
Powered by ViewVC 1.1.2