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

Contents of /contributions/modules/sifr/sifr.module

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


Revision 1.22 - (show annotations) (download) (as text)
Fri Jan 18 18:34:52 2008 UTC (22 months, 1 week ago) by sun
Branch: MAIN
CVS Tags: HEAD
Changes since 1.21: +7 -6 lines
File MIME type: text/x-php
Updated sIFR rule examples based on default Garland theme.
1 <?php
2 // $Id: sifr.module,v 1.21 2008/01/18 18:15:02 sun Exp $
3
4
5 /**
6 * @file
7 * sIFR Module for Drupal
8 * by Jeff Robbins - www.lullabot.com
9 * development sponsored by Bryght
10 * Implements sIFR capabilities for all pages.
11 * http://www.mikeindustries.com/sifr/
12 */
13
14 /**
15 * Implementation of hook_menu().
16 */
17 function sifr_menu($may_cache) {
18 $items = array();
19 $access = user_access('administer site configuration');
20 if ($may_cache) {
21 $items[] = array(
22 'title' => t('sIFR'),
23 'path' => 'admin/settings/sifr',
24 'description' => t('Configure which HTML text elements in your theme are rendered with Flash-based fonts.'),
25 'access' => $access,
26 'callback' => 'sifr_rules',
27 );
28 $items[] = array(
29 'title' => t('Rules'),
30 'path' => 'admin/settings/sifr/rules',
31 'access' => $access,
32 'callback' => 'sifr_rules',
33 'type' => MENU_DEFAULT_LOCAL_TASK,
34 'weight' => -2,
35 );
36 $items[] = array(
37 'title' => t('Add rule'),
38 'path' => 'admin/settings/sifr/addrule',
39 'access' => $access,
40 'callback' => 'sifr_edit_rule',
41 'type' => MENU_LOCAL_TASK,
42 'weight' => -1,
43 );
44 $items[] = array(
45 'title' => t('Manage fonts'),
46 'path' => 'admin/settings/sifr/manage',
47 'access' => $access,
48 'callback' => 'sifr_manage',
49 'type' => MENU_LOCAL_TASK,
50 'weight' => 2,
51 );
52 // dynamic sifr-screen.css file is stored in files directory
53 $items[] = array(
54 'path' => file_directory_path() .'/sifr/sifr-screen.css',
55 'access' => TRUE,
56 'callback' => 'sifr_css_screen',
57 'type' => MENU_CALLBACK,
58 );
59 $items[] = array(
60 'title' => t('Download sifr file'),
61 'path' => 'sifr/download',
62 'callback' => 'sifr_octet_download',
63 'access' => $access,
64 'type' => MENU_CALLBACK,
65 );
66 }
67 else {
68 $items[] = array(
69 'title' => t('Edit'),
70 'path' => 'admin/settings/sifr/edit',
71 'access' => $access,
72 'callback' => 'sifr_edit_rule',
73 'type' => strpos($_GET['q'], 'admin/settings/sifr/edit') === 0 ? MENU_LOCAL_TASK : MENU_CALLBACK,
74 'weight' => 5,
75 );
76 $items[] = array(
77 'title' => t('Delete rule'),
78 'path' => 'admin/settings/sifr/rules/delete',
79 'access' => $access,
80 'callback' => 'drupal_get_form',
81 'callback arguments' => array('sifr_delete_rule_form'),
82 'type' => MENU_CALLBACK,
83 );
84 $items[] = array(
85 'title' => t('Delete font'),
86 'path' => 'admin/settings/sifr/deletefont',
87 'access' => $access,
88 'callback' => 'sifr_fontdelete_confirm',
89 'type' => strpos($_GET['q'], 'admin/settings/sifr/deletefont') === 0 ? MENU_LOCAL_TASK : MENU_CALLBACK,
90 'weight' => 6,
91 );
92 sifr_all_pages();
93 }
94
95 return $items;
96 }
97
98 /**
99 * List the rules
100 */
101 function sifr_rules() {
102 $rules = sifr_get_rules();
103 $sifrdir = drupal_get_path('module', 'sifr');
104 $header = array(t('Rule Name'), t('CSS Selector'), t('Font'), t('Colors'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
105 $editimg = theme('image', $sifrdir .'/images/editor.png');
106 $delimg = theme('image', $sifrdir .'/images/delete.png');
107 drupal_add_css(drupal_get_path('module', 'sifr') .'/sifr.css', 'module', 'all', FALSE);
108 foreach ($rules as $r) {
109 $fontroot = substr($r['font'], strrpos($r['font'], '/') + 1);
110 $rows[] = array(
111 $r['name'],
112 $r['selector'],
113 $fontroot,
114 "<div style='width:90px; height:16px'>
115 <div class='sifr-font-color' style='background-color: $r[color];' title='". t('font color') ."'> </div>
116 <div class='sifr-font-color' style='background-color: $r[linkcolor];' title='". t('link color') ."'> </div>
117 <div class='sifr-font-color' style='background-color: $r[hovercolor];' title='". t('hover color') ."'> </div>
118 <div class='sifr-font-color' style='background-color: $r[bgcolor];' title='". t('background color') ."'> </div>
119 </div>
120 ",
121 $r['weight'],
122 l($editimg, 'admin/settings/sifr/edit/'. $r['rid'], array('title' => t('edit'), 'class' => 'sifr-rule-op'), NULL, NULL, FALSE, TRUE) . l($delimg, 'admin/settings/sifr/rules/delete/'. $r['rid'], array('title' => t('delete'), 'class' => 'sifr-rule-op'), NULL, NULL, FALSE, TRUE),
123 );
124 }
125 $output .= theme_table($header, $rows, array('style' => 'width: 100%; clear: both;'));
126 $output .= '<p>';
127 $output .= l(t('Add another rule'), 'admin/settings/sifr/addrule') .'</p>';
128 $output .= l(theme('image', $sifrdir .'/images/sifr.png', 'sIFR', 'Link to the sIFR', array()), 'http://www.mikeindustries.com/sifr/', array(), NULL, NULL, FALSE, TRUE);
129
130 return $output;
131 }
132
133 /**
134 * Manage the .swf files
135 */
136 function sifr_manage() {
137 $sifr = str_replace(' ', "%20", sifr_find_sifr());
138
139 drupal_set_title(t('Manage sIFR fonts'));
140
141 $delimg = theme('image', drupal_get_path('module', 'sifr') .'/images/delete.png');
142 $filedir = file_directory_path() .'/sifr';
143
144 $header = array(t('Font'), t('Location'), '');
145 foreach (sifr_get_fonts() as $file => $name) {
146 $editable = strpos($file, $filedir) === FALSE ? FALSE : TRUE;
147 $rows[] = array(
148 $name,
149 $file,
150 $editable ? l($delimg .' '. t('delete'), 'admin/settings/sifr/deletefont/'. urlencode($file), array(), NULL, NULL, FALSE, TRUE) : '',
151 );
152 }
153 $output .= theme('table', $header, $rows, array('style' => 'width:100%'));
154
155 $output .= drupal_get_form('sifr_upload');
156 return $output;
157 }
158
159 function sifr_upload() {
160 $form['#attributes'] = array('enctype' => 'multipart/form-data');
161
162 $instructions = t('
163 <h3>Good places to find sIFR fonts:</h3>
164 <ul>
165 <li><a href="http://sifrfonts.com/">SIFRfonts.com</a></li>
166 <li><a href="http://www.fontsmack.com/">Font Smack</a></li>
167 <li><a href="http://www.isarie.com/?p=17">Stefan Isarie</a></li>
168 </ul>
169 <p>Just download \'em from there and upload \'em here.</p>
170
171 <h3>To create your own sIFR font files:</h3>
172 <ol>
173 <li>Download these files to the same directory: !files</li>
174 <li>Open sifr.fla in Macromedia Flash.</li>
175 <li>Double-click the invisible textbox in the middle of the stage. If the &quot;Properties&quot; palette is not already visible, open it by selecting &quot;Window > Properties&quot;, and select which font you\'d like to use from the drop down menu. If you select a TrueType font, you can also create bold and italic styles for your font by clicking on the &quot;I&quot; or &quot;B&quot; buttons. The standard sifr.fla file contains most of the English characters you will generally need. If you need to embed additional characters or languages, click the &quot;Character&quot; button and select more characters from there.</li>
176 <li>To export the new file, choose &quot;File > Export&quot; and save as fontname.swf</li>
177 <li>Upload the your font file below.</li>
178 </ol>',
179 array(
180 '!files' => l('sifr.fla', 'sifr/download/sifr.fla') .', '. l('customize_me.as', 'sifr/download/customize_me.as') .', '. l('dont_customize_me.as', 'sifr/download/dont_customize_me.as')
181 )
182 );
183 $form['instructions'] = array(
184 '#type' => 'fieldset',
185 '#collapsible' => TRUE,
186 '#collapsed' => TRUE,
187 '#title' => t('Instructions'),
188 '#attributes' => array('style' => 'margin-top: 15px'),
189 );
190 $form['instructions']['instructions'] = array(
191 '#type' => 'markup',
192 '#value' => $instructions,
193 );
194 $form['sifrfile'] = array(
195 '#type' => 'file',
196 '#title' => t('Upload sIFR font file'),
197 '#description' => t('Select the .swf file that you created using the instructions above.'),
198 '#size' => 40,
199 );
200 $form[] = array(
201 '#type' => 'submit',
202 '#value' => t('Submit'),
203 );
204
205 return $form;
206 }
207
208 function sifr_upload_validate($form_id, &$form_values) {
209 // check that it's a swf file
210 }
211
212 function sifr_upload_submit($form_id, $form_values) {
213 $dir = file_create_path('sifr');
214 // creates directory if needed
215 if (file_check_directory($dir, 1)) {
216 if ($file = file_save_upload('sifrfile', 'sifr')) {
217 drupal_set_message(t('New font file uploaded.'));
218 }
219 else {
220 drupal_set_message(t('File could not be saved.'), 'error');
221 }
222 }
223 else {
224 drupal_set_message(t('Cannot create sifr directory in files.'), 'error');
225 }
226 }
227
228 /**
229 * Edit or create a new rule
230 *
231 * @param $edit
232 * an array or object of rule parameters
233 *
234 * @return
235 * the rendered form
236 */
237 function sifr_edit_rule($edit = NULL) {
238 if (is_numeric($edit)) {
239 if ($_POST['op'] == t('Delete')) {
240 drupal_goto('admin/settings/sifr/rules/delete/'. $edit);
241 }
242 else {
243 $rule = sifr_load_rule($edit);
244 }
245 }
246 $font_select = $rule = (array)$rule;
247 $rules = array();
248
249 // Override font colors for font selection.
250 $font_select['color'] = '#000000';
251 $font_select['linkcolor'] = '#000000';
252 $font_select['hovercolor'] = '#000000';
253 $font_select['bgcolor'] = '#FFFFFF';
254 $fonts = sifr_get_fonts();
255 foreach ($fonts as $path => $name) {
256 $font_select['selector'] = "#font-". str_replace(array(' ', '_'), '-', $name);
257 $font_select['font'] = $path;
258 $rules[] = sifr_render_rule_js($font_select);
259 }
260 drupal_add_js(sifr_wrap_rules($rules), 'inline');
261
262 drupal_add_css(drupal_get_path('module', 'sifr') .'/sifr.css', 'module', 'all', FALSE);
263
264 return drupal_get_form('sifr_rule', $rule);
265 }
266
267 function sifr_rule($edit) {
268 if ($edit['rid']) {
269 $form['rid'] = array('#type' => 'hidden', '#value' => $edit['rid']);
270 drupal_set_title(t('Edit sIFR rule %s', array('%s' => $edit['name'])));
271 }
272 else {
273 drupal_set_title(t('Add a new sIFR rule'));
274 }
275 $form['basics'] = array(
276 '#type' => 'fieldset',
277 '#title' => t('Basics'),
278 );
279 $form['basics']['name'] = array(
280 '#type' => 'textfield',
281 '#title' => t('Rule Name'),
282 '#default_value' => $edit['name'],
283 '#required' => TRUE,
284 '#description' => t('The human readable name for this rule.'),
285 );
286
287 $form['basics']['selector'] = array(
288 '#type' => 'textfield',
289 '#title' => t('CSS Selector'),
290 '#default_value' => $edit['selector'],
291 '#description' => t('<div>CSS selector for the elements you want to replace.
292 </div>'),
293 '#required' => TRUE,
294 );
295 $form['basics']['selectorhelp'] = array(
296 '#type' => 'fieldset',
297 '#title' => t('CSS Selector Help'),
298 '#collapsible' => TRUE,
299 '#collapsed' => TRUE,
300 );
301 $form['basics']['selectorhelp']['contents'] = array(
302 '#type' => 'markup',
303 '#value' => t('<div>The supported CSS selectors are <code>#</code>, <code>&gt;</code> and <code>.</code>. Whitespace is used to select descendants. Please use whitespace only for this, so instead of <code>#foo &gt; p</code> use <code>#foo&gt;p</code>. You can use multiple selectors by seperating them with a comma ("<code>,</code>").</div>
304 <div style="margin-top:5px">Example presets based on Garland default elements:
305 <div> - <a href="" onclick="document.getElementById(\'edit-selector\').value = \'#header h1 span\';return false;">site title</a></div>
306 <div> - <a href="" onclick="document.getElementById(\'edit-selector\').value = \'#tabs-wrapper h2\';return false;">page title</a></div>
307 <div> - <a href="" onclick="document.getElementById(\'edit-selector\').value = \'.node h2\';return false;">node titles</a></div>
308 <div> - <a href="" onclick="document.getElementById(\'edit-selector\').value = \'.block h2\';return false;">block titles</a></div>
309 <div> - <a href="" onclick="document.getElementById(\'edit-selector\').value = \'.comment h3\';return false;">comment titles</a></div>
310 Selectors may be dependent on your theme. Examine html source to find appropriate classes and ids being output by your current theme.
311 </div>'),
312 );
313
314 $form['basics']['font'] = array(
315 '#type' => 'fieldset',
316 );
317 $form['basics']['font']['font'] = array(
318 '#title' => t('Font'),
319 '#type' => 'item',
320 '#description' => t("Select a font to use for this rule."),
321 '#required' => TRUE,
322 );
323
324 $fonts = sifr_get_fonts();
325 foreach ($fonts as $path => $name) {
326 $form['basics']['font']['font'][]['font'] = array(
327 '#type' => 'radio',
328 '#title' => '<span id="font-'. str_replace(array(' ', '_'), '-', $name) .'" class="sifr-font">'. $name .'</span>',
329 '#return_value' => $path,
330 '#default_value' => $edit['font'],
331 );
332 }
333
334 $form['colors'] = array(
335 '#type' => 'fieldset',
336 '#title' => t('Colors'),
337 '#collapsible' => TRUE,
338 '#collapsed' => TRUE,
339 '#description' => t('Each rule defines the colors for replaced text. Use six-character hexadecimal (CSS-style) color values preceeded by &quot;<code>#</code>&quot; to define text color, linked text color, hover-over link color, and text background color.'),
340 );
341 $form['colors']['color'] = array(
342 '#type' => 'textfield',
343 '#title' => t('Text Color'),
344 '#size' => 12,
345 '#default_value' => $edit['color'] ? $edit['color'] : '#000000',
346 );
347 $form['colors']['linkcolor'] = array(
348 '#type' => 'textfield',
349 '#title' => t('Link Color'),
350 '#size' => 12,
351 '#default_value' => $edit['linkcolor'] ? $edit['linkcolor'] : '#000000',
352 );
353 $form['colors']['hovercolor'] = array(
354 '#type' => 'textfield',
355 '#title' => t('Hover Color'),
356 '#size' => 12,
357 '#default_value' => $edit['hovercolor'] ? $edit['hovercolor'] : '#666666',
358 );
359 $form['colors']['bgcolor'] = array(
360 '#type' => 'textfield',
361 '#title' => t('Text Background Color'),
362 '#size' => 12,
363 '#default_value' => $edit['bgcolor'] ? $edit['bgcolor'] : '#FFFFFF',
364 );
365 $transparency_options = array(
366 0 => t('No transparency'),
367 1 => t('Use transparency'),
368 2 => t('Opaque background'),
369 );
370 $form['colors']['transparent'] = array(
371 '#title' => t('Transparency'),
372 '#type' => 'radios',
373 '#options' => $transparency_options,
374 '#default_value' => $edit['transparent'],
375 '#description' => t('<strong>Warning:</strong> Flash transparency is not well supported in all browsers and therefore not recommended.'),
376 );
377 $form['tweaks'] = array(
378 '#type' => 'fieldset',
379 '#title' => t('Tweaks'),
380 '#collapsible' => TRUE,
381 '#collapsed' => TRUE,
382 );
383 $form['tweaks']['letterspacing'] = array(
384 '#type' => 'textfield',
385 '#title' => t('Letter Spacing'),
386 '#description' => t('A CSS value to adjust letter spacing on replaced text. Examples: &quot;-5px&quot;, &quot;-.2em&quot;'),
387 '#size' => 12,
388 '#default_value' => $edit['letterspacing'],
389 );
390 $form['tweaks']['fontsize'] = array(
391 '#type' => 'textfield',
392 '#title' => t('Font Size'),
393 '#description' => t('A CSS value to adjust font size on replaced text. Examples: &quot;55px&quot;, &quot;2em&quot;'),
394 '#size' => 12,
395 '#default_value' => $edit['fontsize'],
396 );
397 $form['tweaks']['padding'] = array(
398 '#type' => 'fieldset',
399 '#title' => t('Padding'),
400 '#description' => '<div style="display:block">'. t('If you use padding in the elements you want to replace, you have to set the amount of padding here (in pixels, but without the px part)') .'</div>',
401 '#prefix' => '<div class="container-inline">',
402 '#suffix' => '</div>',
403 );
404 $form['tweaks']['padding']['paddingtop'] = array(
405 '#type' => 'textfield',
406 '#title' => t('top'),
407 '#size' => 6,
408 '#default_value' => $edit['paddingtop'],
409 );
410 $form['tweaks']['padding']['paddingright'] = array(
411 '#type' => 'textfield',
412 '#title' => t('right'),
413 '#size' => 6,
414 '#default_value' => $edit['paddingright'],
415 );
416 $form['tweaks']['padding']['paddingbottom'] = array(
417 '#type' => 'textfield',
418 '#title' => t('bottom'),
419 '#size' => 6,
420 '#default_value' => $edit['paddingbottom'],
421 );
422 $form['tweaks']['padding']['paddingleft'] = array(
423 '#type' => 'textfield',
424 '#title' => t('left'),
425 '#size' => 6,
426 '#default_value' => $edit['paddingleft'],
427 );
428 $form['tweaks']['textalign'] = array(
429 '#type' => 'select',
430 '#title' => t('Text Alignment'),
431 '#default_value' => $edit['textalign'],
432 '#options' => array(
433 'left' => t('Left'),
434 'center' => t('Center'),
435 'right' => t('Right'),
436 ),
437 );
438 $form['tweaks']['lettercase'] = array(
439 '#type' => 'select',
440 '#title' => t('Case Transform'),
441 '#default_value' => $edit['lettercase'],
442 '#options' => array(
443 'normal' => t('Normal'),
444 'upper' => t('Upper-Case'),
445 'lower' => t('Lower-Case'),
446 ),
447 '#description' => t('You can transform the text to be all upper-case or all lower-case'),
448 );
449 $form['tweaks']['underline'] = array(
450 '#type' => 'checkbox',
451 '#title' => t('Underline Links on Hover?'),
452 '#default_value' => $edit['underline'],
453 '#return_value' => 1,
454 );
455
456 $form['weight'] = array(
457 '#type' => 'weight',
458 '#title' => t('Weight'),
459 '#description' => t('You can adjust the order in which the rules are executed by changing their weights. Lighter items are executed before heavier.'),
460 '#delta' => 10,
461 '#default_value' => isset($edit['weight']) ? $edit['weight'] : 0,
462 );
463
464 $form['submits'] = array(
465 '#prefix' => '<div class="container-inline">',
466 '#suffix' => '</div>',
467 );
468 $form['submits'][] = array('#type' => 'submit', '#value' => t('Submit'));
469 if ($edit['rid']) {
470 $form['submits'][] = array('#type' => 'button', '#value' => t('Delete'));
471 }
472
473 return $form;
474 }
475
476 function sifr_rule_submit($form_id, $form) {
477 sifr_save_rule($form);
478 return 'admin/settings/sifr';
479 }
480
481 function sifr_delete_rule_form($rid = NULL) {
482 if (!isset($rid) || !($rule = sifr_load_rule($rid))) {
483 drupal_goto('admin/settings/sifr');
484 }
485 $form = array();
486 $form['rid'] = array(
487 '#type' => 'hidden',
488 '#value' => $rule['rid'],
489 );
490 return confirm_form($form, t('Are you sure you want to delete the rule @rule?', array('@rule' => $rule['name'])), 'admin/settings/sifr');
491 };
492
493 function sifr_delete_rule_form_submit($form_id, $form_values) {
494 if ($form_values['op'] == t('Confirm')) {
495 $rule = sifr_load_rule($form_values['rid']);
496 db_query('DELETE FROM {sifr} WHERE rid = %d', $form_values['rid']);
497 drupal_set_message(t('Rule %name has been deleted.', array('%name' => $rule['name'])));
498 variable_del('sifr_rules');
499 sifr_css_screen(FALSE);
500 }
501 }
502
503 function sifr_fontdelete_confirm() {
504 $clean_url_setting = variable_get('clean_url', 0);
505
506 if ($clean_url_setting) {
507 $fontfile = substr($_GET['q'], 31);
508 }
509 else {
510 $fontfile = arg(4);
511 }
512
513 if (!$fontfile) {
514 drupal_goto('admin/settings/sifr/manage');
515 }
516 elseif (!$clean_url_setting) {
517 $fontfile = urldecode($fontfile);
518 }
519 return drupal_get_form('sifr_fontdelete_confirm_form', $fontfile);
520 }
521
522 function sifr_fontdelete_confirm_form($fontfile) {
523 return confirm_form(array('sifr_fontfile' => array('#type' => 'hidden', '#value' => $fontfile)), t("Are you sure you want to delete the file &quot;%fontfile&quot;?", array('%fontfile' => $fontfile)), 'admin/settings/sifr/manage');
524 }
525
526 function sifr_fontdelete_confirm_form_submit($form_id, $form_values) {
527 if ($form_values['op'] == t('Confirm')) {
528 sifr_font_delete($form_values['sifr_fontfile']);
529 }
530 return 'admin/settings/sifr/manage';
531 }
532
533 /**
534 * Scans files/sifr and modules/sifr/sifr for font files
535 *
536 * @return array
537 * Keyed array for select field
538 */
539 function sifr_get_fonts() {
540 $selects = array();
541
542 // scan files/sifr
543 $dir = file_create_path('sifr');
544 $listings = file_scan_directory($dir, '.*\.(swf)', array('.', '..', 'CVS', '.svn'), 0, FALSE);
545 foreach ((array)$listings as $listing) {
546 $selects[$listing->filename] = $listing->name;
547 }
548 // scan modules/sifr/sifr
549 $dir = sifr_find_sifr();
550 $listings = file_scan_directory($dir, '.*\.(swf)', array('.', '..', 'CVS', '.svn'), 0, FALSE);
551 foreach ((array)$listings as $listing) {
552 $selects[$listing->filename] = $listing->name;
553 }
554 return $selects;
555 }
556
557 /**
558 * Fetch rules from database.
559 *
560 * @return
561 * An array of rules.
562 */
563 function sifr_get_rules() {
564 $rules = array();
565 $result = db_query('SELECT * FROM {sifr} ORDER BY weight');
566 while ($r = db_fetch_array($result)) {
567 $rules[$r['rid']] = $r;
568 }
569 return $rules;
570 }
571
572 /**
573 * Get an individual rule
574 *
575 * @return object
576 * the rule
577 */
578 function sifr_load_rule($rid) {
579 return db_fetch_array(db_query('SELECT * FROM {sifr} WHERE rid = %d', $rid));
580 }
581
582 /**
583 * Save a rule into the db
584 *
585 * @param array $edit
586 * the edit array from the rule add/edit form
587 */
588 function sifr_save_rule($edit) {
589 $edit = (array)$edit;
590 if ($edit['rid']) {
591 db_query('DELETE FROM {sifr} WHERE rid = %d', $edit['rid']);
592 }
593 else {
594 $edit['rid'] = db_next_id('sifr');
595 }
596 foreach ($edit as $key => $val) {
597 if (in_array($key, _sifr_fields())) {
598 // only save keyed values
599 if (!is_numeric($key)) {
600 if (is_numeric($val)) {
601 if (is_float(0 + $val)) {
602 $valsubs[] = '%f';
603 }
604 else {
605 $valsubs[] = '%d';
606 }
607 }
608 else {
609 $valsubs[] = '\'%s\'';
610 }
611 $vals[] = $val;
612 $keys[] = $key;
613 }
614 }
615 }
616 $keys = implode(', ', $keys);
617 $valsubs = implode(', ', $valsubs);
618 db_query("INSERT INTO {sifr} ($keys) VALUES ($valsubs) ", $vals);
619 drupal_set_message(t('%rule saved.', array('%rule' => $edit['name'])));
620 // Create the CSS file.
621 sifr_css_screen(FALSE);
622 variable_del('sifr_rules');
623 }
624
625 function sifr_rule_delete($rid) {
626 db_query('DELETE FROM {sifr} WHERE rid = %d', $rid);
627 drupal_set_message(t('Rule deleted.'));
628 variable_del('sifr_rules');
629 }
630
631 function sifr_font_delete($fontfile) {
632 if (file_delete($fontfile)) {
633 drupal_set_message(t('File deleted.'));
634 }
635 else {
636 drupal_set_message(t('There was a problem deleting the file.'), 'error');
637 }
638 }
639
640 /**
641 * Find the sIFR directory and save its location as a variable
642 *
643 * This removes the need for admins to rename the sIFR directory
644 * when they place it in the module directory
645 *
646 * recurses the directory for this module to find the shallowest
647 * file called sifr.js
648 *
649 * @return unknown
650 */
651 function sifr_find_sifr() {
652 $moddir = drupal_get_path('module', 'sifr');
653 $files = file_scan_directory($moddir, 'sifr\.js', array('.', '..', 'CVS', '.svn'), 0, TRUE, 'filename', 1);
654 // the first one is the shallowest, return that one
655 $file = array_shift($files);
656 $file->dir = substr($file->filename, 0, strlen($file->filename) - strlen($file->basename) - 1);
657 variable_set('sifr_dir', $file->dir);
658 return $file->dir ? $file->dir : FALSE;
659 }
660
661 function sifr_all_pages() {
662 static $done;
663 if ($done) {
664 return;
665 }
666 $sifrdir = variable_get('sifr_dir', FALSE);
667 // no variable set? scan the dir
668 if (!$sifrdir) {
669 $sifrdir = urlencode(sifr_find_sifr());
670 }
671 // still can't find it? display an error
672 if (!$sifrdir) {
673 drupal_set_message(t('The sIFR library is in not installed correctly. Please download from <a href="http://www.mikeindustries.com/sifr/">http://www.mikeindustries.com/sifr/</a> and place in Drupal\'s modules/sifr/ directory.'), 'error');
674 }
675 else {
676 $path = $sifrdir .'/';
677 drupal_add_css(file_directory_path() .'/sifr/sifr-screen.css', 'module', 'screen');
678 drupal_add_css($path .'sIFR-print.css', 'module', 'print');
679 drupal_add_js($path .'sifr.js');
680 drupal_add_js(sifr_render_rules_js(), 'inline');
681 }
682 $done = true;
683 }
684
685 function sifr_render_rules_js() {
686 $rules = variable_get('sifr_rules', null);
687 if (isset($rules)) {
688 return $rules;
689 }
690
691 $rules = array();
692
693 foreach (sifr_get_rules() as $rule) {
694 $rules[] = sifr_render_rule_js($rule);
695 }
696
697 $output = sifr_wrap_rules($rules);
698 variable_set('sifr_rules', $output);
699 return $output;
700 }
701
702 function sifr_render_rule_js($rule) {
703 $properties = array();
704 // convert spaces in filename
705 $fontpath = base_path() . str_replace('%2F', '/', rawurlencode($rule['font']));
706 $properties['sFlashSrc'] = $fontpath;
707 $properties['sColor'] = $rule['color'];
708 $properties['sLinkColor'] = $rule['linkcolor'];
709 $properties['sHoverColor'] = $rule['hovercolor'];
710 $properties['sBgColor'] = $rule['bgcolor'];
711 $properties['nPaddingTop'] = is_numeric($rule['paddingtop']) ? $rule['paddingtop'] : null;
712 $properties['nPaddingRight'] = is_numeric($rule['paddingright']) ? $rule['paddingright'] : null;
713 $properties['nPaddingBottom'] = is_numeric($rule['paddingbottom']) ? $rule['paddingbottom'] : null;
714 $properties['nPaddingLeft'] = is_numeric($rule['paddingleft']) ? $rule['paddingleft'] : null;
715 switch ($rule['transparent']) {
716 case '1':
717 $properties['sWmode'] = 'transparent';
718 break;
719
720 case '2':
721 $properties['sWmode'] = 'opaque';
722 break;
723 }
724 $properties['sCase'] = in_array($rule['lettercase'], array('upper', 'lower')) ? $rule['lettercase'] : '';
725 $vars = array();
726 if ($rule['underline']) {
727 $vars[] = 'underline=true';
728 }
729 if (in_array($rule['textalign'], array('left', 'center', 'right'))) {
730 $vars[] = 'textalign='. $rule['textalign'];
731 }
732 $properties['sFlashVars'] = implode($vars, '&');
733
734 $output = 'sIFR.replaceElement("'. $rule['selector'] .'", named({';
735 foreach ($properties as $property => $value) {
736 if (!empty($value)) {
737 $output .= $property .': "'. $value .'", ';
738 }
739 }
740 $output = substr($output, 0, -2);
741 $output .= '}));';
742
743 return $output;
744 }
745
746 function sifr_wrap_rules($rules) {
747 $output = '';
748 if (is_array($rules) && count($rules)) {
749 $output .= "\n//<![CDATA['\n";
750 $output .= "if(typeof sIFR == \"function\") {\n";
751 foreach ($rules as $rule) {
752 $output .= ' '. $rule ."\n";
753 }
754 $output .= "};\n";
755 $output .= "//]]>\n";
756 }
757
758 return $output;
759 }
760
761 function sifr_css_screen($print = TRUE) {
762 if ($print) {
763 header("Content-type: text/css");
764 header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
765 header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT");
766 header("Cache-Control: no-store, no-cache, must-revalidate");
767 header("Cache-Control: post-check=0, pre-check=0", FALSE);
768 header("Pragma: no-cache");
769 }
770 $output = "/* These are standard sIFR styles... do not modify */
771
772 .sIFR-flash {
773 visibility: visible !important;
774 margin: 0;
775 }
776
777 .sIFR-replaced {
778 visibility: visible !important;
779 }
780
781 span.sIFR-alternate {
782 position: absolute;
783 left: 0;
784 top: 0;
785 width: 0;
786 height: 0;
787 display: block;
788 overflow: hidden;
789 }
790
791 /* Hide Adblock Object tab: the text should show up just fine, not poorly with a tab laid over it. */
792 .sIFR-flash + div[adblocktab=true] {
793 display: none !important;
794 }
795
796 /* These \"decoy\" styles are used to hide the browser text before it is replaced... the negative-letter spacing in this case is used to make the browser text metrics match up with the sIFR text metrics since the sIFR text in this example is so much narrower... your own settings may vary... any weird sizing issues you may run into are usually fixed by tweaking these decoy styles */
797
798 ";
799
800 foreach (sifr_get_rules() as $rule) {
801 $fontsize = trim($rule['fontsize']) ? " font-size: $rule[fontsize];\n" : '';
802 $letterspacing = trim($rule['letterspacing']) ? " letter-spacing: $rule[letterspacing];\n" : '';
803 $rule['selector'] = str_replace(',', ', .sIFR-hasFlash ', $rule['selector']);
804 $output .= "
805 .sIFR-hasFlash $rule[selector] {
806 visibility: hidden;
807 $fontsize$letterspacing}
808 ";
809 }
810 $dir = file_create_path('sifr');
811 if (file_check_directory($dir, 1)) {
812 if ($file = file_save_data($output, $dir .'/sifr-screen.css', FILE_EXISTS_REPLACE)) {
813 drupal_set_message(t('CSS file saved.'));
814 }
815 else {
816 drupal_set_message(t('CSS file could not be saved.'), 'error');
817 }
818 }
819 else {
820 drupal_set_message(t('Cannot create sifr directory in files.'), 'error');
821 }
822 if ($print) {
823 print $output;
824 }
825 }
826
827 /**
828 * Downloads files using applictation/octet-stream mime type
829 * Makes txt files download rather than appearing in browser
830 *
831 * @param string $file
832 * The file to be downloaded
833 */
834 function sifr_octet_download($file) {
835 $filepath = sifr_find_sifr() .'/'. $file;
836 if (is_file($filepath)) {
837 header('Content-type: application/octet-stream');
838 ob_start();
839 include $filepath;
840 $contents = ob_get_contents();
841 ob_end_clean();
842 print $contents;
843 }
844 else {
845 return t('File not found. ');
846 }
847 }
848
849 function _sifr_fields() {
850 return array('rid', 'name', 'font', 'selector', 'color', 'linkcolor', 'hovercolor', 'bgcolor', 'transparent', 'weight', 'letterspacing', 'fontsize', 'paddingtop', 'paddingright', 'paddingbottom', 'paddingleft', 'textalign', 'underline', 'lettercase');
851 }
852

  ViewVC Help
Powered by ViewVC 1.1.2