| 1 |
<?php
|
| 2 |
// $Id: table_altrow.module,v 1.1.2.2 2008/02/17 18:59:11 deviantintegral Exp $
|
| 3 |
/**
|
| 4 |
* table_altrow.module
|
| 5 |
* Insert even and odd classes for tables via input filters to allow for proper
|
| 6 |
* zebra-style striping.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_filter_tips
|
| 11 |
*/
|
| 12 |
function table_altrow_filter_tips($delta, $format, $long = FALSE) {
|
| 13 |
switch ($delta) {
|
| 14 |
case 0:
|
| 15 |
if ($long) {
|
| 16 |
return t('Tables will be rendered with different styles for even and odd rows if supported.');
|
| 17 |
}
|
| 18 |
else {
|
| 19 |
return t('Tables will be rendered with different styles for even and odd rows if supported.');
|
| 20 |
}
|
| 21 |
break;
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Implementation of hook_filter
|
| 27 |
*/
|
| 28 |
function table_altrow_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 29 |
// The "list" operation provides the module an opportunity to declare both how
|
| 30 |
// many filters it defines and a human-readable name for each filter. Note that
|
| 31 |
// the returned name should be passed through t() for translation.
|
| 32 |
if ($op == 'list') {
|
| 33 |
return array(
|
| 34 |
0 => t('Table alternate row filter'),
|
| 35 |
);
|
| 36 |
}
|
| 37 |
|
| 38 |
// All operations besides "list" provide a $delta argument so we know which
|
| 39 |
// filter they refer to. We'll switch on that argument now so that we can
|
| 40 |
// discuss each filter in turn.
|
| 41 |
switch ($delta) {
|
| 42 |
// First we define the simple string substitution filter.
|
| 43 |
case 0:
|
| 44 |
|
| 45 |
switch ($op) {
|
| 46 |
// This description is shown in the administrative interface, unlike the
|
| 47 |
// filter tips which are shown in the content editing interface.
|
| 48 |
case 'description':
|
| 49 |
return t('Adds required attributes to an HTML tag.');
|
| 50 |
|
| 51 |
// We don't need the "prepare" operation for this filter, but it's required
|
| 52 |
// to at least return the input text as-is.
|
| 53 |
case 'prepare':
|
| 54 |
return $text;
|
| 55 |
|
| 56 |
// The actual filtering is performed here. The supplied text should be
|
| 57 |
// returned, once any necessary substitutions have taken place.
|
| 58 |
case 'process':
|
| 59 |
// First, we have to parse the variable.
|
| 60 |
$matches = array();
|
| 61 |
$offset = 0;
|
| 62 |
// Find a tbody
|
| 63 |
while(preg_match('!(<tbody ?.*>)!', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
|
| 64 |
$offset = $matches[0][1];
|
| 65 |
$count = 1;
|
| 66 |
// While the tbody is still open
|
| 67 |
while(preg_match('!(<tr( ?.*)>)|(</tbody>)!', $text, $matches, PREG_OFFSET_CAPTURE, $offset)) {
|
| 68 |
// +1 so we don't match the same string
|
| 69 |
$offset = $matches[0][1] + 1;
|
| 70 |
|
| 71 |
// Don't process tr's until we find a tbody.
|
| 72 |
if($matches[0][0] == '</tbody>') {
|
| 73 |
break;
|
| 74 |
}
|
| 75 |
|
| 76 |
// Don't replace existing classes. Perhaps this should append a class instead?
|
| 77 |
if(!strstr($matches[2][0], 'class=')) {
|
| 78 |
if(($count % 2) == 0) {
|
| 79 |
$new_tag = '<tr class="even"' . $matches[2][0] . '>';
|
| 80 |
$text = str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
|
| 81 |
}
|
| 82 |
else {
|
| 83 |
$new_tag = '<tr class="odd"' . $matches[2][0] . '>';
|
| 84 |
$text = str_replace_count($matches[0][0], $new_tag, $text, $offset - 1, 1);
|
| 85 |
}
|
| 86 |
}
|
| 87 |
$count++;
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
return $text;
|
| 92 |
}
|
| 93 |
break;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Replace every instance of a string with a count parameter like PHP5.
|
| 99 |
* This can probably be removed with Drupal goes to PHP5 only.
|
| 100 |
* Shamelessly stolen and modified from
|
| 101 |
* http://ca.php.net/manual/en/function.str-replace.php#76180
|
| 102 |
*
|
| 103 |
* @param string $needle
|
| 104 |
* @param string $replace
|
| 105 |
* @param string $haystack
|
| 106 |
* @param integer $count
|
| 107 |
* @param boolean $replace_first
|
| 108 |
* @return string
|
| 109 |
*/
|
| 110 |
function str_replace_count($needle, $replace, $haystack, $offset = null, $count = null) {
|
| 111 |
if ($count == null) $count = 0;
|
| 112 |
if ($offset == null) $offset = strpos($haystack, $needle);
|
| 113 |
$rpl_count = 0;
|
| 114 |
while (($offset !== false) && ($rpl_count < $count)) {
|
| 115 |
$haystack = substr_replace($haystack, $replace, $offset, strlen($needle));
|
| 116 |
$offset += strlen($replace);
|
| 117 |
$offset = strpos($haystack, $needle, $offset);
|
| 118 |
$rpl_count++;
|
| 119 |
}
|
| 120 |
return $haystack;
|
| 121 |
}
|