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

Contents of /contributions/modules/chipin/chipin.module

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


Revision 1.5 - (show annotations) (download) (as text)
Thu Aug 31 23:43:41 2006 UTC (3 years, 2 months ago) by robroy
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +0 -7 lines
File MIME type: text/x-php
- Content change.
- Filter tips long version.
1 <?php
2 // $Id: chipin.module,v 1.2 2006/08/28 20:19:33 robroy Exp $
3
4 /**
5 * @file
6 * Lets users easily insert a ChipIn Flash widget into content.
7 */
8
9 define('CHIPIN_ACCESS_PAGES_DEFAULT', "node/*\nadmin/block/*");
10
11 /**
12 * Implementation of hook_help().
13 */
14 function chipin_help($section) {
15 switch ($section) {
16 case 'admin/modules#description':
17 return t('Enable users to insert a ChipIn widget to enable group money collection for any purpose.');
18 case 'admin/help#chipin':
19 return t('<h2 id="about">About ChipIn</h2>
20 <p>ChipIn empowers individuals to connect with people in their social network to collect money for a personal cause, to purchase a gift, or for community fundraising. ChipIn makes this <strong>social ecommerce</strong><sup>SM</sup> process of connecting and collecting fun, easy and secure. ChipIn believes that positive change will result from enabling groups to harness the power of giving. That\'s the ChipIn mission.</p>
21 <p><strong>ChipIn helps people:</strong></p>
22 <ul>
23 <li>Organize and collaborate with each other to collect money for a group purchase</li>
24 <li>Raise money for personal and community causes</li>
25 <li>Collect money to make a difference for a recipient</li>
26 <li>Connect with people who share a common goal for collecting funds</li>
27 <li>Get people involved in giving together</li>
28 <li>Actually enjoy being an organizer</li>
29 <li>Account for money collected</li>
30 </ul>
31
32 <p><strong>Want to learn more about ChipIn?</strong></p>
33 <ul>
34 <li><a href="http://www.chipin.com/overview">Take the ChipIn quick tour</a> of how the ChipIn process works</li>
35 <li>Read the <a href="http://faq.chipin.com/">ChipIn FAQ</a> for frequently asked questions about ChipIn</li>
36 </ul>
37 <h2 id="widget">What\'s a widget?</h2>
38 <img src="'. base_path() . drupal_get_path('module', 'chipin') .'/images/widget.mini.png" alt="An example of a ChipIn widget." style="margin: 0 20px; float: right;" height="200" width="204" />
39 <p>The <strong>ChipIn widget</strong> is a small indicator of the progress of your event that you can add to your Web pages.</p>
40 <ul>
41 <li>The ChipIn widget can be easily added to your Web page and helps to promote your ChipIn event to your site visitors.</li>
42 <li>The widget provides real-time updates on the amount contributed to date, the percentage of the goal reached, and the number of people contributing.</li>
43 <li>By clicking on the "ChipIn!" button on the widget, visitors will be taken to your event invitation page where they can contribute to your ChipIn.</li>
44 </ul>
45 <br class="clear" />');
46 }
47 }
48
49 /**
50 * Implementation of hook_filter().
51 */
52 function chipin_filter($op, $delta = 0, $format = -1, $text = '') {
53 switch ($op) {
54 case 'list':
55 return array(0 => t('ChipIn Flash widget'));
56
57 case 'description':
58 return t('Add a ChipIn Flash widget to your posts.');
59
60 case 'process':
61 foreach(chipin_get_macros($text) as $unexpanded_macro => $macro) {
62 $expanded_macro = chipin_render_widget($macro);
63 $text = str_replace($unexpanded_macro, $expanded_macro, $text);
64 }
65 return $text;
66
67 default:
68 return $text;
69 }
70 }
71
72 /**
73 * Implementation of hook_filter_tips().
74 */
75 function chipin_filter_tips($delta, $format, $long = FALSE) {
76 if (variable_get('chipin_link', 'icon') == 'icon') {
77 $type = t('icon');
78 }
79 else {
80 $type = t('link');
81 }
82 switch ($long) {
83 case 0:
84 return t('Click the ChipIn %type above to add a ChipIn widget to this post.', array('%type' => $type));
85 case 1:
86 return t('Click the ChipIn %type below any text box to add a ChipIn widget to that post.', array('%type' => $type));
87 }
88 }
89
90 /**
91 * Implementation of hook_menu().
92 */
93 function chipin_menu($may_cache) {
94 $items = array();
95 if ($may_cache) {
96 $items[] = array(
97 'path' => 'chipin/wizard',
98 'title' => t('ChipIn Flash Widget Wizard'),
99 'callback' => 'chipin_wizard',
100 'access' => user_access('access content'),
101 'type' => MENU_CALLBACK,
102 );
103 }
104 return $items;
105 }
106
107 /**
108 * Implementation of hook_perm().
109 */
110 function chipin_perm() {
111 return array('access ChipIn widget wizard');
112 }
113
114 /**
115 * Implementation of hook_settings().
116 *
117 * Based on the hook_settings() in TinyMCE.
118 */
119 function chipin_settings() {
120 $form = array();
121
122 $form['info'] = array(
123 '#type' => 'markup',
124 '#value' => '<p>'
125 . t('ChipIn empowers individuals to connect with people in their social network to collect money for a personal cause, to purchase a gift, or for community fundraising. ChipIn makes this <strong>social ecommerce</strong><sup>SM</sup> process of connecting and collecting fun, easy and secure. ChipIn believes that positive change will result from enabling groups to harness the power of giving. That\'s the ChipIn mission.')
126 . ' ' . t('Learn more on the %chipin-link.', array('%chipin-link' => l(t('ChipIn help page'), 'admin/help/chipin')))
127 . '</p>',
128 );
129
130 $form['visibility'] = array(
131 '#type' => 'fieldset',
132 '#title' => t('Visibility'),
133 '#collapsible' => FALSE,
134 '#collapsed' => FALSE,
135 );
136
137 $access = user_access('use PHP for block visibility');
138
139 // If the visibility is set to PHP mode but the user doesn't have this block
140 // permission, don't allow them to edit nor see this PHP code.
141 if (variable_get('chipin_access', 1) == 2 && !$access) {
142 $form['visibility'] = array();
143 $form['visibility']['chipin_access'] = array(
144 '#type' => 'value',
145 '#value' => 2,
146 );
147 $form['visibility']['chipin_access_pages'] = array(
148 '#type' => 'value',
149 '#value' => variable_get('chipin_access_pages', CHIPIN_ACCESS_PAGES_DEFAULT),
150 );
151 }
152 else {
153 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
154 $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '%blog' for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => theme('placeholder', 'blog'), '%blog-wildcard' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>')));
155
156 if ($access) {
157 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
158 $description .= t(' If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => theme('placeholder', '<?php ?>')));
159 }
160 $form['visibility']['chipin_access'] = array(
161 '#type' => 'radios',
162 '#title' => t('Show ChipIn wizard on specific pages'),
163 '#default_value' => variable_get('chipin_access', 1),
164 '#options' => $options,
165 );
166 $form['visibility']['chipin_access_pages'] = array(
167 '#type' => 'textarea',
168 '#title' => t('Pages'),
169 '#default_value' => variable_get('chipin_access_pages', CHIPIN_ACCESS_PAGES_DEFAULT),
170 '#description' => $description,
171 );
172 }
173
174 $form['visibility']['chipin_link'] = array(
175 '#type' => 'select',
176 '#title' => t('Textarea widget link'),
177 '#default_value' => variable_get('chipin_link', 'icon'),
178 '#options' => array('icon' => t('Show icon'), 'text' => t('Show text link')),
179 '#description' => t('Choose what to show under the textareas for which the ChipIn widget wizard is enabled.'),
180 );
181 return $form;
182 }
183
184 /**
185 * Menu callback; print the ChipIn Flash widget wizard interface.
186 *
187 * @param $editor
188 * Specify with what type of editor (if any) we are working. Planned for
189 * future use as a TinyMCE plugin.
190 */
191 function chipin_wizard($editor = 'textarea') {
192 //drupal_set_title(t('ChipIn Flash Widget Wizard'));
193
194 $text_attributes = array(
195 'onchange' => 'setWidget();',
196 'onkeypress' => 'return returnFilter(this, event);',
197 );
198 $color_attributes = array(
199 'onchange' => 'setWidget();',
200 'onblur' => 'setWidget();',
201 'onkeypress' => 'return hexFilter(this, event);',
202 );
203
204 $form = array();
205 $form['#id'] = 'widgetform';
206
207 /**
208 * Construct the event text fields.
209 */
210 $form['event'] = array();
211 $form['event']['label'] = array();
212 $form['event']['field'] = array();
213
214 $form['event']['label']['event_id'] = array('#value' => '<label for="edit-event_id">'. t('Event ID') .'</label>');
215 $form['event']['field']['event_id'] = array(
216 '#type' => 'textfield',
217 '#default_value' => '',
218 '#maxlength' => 127,
219 '#attributes' => $text_attributes,
220 );
221
222 $form['event']['label']['event_title'] = array('#value' => '<label for="edit-event_title">'. t('Event Title') .'</label>');
223 $form['event']['field']['event_title'] = array(
224 '#type' => 'textfield',
225 '#default_value' => '',
226 '#maxlength' => 127,
227 '#attributes' => $text_attributes,
228 );
229 $form['event']['label']['event_desc'] = array('#value' => '<label for="edit-event_desc">'. t('Description') .'</label>');
230 $form['event']['field']['event_desc'] = array(
231 '#type' => 'textfield',
232 '#default_value' => '',
233 '#maxlength' => 2048,
234 '#attributes' => $text_attributes,
235 );
236
237 /**
238 * Construct the dropdown size fields.
239 */
240 $form['size'] = array();
241 $form['size']['label'] = array();
242 $form['size']['field'] = array();
243
244 $form['size']['label']['widget_width'] = array('#value' => '<label for="edit-widget_width">'. t('Width') .'</label>');
245 $form['size']['field']['widget_width'] = array(
246 '#type' => 'select',
247 '#default_value' => 280,
248 '#options' => array(160 => 160, 180 => 180, 200 => 200, 234 => 234, 240 => 240, 245 => 245, 250 => 250, 270 => 270, 280 => 280, 300 => 300, 336 => 336, 425 => 425, 468 => 468, '100%' => '100%'),
249 '#attributes' => array('onchange' => 'setWidget()'),
250 );
251
252 $form['size']['label']['widget_height'] = array('#value' => '<label for="edit-widget_height">'. t('Height') .'</label>');
253 $form['size']['field']['widget_height'] = array(
254 '#type' => 'select',
255 '#default_value' => 255,
256 '#options' => array(90 => 90, 100 => 100, 110 => 110, 120 => 120, 125 => 125, 160 => 160, 180 => 180, 200 => 200, 220 => 220, 240 => 240, 250 => 250, 255 => 255, 270 => 270, 280 => 280, 400 => 400, 425 => 425, 500 => 500, '100%' => '100%'),
257 '#attributes' => array('onchange' => 'setWidget()'),
258 );
259
260 $form['size']['label']['widget_align'] = array('#value' => '<label for="edit-widget_align">'. t('Align') .'</label>');
261 $form['size']['field']['widget_align'] = array(
262 '#type' => 'select',
263 '#default_value' => '',
264 '#options' => array('' => t('none'), 'left' => t('left'), 'right' => t('right')),
265 '#attributes' => array('onchange' => 'setWidget()'),
266 );
267
268 /**
269 * Construct the color fields.
270 */
271 $form['colors'] = array();
272 $form['colors']['label'] = array();
273 $form['colors']['field'] = array();
274 $form['colors']['swatch'] = array();
275
276 $form['colors']['header'] = array('#value' => '<p><b>Colors</b></p>');
277
278 $colors = array();
279 $colors['colortitle'] = array('color' => 'ffffff', 'title' => t('Title'));
280 $colors['colorbar'] = array('color' => '758f46', 'title' => t('Bar'));
281 $colors['colordesc'] = array('color' => '000000', 'title' => t('Description'));
282 $colors['colorbarb'] = array('color' => 'dddddd', 'title' => t('Bar Back'));
283 $colors['colorborder'] = array('color' => 'fcab42', 'title' => t('Border'));
284 $colors['colorbart'] = array('color' => '758f46', 'title' => t('Bar Text'));
285 $colors['colorbg'] = array('color' => 'ffffff', 'title' => t('Background'));
286
287 foreach ($colors as $field => $params) {
288 $form['colors']['label'][$field .'f'] = array('#value' => '<label for="edit-'. $field .'f" id="edit-'. $field .'-label">'. $params['title'] .'</label>');
289 $form['colors']['field'][$field .'f'] = array('#type' => 'textfield', '#default_value' => $params['color'], '#size' => 6, '#maxlength' => 6, '#attributes' => $color_attributes);
290 $form['colors']['swatch'][$field .'f'] = array('#value' => '<a href="#" onclick="pickcolor(\''. $field .'\');return false"><div class="swatch" id="edit-'. $field .'d"></div></a>');
291 }
292
293 $form['colors']['label']['presets'] = array('#value' => '<label>'. t('Presets') .'</label>');
294 $form['colors']['field']['presets'] = array(
295 '#type' => 'markup',
296 '#value' => '<b><a href="#" onclick="setWidgetLayout(\'1\');return false">1</a> <a href="#" onclick="setWidgetLayout(\'2\');return false">2</a> <a href="#" onclick="setWidgetLayout(\'3\');return false">3</a></b>',
297 );
298
299 $form['colors']['spectrum'] = array(
300 '#type' => 'markup',
301 '#value' => '<div id="spectrum" style="display:none;text-align:right;width:305px;margin:10px auto 0 auto;padding-right: 5px;">'. "\n"
302 . ' <div>'. "\n"
303 . ' <img id="spectrumimg" src="'. base_path() . drupal_get_path('module', 'chipin') .'/images/spectrum.png" alt="" onmousedown="chooseColor(event);return false;" onmouseup="return false;" onmousemove="return false;"/>'. "\n"
304 . ' <table cellpadding="0" cellspacing="0" border="0" width="100%">'. "\n"
305 . ' <tr>'. "\n"
306 . ' <td nowrap="true" align="left">click on a color</td>'. "\n"
307 . ' <td align="right" style="padding:0"><a href="#" onclick="closePicker();return false;">close</a></td>'. "\n"
308 . ' </tr>'. "\n"
309 . ' </table>'. "\n"
310 . ' </div>'. "\n"
311 . '</div>'. "\n",
312 );
313
314 /**
315 * Construct the checkbox fields.
316 */
317 $form['show'] = array();
318 $form['show']['label'] = array();
319 $form['show']['field'] = array();
320
321 $form['show']['header'] = array('#value' => '<p><b>Show</b></p>');
322
323 $form['show']['label']['showborder'] = array('#value' => '<label for="edit-showborder">'. t('Border') .'</label>');
324 $form['show']['field']['showborder'] = array(
325 '#type' => 'checkbox',
326 '#default_value' => 1,
327 '#return_value' => 1,
328 '#attributes' => array('onclick' => 'setWidget();'),
329 );
330 $form['show']['label']['showbar'] = array('#value' => '<label for="edit-showbar">'. t('Progress Bar') .'</label>');
331 $form['show']['field']['showbar'] = array(
332 '#type' => 'checkbox',
333 '#default_value' => 1,
334 '#return_value' => 1,
335 '#attributes' => array('onclick' => 'setWidget();'),
336 );
337 $form['show']['label']['showevent'] = array('#value' => '<label for="edit-showevent">'. t('Event Image') .'</label>');
338 $form['show']['field']['showevent'] = array(
339 '#type' => 'checkbox',
340 '#default_value' => 1,
341 '#return_value' => 1,
342 '#attributes' => array('onclick' => 'setWidget();'),
343 );
344 $form['show']['label']['showbg'] = array('#value' => '<label for="edit-showbg">'. t('Background Image') .'</label>');
345 $form['show']['field']['showbg'] = array(
346 '#type' => 'checkbox',
347 '#default_value' => 1,
348 '#return_value' => 1,
349 '#attributes' => array('onclick' => 'setWidget();'),
350 );
351 $form['show']['label']['showdesc'] = array('#value' => '<label for="edit-showdesc">'. t('Description') .'</label>');
352 $form['show']['field']['showdesc'] = array(
353 '#type' => 'checkbox',
354 '#default_value' => 1,
355 '#return_value' => 1,
356 '#attributes' => array('onclick' => 'setWidget();'),
357 );
358
359 $form['widgetframe'] = array(
360 '#type' => 'markup',
361 '#value' => '<iframe scrolling="no" id="widgetframe" name="widgetframe" width="280" height="255" frameborder="0" border="0" style="z-index:-1;border:0;padding:0;margin-bottom:20px" src="https://www.chipin.com/widget/widget.swf"></iframe><br class="clear" />',
362 );
363
364 $form['insert'] = array(
365 '#type' => 'submit',
366 '#value' => t('Insert Widget Code'),
367 '#attributes' => array('onclick' => 'parent.insertWidgetCode(); return false;', 'style' => 'float:left;', 'id' => 'insert'),
368 );
369 $form['cancel'] = array(
370 '#type' => 'button',
371 '#value' => t('Cancel'),
372 '#button_type' => 'button',
373 '#attributes' => array('onclick' => 'parent.cancelAction()', 'style' => 'float:right;', 'id' => 'cancel'),
374 );
375
376 $form['help'] = array(
377 '#type' => 'markup',
378 '#value' => '<div style="padding: 10px 0; clear: both;">'
379 . t('<strong>Don\'t have your ChipIn event ID?</strong> Your ChipIn event ID can be found at the bottom of your %chipin-status-page.', array('%chipin-status-page' => l(t('ChipIn status page'), 'http://www.chipin.com', array('target' => '_blank'), NULL, NULL, TRUE)))
380 . '</div>',
381 );
382
383 $form['widgettag'] = array(
384 '#type' => 'hidden',
385 '#value' => '',
386 );
387
388 $output = drupal_get_form('chipin_wizard', $form);
389 $output = theme('chipin_popup_page', $output);
390 print $output;
391 }
392
393
394 /**
395 * Theme function ChipIn wizard form.
396 *
397 * @param $form
398 * The form to be themed.
399 *
400 * @return
401 * HTML for either a ChipIn image or text link for adding a Flash widget.
402 *
403 * @ingroup themeable
404 */
405 function theme_chipin_wizard($form) {
406 // Render the event text fields.
407 $rows = _chipin_get_table_rows($form['event'], 2, array('label', 'field'), 'field');
408 $output .= theme('table', array(), $rows, array('id' => 'titledesc'));
409
410 // Render the size selects.
411 $rows = _chipin_get_table_rows($form['size'], 6, array('label', 'field'), 'field');
412 $output .= theme('table', array(), $rows);
413
414 // Render the color swatches and spectrum.
415 $rows = _chipin_get_table_rows($form['colors'], 6, array('label', 'field', 'swatch'), 'field');
416 $output .= form_render($form['colors']['header']);
417 $output .= theme('table', array(), $rows);
418 $output .= form_render($form['colors']['spectrum']);
419
420 // Render the checkboxes.
421 $output .= form_render($form['show']['header']);
422 $rows = _chipin_get_table_rows($form['show'], 4, array('field', 'label'), 'field');
423 $output .= theme('table', array(), $rows, array('id' => 'showtable'));
424
425 // Render the rest of the items in the right column and theme the outer table.
426 $rows = array(array(array('data' => $output, 'style' => 'vertical-align: top;'), array('data' => form_render($form), 'style' => 'vertical-align: top;')));
427 $output = '<div class="highlightFormSection">'
428 . theme('table', array(), $rows, array('id' => 'widgetformtable', 'style' => 'z-index:0;'))
429 . '</div>';
430 return $output;
431 }
432
433 /**
434 * Form helper function to easily print out structured form fields in a table.
435 *
436 * @param $form
437 * A form element whose children are to be themed.
438 *
439 * @param $columns
440 * Number of columns in the table.
441 *
442 * @param $fields
443 * An array of field names to render.
444 *
445 * @param $primary_field
446 * The primary field name on which to loop.
447 *
448 * @param $last_column_data
449 * The data to place in the last column(s) if there is an incomplete row
450 * at the end of the table.
451 *
452 * @return
453 * An array of table rows.
454 *
455 * @ingroup themeable
456 */
457 function _chipin_get_table_rows(&$form, $columns, $fields, $primary_field, $last_column_data = '&nbsp;') {
458 $row = array();
459 $rows = array();
460 foreach (element_children($form[$primary_field]) as $key) {
461 foreach ($fields as $field) {
462 if (isset($form[$field][$key])) {
463 $row[] = form_render($form[$field][$key]);
464 }
465 else {
466 $row[] = '&nbsp;';
467 }
468 }
469 if (count($row) == $columns) {
470 $rows[] = $row;
471 $row = array();
472 }
473 }
474 if (count($row)) {
475 $row[] = array('data' => $last_column_data, 'colspan' => $columns - count($row));
476 $rows[] = $row;
477 }
478 return $rows;
479 }
480
481 /**
482 * Return all ChipIn macros as an array.
483 *
484 * An example of a ChipIn macro:
485 *
486 * [chipin|event_id=38444da8e48c7190|event_title=some title
487 * |event_desc=something else|show_url=true|color_title=ffffff
488 * |color_graphfg=758f46|color_fg=000000|color_graphbg=dddddd
489 * |color_border=fcab42|color_info=758f46|color_bg=ffffff]
490 *
491 * @param $text
492 * Text to be parsed for ChipIn macros.
493 *
494 * @return
495 * An array of all ChipIn macros contained in the input text.
496 */
497 function chipin_get_macros($text) {
498 $macros = array();
499 preg_match_all('/ \[ ( [^\[\]]+ )* \] /x', $text, $matches);
500
501 // Don't process duplicates.
502 $tag_match = array_unique($matches[1]);
503
504 foreach($tag_match as $macro) {
505 $current_macro = '['. $macro .']';
506 $params = array_map('trim', explode('|', $macro));
507
508 // The first macro parameter is assumed to be the function name.
509 $func_name = array_shift($params);
510
511 if ($func_name == 'chipin') {
512 $vars = array();
513 foreach($params as $param) {
514 $pos = strpos($param, '=');
515 $varname = substr($param, 0, $pos);
516 $varvalue = urldecode(substr($param, $pos + 1));
517 $vars[$varname] = $varvalue;
518 }
519
520 // The full unaltered filter string is the key for the array of filter attributes.
521 $macros[$current_macro] = $vars;
522 }
523 }
524
525 return $macros;
526 }
527
528 /**
529 * Returns HTML for a corresponding ChipIn Flash widget.
530 *
531 * @param $attributes
532 * An array of attributes parsed from the macro filter string.
533 *
534 * @return
535 * HTML for the ChipIn Flash widget.
536 */
537 function chipin_render_widget($attributes = array()) {
538 global $user;
539
540 // Strip out any non-allowed attributes.
541 $allowed_in_macro = array(
542 'event_id' => 'text',
543 'event_title' => 'text',
544 'event_desc' => 'text',
545 'show_url' => 'text',
546 'color_title' => 'color',
547 'color_graphfg' => 'color',
548 'color_fg' => 'color',
549 'color_graphbg' => 'color',
550 'color_border' => 'color',
551 'color_info' => 'color',
552 'color_bg' => 'color',
553 'width' => 'text',
554 'height' => 'text',
555 'align' => 'text',
556 'use_sample_data' => 'text',
557 );
558
559 $attributes = _chipin_filter_attributes($attributes, $allowed_in_macro);
560
561 // Apply width and height to embed attributes as they aren't used in flashVars.
562 $embed['width'] = $attributes['width'] ? $attributes['width'] : 280;
563 $embed['height'] = $attributes['height'] ? $attributes['height'] : 255;
564
565 // Set Flash HTML element attributes and unset from attributes array.
566 $embed['pluginspage'] = 'http://www.macromedia.com/go/getflashplayer';
567 $embed['quality'] = 'high';
568 $embed['allowScriptAccess'] = 'never';
569 $embed['src'] = 'http://widget.chipin.com/widget/widget.swf';
570
571 // Create flashVars string of filtered attributes.
572 $embed['flashVars'] = _chipin_query_string_encode($attributes);
573
574 $output = '<embed'. drupal_attributes($embed) .'></embed>';
575 $output = theme('chipin_widget', $output, $attributes);
576 return $output;
577 }
578
579
580 /**
581 * Filters out any unallowed attributes in the given array, returning a query
582 * string of key/value pairs.
583 *
584 * @return
585 * A query string of key/value pairs for use in the flashVars embed variable.
586 */
587 function _chipin_query_string_encode($attributes) {
588 // Strip out any non-allowed attributes.
589 $allowed_in_query_string = array(
590 'event_id' => 'text',
591 'event_title' => 'text',
592 'event_desc' => 'text',
593 'show_url' => 'text',
594 'color_title' => 'color',
595 'color_graphfg' => 'color',
596 'color_fg' => 'color',
597 'color_graphbg' => 'color',
598 'color_border' => 'color',
599 'color_info' => 'color',
600 'color_bg' => 'color',
601 'use_sample_data' => 'text',
602 );
603
604 $attributes = _chipin_filter_attributes($attributes, $allowed_in_query_string);
605 return drupal_query_string_encode($attributes);
606 }
607
608 /**
609 * Helper function that strips any unallowed attributes from array.
610 *
611 * @param $attributes
612 * An attributes array of key/value pairs.
613 *
614 * @param $allowed
615 * An array with key being allowed keys in $attributes array and value being
616 * the type of validation to occur.
617 *
618 * @return
619 * A filtered attributes array.
620 */
621 function _chipin_filter_attributes($attributes, $allowed) {
622 $attributes = array_map('trim', $attributes);
623
624 foreach ($attributes as $k => $v) {
625 $remove = TRUE;
626
627 if (isset($allowed[$k])) {
628 switch ($allowed[$k]) {
629 case 'color':
630 $remove = !preg_match('/([a-fA-F0-9]{6})/', $attributes[$k]);
631 break;
632 default:
633 $remove = FALSE;
634 break;
635 }
636 }
637
638 if ($remove) {
639 unset($attributes[$k]);
640 }
641 }
642
643 return $attributes;
644 }
645
646 /**
647 * Implementation of hook_elements().
648 */
649 function chipin_elements() {
650 $type = array();
651 $type['textarea'] = array('#process' => array('chipin_textarea' => array()));
652 $type['colorfield'] = array('#input' => TRUE, '#size' => 6, '#maxlength' => 6, '#autocomplete_path' => FALSE, '#process' => array('chipin_colorfield' => array()));
653 return $type;
654 }
655
656 /**
657 * Add ChipIn image link underneath applicable textareas.
658 */
659 function chipin_textarea($element) {
660 $link = variable_get('chipin_link', 'icon');
661 if (_chipin_page_match() && _chipin_element_match($element)) { // && !strstr($_GET['q'], 'chipin')
662 if (user_access('access ChipIn widget wizard')) {
663 $element['#suffix'] .= theme('chipin_textarea_link', $element, $link);
664 }
665 }
666 return $element;
667 }
668
669 /**
670 * Determine if ChipIn has permission to place a link under this form element.
671 *
672 * @return
673 * TRUE if can render, FALSE if not allowed.
674 */
675 function _chipin_element_match($element) {
676 $return = TRUE;
677
678 // $element is an array of attributes for the textarea but there is no just
679 // 'name' value, so we extract this from the #id field.
680 $textarea_name = substr($element['#id'], strpos($element['#id'], '-') + 1);
681 switch ($textarea_name) {
682 case 'log':
683 case 'code':
684 $return = FALSE;
685 break;
686 }
687
688 return $return;
689 }
690
691 /**
692 * Determine if ChipIn has permission to be used on the current page.
693 *
694 * @return
695 * TRUE if can render, FALSE if not allowed.
696 */
697 function _chipin_page_match() {
698 $page_match = FALSE;
699
700 if (variable_get('chipin_access_pages', CHIPIN_ACCESS_PAGES_DEFAULT)) {
701 // If the PHP option wasn't selected
702 if (variable_get('chipin_access', 1) < 2) {
703 $path = drupal_get_path_alias($_GET['q']);
704 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote(variable_get('chipin_access_pages', CHIPIN_ACCESS_PAGES_DEFAULT), '/')) .')$/';
705 $page_match = !(variable_get('chipin_access', 1) xor preg_match($regexp, $path));
706 }
707 else {
708 $page_match = drupal_eval(variable_get('chipin_access_pages', CHIPIN_ACCESS_PAGES_DEFAULT));
709 }
710 }
711 // No pages were specified to block so show on all
712 else {
713 $page_match = TRUE;
714 }
715
716 return $page_match;
717 }
718
719 /**
720 * Theme function for adding a ChipIn link underneath textareas.
721 *
722 * @param $element
723 * The textarea form element.
724 *
725 * @param $link
726 * The type of link ('icon' or 'text') to add to the bottom of this textarea.
727 *
728 * @return
729 * HTML for either a ChipIn image or text link for adding a Flash widget.
730 *
731 * @ingroup themeable
732 */
733 function theme_chipin_textarea_link($element, $link) {
734 $output = '<div class="chipin-button"><a class="chipin-link" id="chipin-link-'. $element['#id'] .'" title="'. t('Add a ChipIn Money Collection Widget') .'" href="'. url('chipin/wizard/textarea', 'textarea='. $element['#name']) .'" onclick="window.open(this.href, \'chipin_link\', \'width=705,height=550,scrollbars=no,status=no,resizable=yes,toolbar=no,menubar=no\'); return false">';
735 if ($link == 'icon') {
736 $output .= '<img src="'. base_path() . drupal_get_path('module', 'chipin') .'/images/chipin.logo.gif" border="0" width="99" height="42" />';
737 }
738 else {
739 $output .= t('Add a ChipIn Money Collection Widget');
740 }
741 $output .= '</a></div>';
742 return $output;
743 }
744
745 /**
746 * Theme function for customizing the display of the ChipIn Flash widget.
747 *
748 * @param $text
749 * The Flash widget HTML to be themed.
750 *
751 * @param $attributes
752 * Optional array of attributes for additional theme customization.
753 *
754 * @return
755 * Themed HTML for the ChipIn Flash widget.
756 *
757 * @ingroup themeable
758 */
759 function theme_chipin_widget($text, $attributes = array()) {
760 if (isset($attributes['align']) && !empty($attributes['align'])) {
761 $style = ' style="float: '. check_plain($attributes['align']) .';"';
762 }
763 else {
764 $style = '';
765 }
766 $output = '<div class="chipin"'. $style .'>'. "\n";
767 $output .= $text;
768 $output .= '</div>'. "\n";
769 return $output;
770 }
771
772 /**
773 * Themeable page function for ChipIn widget popup window.
774 *
775 * @ingroup themeable
776 */
777 function theme_chipin_popup_page($content) {
778 // We aren't doing this anymore, to make jQuery integration easier.
779 //drupal_set_header('Content-Type: text/html; charset=utf-8');
780 //drupal_add_js(drupal_get_path('module', 'chipin') .'/js/jquery.js');
781 //drupal_add_js(drupal_get_path('module', 'chipin') .'/js/chipin_textarea.js');
782 //drupal_add_js(drupal_get_path('module', 'chipin') .'/js/chipin.js');
783 //$output .= drupal_set_html_head();
784
785 $output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'. "\n";
786 $output .= '<html xmlns="http://www.w3.org/1999/xhtml">'. "\n";
787 $output .= '<head>'. "\n";
788 $output .= ' <title>'. drupal_get_title() .'</title>'. "\n";
789 $output .= ' <script type="text/javascript" src="'. base_path() . drupal_get_path('module', 'chipin') .'/js/jquery.js"></script>'. "\n";
790 $output .= ' <script type="text/javascript" src="'. base_path() . drupal_get_path('module', 'chipin') .'/js/chipin_textarea.js"></script>'. "\n";
791 $output .= ' <script type="text/javascript" src="'. base_path() . drupal_get_path('module', 'chipin') .'/js/chipin.js"></script>'. "\n";
792 $output .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'. "\n";
793 $output .= ' <link rel="shortcut icon" href="'. base_path() .'misc/favicon.ico" type="image/x-icon" />';
794 $output .= theme('stylesheet_import', base_path() . drupal_get_path('module', 'chipin') .'/chipin.css', 'all');
795 $output .= '</head>'. "\n";
796 $output .= '<body>'. "\n";
797
798 $output .= "\n<!-- begin content -->\n";
799 $output .= $content;
800 $output .= "\n<!-- end content -->\n";
801
802 return $output;
803 }

  ViewVC Help
Powered by ViewVC 1.1.2