[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/plugins/serendipity_event_contentrewrite/ -> serendipity_event_contentrewrite.php (source)

   1  <?php # $Id: serendipity_event_contentrewrite.php 1717 2007-06-08 09:21:37Z garvinhicking $
   2  
   3  
   4  if (IN_serendipity !== true) {
   5      die ("Don't hack!");
   6  }
   7  
   8  // Probe for a language include with constants. Still include defines later on, if some constants were missing
   9  $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
  10  if (file_exists($probelang)) {
  11      include $probelang;
  12  }
  13  
  14  include dirname(__FILE__) . '/lang_en.inc.php';
  15  
  16  class serendipity_event_contentrewrite extends serendipity_event
  17  {
  18      var $title = PLUGIN_EVENT_CONTENTREWRITE_NAME;
  19      var $rewrite_from, $rewrite_to;
  20      var $fromstring, $tostring;
  21  
  22      function cleanup() {
  23          // Cleanup. Remove all empty configs on SAVECONF-Submit.
  24          serendipity_plugin_api::remove_plugin_value($this->instance, array('title', 'description'));
  25  
  26          return true;
  27      }
  28  
  29      function introspect(&$propbag)
  30      {
  31          global $serendipity;
  32  
  33          $this->title = $this->get_config('title', $this->title);
  34          $propbag->add('name',          PLUGIN_EVENT_CONTENTREWRITE_NAME);
  35          $propbag->add('description',   PLUGIN_EVENT_CONTENTREWRITE_DESCRIPTION);
  36          $propbag->add('stackable',     true);
  37          $propbag->add('author',        'Garvin Hicking');
  38          $propbag->add('version',       '1.4');
  39          $propbag->add('requirements',  array(
  40              'serendipity' => '0.8',
  41              'smarty'      => '2.6.7',
  42              'php'         => '4.1.0'
  43          ));
  44          $propbag->add('cachable_events', array('frontend_display' => true));
  45          $propbag->add('event_hooks',    array(
  46              'frontend_display' => true
  47          ));
  48  
  49          $propbag->add('groups', array('BACKEND_EDITOR'));
  50  
  51          if (!empty($_POST['SAVECONF'])) $this->cleanup();
  52          $rows = serendipity_db_query("SELECT name, value FROM {$serendipity['dbPrefix']}config WHERE name LIKE '" . $this->instance . "/%' ORDER BY name");
  53  
  54          $this->fromstring = $this->get_config('fromstring', null);
  55          $this->tostring   = $this->get_config('tostring', null);
  56  
  57          if (empty($this->fromstring)) {
  58              $this->fromstring = PLUGIN_EVENT_CONTENTREWRITE_FROM;
  59              $this->set_config('fromstring', $this->fromstring);
  60          }
  61  
  62          if (empty($this->tostring)) {
  63              $this->tostring = PLUGIN_EVENT_CONTENTREWRITE_TO;
  64              $this->set_config('tostring', $this->tostring);
  65          }
  66  
  67          $counter = 0;
  68          $values  = array();
  69          $values[] = 'title';
  70          $values[] = 'rewrite_string';
  71          $values[] = 'rewrite_char';
  72          $this->title_values = array();
  73          $this->descr_values = array();
  74          $this->rewrite_from = array();
  75          $this->rewrite_to   = array();
  76  
  77          if (is_array($rows)) {
  78              foreach ($rows as $row) {
  79                  $plugin_value = str_replace($this->instance . '/', '', $row['name']);
  80                  preg_match('|^(.+)_([0-9]+)$|msU', $plugin_value, $reg_counter);
  81  
  82                  $found = false;
  83                  if ($reg_counter[1] == 'title') {
  84                      $this->title_values[] = $plugin_value;
  85                      $this->rewrite_from[] = $row['value'];
  86                      $found = true;
  87                  } elseif ($reg_counter[1] == 'description') {
  88                      $this->descr_values[] = $plugin_value;
  89                      $this->rewrite_to[]   = $row['value'];
  90                      $found = true;
  91                  }
  92  
  93                  if ($found) {
  94                      $next_counter = $reg_counter[2] + 1;
  95                      $counter      = ($next_counter > $counter ? $next_counter : $counter);
  96                  }
  97              }
  98          }
  99  
 100          $counter        = ($counter > 0 ? $counter + 1 : $counter);
 101  
 102          if (count($this->title_values) == count($this->descr_values)) {
 103              foreach($this->title_values AS $key => $val) {
 104                  $values[] = $this->title_values[$key];
 105                  $values[] = $this->descr_values[$key];
 106              }
 107          }
 108  
 109          $values[] = 'title'       . '_' . $counter;
 110          $values[] = 'description' . '_' . $counter;
 111  
 112          $this->markup_elements = array(
 113              array(
 114                'name'     => 'ENTRY_BODY',
 115                'element'  => 'body',
 116              ),
 117              array(
 118                'name'     => 'EXTENDED_BODY',
 119                'element'  => 'extended',
 120              ),
 121              array(
 122                'name'     => 'COMMENT',
 123                'element'  => 'comment',
 124              ),
 125              array(
 126                'name'     => 'HTML_NUGGET',
 127                'element'  => 'html_nugget',
 128              )
 129          );
 130  
 131          foreach($this->markup_elements as $element) {
 132              $values[] = $element['name'];
 133          }
 134  
 135          $propbag->add('configuration', $values);
 136          $this->counter = $counter;
 137      }
 138  
 139      function example() {
 140          printf(PLUGIN_EVENT_CONTENTREWRITE_REWRITESTRING, $this->fromstring, $this->tostring);
 141      }
 142  
 143      function install() {
 144          serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
 145      }
 146  
 147      function uninstall() {
 148          serendipity_plugin_api::hook_event('backend_cache_purge', $this->title);
 149          serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
 150      }
 151  
 152      function introspect_config_item($name, &$propbag)
 153      {
 154          if (is_array($this->markup_elements)) {
 155              foreach($this->markup_elements as $element) {
 156                  if ($name==$element['name']) {
 157                      $propbag->add('type',        'boolean');
 158                      $propbag->add('name',        constant($name));
 159                      $propbag->add('description', sprintf(APPLY_MARKUP_TO, constant($name)));
 160                      return true;
 161                  }
 162              }
 163          }    
 164  
 165          preg_match('|^(.+)_([0-9]+)$|msU', $name, $switch);
 166  
 167          if (!isset($switch[2])) {
 168              $switch[1] = $name;
 169              $switch[2] = 'empty';
 170          }
 171  
 172          switch($switch[1]) {
 173              case 'title':
 174  
 175                      if ($switch[2] != 'empty') {
 176                          $propbag->add('type',        'string');
 177  
 178                          if ($this->counter == $switch[2]) {
 179                              $propbag->add('name',        PLUGIN_EVENT_CONTENTREWRITE_NEWTITLE);
 180                              $propbag->add('description', PLUGIN_EVENT_CONTENTREWRITE_NEWTDESCRIPTION);
 181                          } else {
 182                              $propbag->add('name',        sprintf(PLUGIN_EVENT_CONTENTREWRITE_OLDTITLE, $switch[2]));
 183                              $propbag->add('description', PLUGIN_EVENT_CONTENTREWRITE_OLDTDESCRIPTION);
 184                          }
 185                      } else {
 186                          $propbag->add('type',        'string');
 187                          $propbag->add('name',        PLUGIN_EVENT_CONTENTREWRITE_PTITLE);
 188                          $propbag->add('description', PLUGIN_EVENT_CONTENTREWRITE_PDESCRIPTION);
 189                      }
 190                      break;
 191  
 192              case 'description':
 193                      $propbag->add('type',        'string');
 194  
 195                      if ($this->counter == $switch[2]) {
 196                          $propbag->add('name',        PLUGIN_EVENT_CONTENTREWRITE_NEWDESCRIPTION);
 197                          $propbag->add('description', PLUGIN_EVENT_CONTENTREWRITE_NEWDDESCRIPTION);
 198                      } else {
 199                          $propbag->add('name',        sprintf(PLUGIN_EVENT_CONTENTREWRITE_OLDDESCRIPTION, $switch[2]));
 200                          $propbag->add('description', PLUGIN_EVENT_CONTENTREWRITE_OLDDDESCRIPTION);
 201                      }
 202                      break;
 203  
 204              case 'rewrite_string':
 205                      $propbag->add('type',         'string');
 206                      $propbag->add('name',         PLUGIN_EVENT_CONTENTREWRITE_REWRITESTRING);
 207                      $propbag->add('description',  PLUGIN_EVENT_CONTENTREWRITE_REWRITESTRINGDESC);
 208                      break;
 209  
 210              case 'rewrite_char':
 211                      $propbag->add('type',         'string');
 212                      $propbag->add('name',         PLUGIN_EVENT_CONTENTREWRITE_REWRITECHAR);
 213                      $propbag->add('description',  PLUGIN_EVENT_CONTENTREWRITE_REWRITECHARDESC);
 214                      break;
 215  
 216              default:
 217                      return false;
 218          }
 219          return true;
 220      }
 221  
 222      function generate_content(&$title)
 223      {
 224          $title = $this->get_config('title', $this->title);
 225  ?>
 226  <table>
 227  <?php
 228      if (is_array($this->rewrite_from)) {
 229              foreach($this->rewrite_from AS $key => $val) {
 230  ?>
 231      <tr>
 232          <th style="font-size: 8pt; font-color: white;"><?php echo htmlspecialchars($val); ?></th>
 233          <td><?php echo htmlspecialchars($this->rewrite_to[$key]); ?></td>
 234      </tr>
 235  <?php
 236              }
 237          }
 238  ?>
 239  </table>
 240  <?php
 241      }
 242  
 243      function event_hook($event, &$bag, &$eventData) {
 244          global $serendipity;
 245  
 246          $hooks = &$bag->get('event_hooks');
 247          if (isset($hooks[$event])) {
 248              switch($event) {
 249                  case 'frontend_display':
 250                      $char   = &$this->get_config('rewrite_char');
 251                      $string = &$this->get_config('rewrite_string');
 252                      foreach($this->rewrite_from AS $nr => $v_from) {
 253                          $v_to   = $this->rewrite_to[$nr];
 254                          if ($v_from != '' && $v_to != '') {
 255                              // Use the supplied rewrite string and replace the {from} and {to} values with the ones we got from the plugin
 256                              $new =
 257                                str_replace(
 258                                  array(
 259                                    '{' . $this->fromstring . '}',
 260                                    '{' . $this->tostring . '}'
 261                                  ),
 262  
 263                                  array(
 264                                    str_replace(
 265                                      $char,
 266                                      '',
 267                                      $v_from
 268                                    ),
 269  
 270                                    $v_to
 271                                  ),
 272  
 273                                  $string
 274                              );
 275  
 276                              // Build a regular expression (ungreedy, multiline) with our quoted value. $val here is the word needing the replacement
 277                              $regex = '°([^\d\w])(' . preg_quote($v_from) . ')([^\d\w])°msU';
 278  
 279                              // \1 and \3 are the prepend/append strings (usually whitespaces) and $new containing the new value
 280                              foreach ($this->markup_elements as $temp) {
 281                                  if (serendipity_db_bool($this->get_config($temp['name'], true)) && isset($eventData[$temp['element']]) &&
 282                                      !$eventData['properties']['ep_disable_markup_' . $this->instance] &&
 283                                      !isset($serendipity['POST']['properties']['disable_markup_' . $this->instance])) {
 284                                      $element = $temp['element'];
 285                                      $eventData[$element] = preg_replace($regex, '\1' . $new . '\3', $eventData[$element]);
 286                                  }
 287                              }
 288  
 289                          }
 290                      }
 291  
 292                      return true;
 293                      break;
 294              }
 295          }
 296      }
 297  }
 298  
 299  /* vim: set sts=4 ts=4 expandtab : */
 300  ?>


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics