public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / lib / classes / class.cms_template_operations.php
100644 304 lines (250 sloc) 7.361 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
/**
* Static methods for handling templates.
*
* @author Ted Kulp
* @since 2.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license GPL
**/
class CmsTemplateOperations extends CmsObject
{
  static private $instance = NULL;
 
  function __construct()
  {
    parent::__construct();
  }
  
  /**
   * Get an instance of this object, though most people should be using
   * the static methods instead. This is more for compatibility than
   * anything else.
   *
   * @return CmsTemplateOperations The instance of the singleton object.
   * @author Ted Kulp
   **/
  static public function get_instance()
  {
    if (self::$instance == NULL)
    {
      self::$instance = new CmsTemplateOperations();
    }
    return self::$instance;
  }
 
  function LoadTemplates()
  {  
    return cmsms()->template->find_all(array('order' => 'name ASC'));
  }
 
  function LoadTemplateByID($id)
  {
    return cmsms()->template->find_by_id($id);
  }
 
  function LoadTemplateByContentAlias($alias)
  {
    return cmsms()->template->find_by_query("SELECT t.* FROM ".cms_db_prefix()."templates t INNER JOIN ".cms_db_prefix()."content c ON c.template_id = t.template_id WHERE (c.content_alias = ? OR c.content_id = ?) AND c.active = 1", array($alias, $alias));
  }
 
  function LoadTemplateAndContentDates($alias)
  {
    $result = array();
 
    $query = "SELECT c.modified_date AS c_date, t.modified_date AS t_date FROM ".cms_db_prefix()."templates t INNER JOIN ".cms_db_prefix()."content c ON c.template_id = t.template_id WHERE (c.content_alias = ? OR c.content_id = ?) AND c.active = 1";
    $dbresult = cms_db()->Execute($query, array($alias, $alias));
 
    while ($dbresult && !$dbresult->EOF)
    {
      $result[] = $dbresult->fields['c_date'];
      $result[] = $dbresult->fields['t_date'];
      $dbresult->MoveNext();
    }
    
    if ($dbresult) $dbresult->Close();
 
    return $result;
  }
 
  function LoadDefaultTemplate()
  {
    return cmsms()->template->find_by_default(1);
  }
 
  function UsageCount($id)
  {
    return cmsms()->content->find_count_by_template_id($id);
  }
 
  function InsertTemplate($template)
  {
    $result = -1;
 
    $dbresult = $template->save();
 
    if ($dbresult !== false)
    {
      $result = $template->id;
      CmsContentOperations::do_cross_reference($result, 'template', $template->content);
    }
 
    return $result;
  }
 
  function UpdateTemplate($template)
  {
    $result = false;
 
    $dbresult = $template->save();
 
    if ($dbresult !== false)
    {
      $result = true;
      CmsContentOperations::do_cross_reference($template->id, 'template', $template->content);
    }
 
    return $result;
  }
 
  function DeleteTemplateByID($id)
  {
    return cmsms()->template->delete($id);
  }
 
  function CountPagesUsingTemplateByID($id)
  {
    $result = 0;
 
        $query = "SELECT count(*) AS count FROM ".cms_db_prefix()."content WHERE template_id = ?";
        $row = cms_db()->GetRow($query,array($id));
 
    if ($row)
    {
      if (isset($row["count"]))
      {
        $result = $row["count"];
      }
    }
 
    return $result;
  }
 
  function StylesheetsUsed()
  {
    $result = 0;
 
        $query = "SELECT count(*) AS count FROM ".cms_db_prefix()."templates WHERE stylesheet is not null and stylesheet != ''";
        $row = cms_db()->GetRow($query);
 
    if ($row)
    {
      if (isset($row["count"]))
      {
        $result = $row["count"];
      }
    }
 
    return $result;
  }
 
  function TouchAllTemplates($blob_name='')
  {
    $result = false;
 
    $dbresult = false;
 
    $time = cms_db()->DBTimeStamp(time());
    if ($blob_name != '')
    {
      $query = "UPDATE ".cms_db_prefix()."templates SET modified_date = ".$time." WHERE template_content like ?";
      $dbresult = cms_db()->Execute($query,array('%{html_blob name="'.$blob_name.'"}%'));
    }
    else
    {
      $query = "UPDATE ".cms_db_prefix()."templates SET modified_date = ".$time;
      $dbresult = cms_db()->Execute($query);
    }
 
    if ($dbresult !== false)
    {
      $result = true;
    }
 
    return $result;
  }
 
  function CheckExistingTemplateName($name)
  {
    $result = false;
 
    $query = "SELECT id from ".cms_db_prefix()."templates WHERE template_name = ?";
    $row = cms_db()->GetRow($query,array($name));
 
    if ($row)
    {
      $result = true;
    }
 
    return $result;
  }
  
  function TemplateDropdown($id = 'template_id', $selected_id = -1, $othertext = '', $show_hidden = false)
  {
    $result = "";
    
    $templateops = cmsms()->GetTemplateOperations();
 
    $alltemplates = $templateops->LoadTemplates();
    
    if (count($alltemplates) > 0)
    {
      $result .= '<select name="'.$id.'"';
      if ($othertext != '')
      {
        $result .= ' ' . $othertext;
      }
      $result .= '>';
      #$result .= '<option value="">Select Template</option>';
      foreach ($alltemplates as $onetemplate)
      {
        if ($onetemplate->active == true || $show_hidden == true)
        {
          $result .= '<option value="'.$onetemplate->id.'"';
          if ($onetemplate->id == $selected_id || ($selected_id == -1 && $onetemplate->default == true))
          {
            $result .= ' selected="selected"';
          }
          $result .= '>'.$onetemplate->name.'</option>';
        }
      }
      $result .= '</select>';
    }
    
    return $result;
  }
  
  function parse_content_blocks_from_template(&$template)
  {
    $blocks = array();
    
    if ($template != null)
    {    
      $pattern = '/{content([^}]*)}/';
      $pattern2 = '/([a-zA-z0-9]*)=["\']([^"\']+)["\']/';
    
      $matches = array();
      $result = preg_match_all($pattern, $template->content, $matches);
 
      if ($result && count($matches[1]) > 0)
      {
        foreach ($matches[1] as $wholetag)
        {
         $id = 'default';
         $name = 'default';
        
          $morematches = array();
          $result2 = preg_match_all($pattern2, $wholetag, $morematches);
          if ($result2)
          {
            $keyval = array();
            for ($i = 0; $i < count($morematches[1]); $i++)
            {
              $keyval[$morematches[1][$i]] = $morematches[2][$i];
            }
 
            foreach ($keyval as $key=>$val)
            {
              switch($key)
              {
                case 'block':
                case 'name':
                  $id = strtolower(str_replace(' ', '_', $val));
                  $name = $val;
                  break;
              }
            }
          }
        
          $blocks[$name]['id'] = $id;
        }
      }
    }
    
    return $blocks;
  }
}
 
class TemplateOperations extends CmsTemplateOperations
{
}
 
# vim:ts=4 sw=4 noet
?>