[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> adminfunctions_templates.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: adminfunctions_templates.php 5297 2010-12-28 22:01:14Z Tomm $
  10   */
  11  
  12  /**
  13   * Find and replace a string in a particular template through every template set.
  14   *
  15   * @param string The name of the template
  16   * @param string The regular expression to match in the template
  17   * @param string The replacement string
  18   * @param int Set to 1 to automatically create templates which do not exist for that set (based off master) - Defaults to 1
  19   * @return bolean true if updated one or more templates, false if not.
  20   */
  21  
  22  function find_replace_templatesets($title, $find, $replace, $autocreate=1)
  23  {
  24      global $db, $mybb;
  25      
  26      $return = false;
  27      
  28      $template_sets = array(-2, -1);
  29      
  30      // Select all global with that title
  31      $query = $db->simple_select("templates", "tid, template", "title = '".$db->escape_string($title)."' AND sid='-1'");
  32      while($template = $db->fetch_array($query))
  33      {
  34          // Update the template if there is a replacement term or a change
  35          $new_template = preg_replace($find, $replace, $template['template']);
  36          if($new_template == $template['template'])
  37          {
  38              continue;
  39          }
  40          
  41          // The template is a custom template.  Replace as normal.
  42          $updated_template = array(
  43              "template" => $db->escape_string($new_template)
  44          );
  45          $db->update_query("templates", $updated_template, "tid='{$template['tid']}'");
  46      }
  47      
  48      // Select all other modified templates with that title
  49      $query = $db->simple_select("templates", "tid, sid, template", "title = '".$db->escape_string($title)."' AND sid > 0");
  50      while($template = $db->fetch_array($query))
  51      {
  52          // Keep track of which templates sets have a modified version of this template already
  53          $template_sets[] = $template['sid'];
  54          
  55          // Update the template if there is a replacement term or a change
  56          $new_template = preg_replace($find, $replace, $template['template']);
  57          if($new_template == $template['template'])
  58          {
  59              continue;
  60          }
  61          
  62          // The template is a custom template.  Replace as normal.
  63          $updated_template = array(
  64              "template" => $db->escape_string($new_template)
  65          );
  66          $db->update_query("templates", $updated_template, "tid='{$template['tid']}'");
  67          
  68          $return = true;
  69      }
  70      
  71      // Add any new templates if we need to and are allowed to
  72      if($autocreate != 0)
  73      {
  74          // Select our master template with that title
  75          $query = $db->simple_select("templates", "title, template", "title='".$db->escape_string($title)."' AND sid='-2'", array('limit' => 1));
  76          $master_template = $db->fetch_array($query);
  77          $master_template['new_template'] = preg_replace($find, $replace, $master_template['template']);
  78          
  79          if($master_template['new_template'] != $master_template['template'])
  80          {
  81              // Update the rest of our template sets that are currently inheriting this template from our master set            
  82              $query = $db->simple_select("templatesets", "sid", "sid NOT IN (".implode(',', $template_sets).")");
  83              while($template = $db->fetch_array($query))
  84              {
  85                  $insert_template = array(
  86                      "title" => $db->escape_string($master_template['title']),
  87                      "template" => $db->escape_string($master_template['new_template']),
  88                      "sid" => $template['sid'],
  89                      "version" => $mybb->version_code,
  90                      "status" => '',
  91                      "dateline" => TIME_NOW
  92                  );
  93                  $db->insert_query("templates", $insert_template);
  94                  
  95                  $return = true;
  96              }
  97          }
  98      }
  99      
 100      return $return;
 101  }
 102  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1