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 / module / class.cms_module_template.php
100644 153 lines (138 sloc) 4.939 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
<?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$
 
class CmsModuleTemplate extends CmsObjectRelationalMapping
{
  var $params = array('id' => -1, 'name' => '', 'module' => '', 'template_type' => '', 'default' => false);
  var $field_maps = array('template_name' => 'name', 'module_name' => 'module', 'default_template' => 'default');
  var $table = 'module_templates';
 
  function __construct()
  {
    parent::__construct();
  }
  
  function validate()
  {
    $this->validate_not_blank('name', lang('nofieldgiven', array(lang('name'))));
    $this->validate_not_blank('content', lang('nofieldgiven', array(lang('content'))));
    if ($this->name != '')
    {
      $result = $this->find_count(array('conditions' => array('template_name = ? and id <> ? and template_type = ? and module_name = ?', $this->name, $this->id, $this->template_type, $this->module)));
      if ($result > 0)
      {
        $this->add_validation_error(lang('templateexists'));
      }
    }
  }
  
  function before_save()
  {
    //Handle some logic for the default template
    $count = $this->find_count(array('conditions' => array('template_type = ? and module_name = ?', $this->template_type, $this->module)));
    if ($count == 0 && $this->default == false)
    {
      $this->default = true;
    }
  }
  
  function after_save()
  {
    //Reset all other templates to have a false default if this one is true
    if ($this->default)
    {
      $cms_db_prefix = CMS_DB_PREFIX;
      cms_db()->Execute("UPDATE {$cms_db_prefix}module_templates SET default_template = 0 WHERE template_type = ? AND module_name = ? AND id <> ?", array($this->template_type, $this->module, $this->id));
    }
  }
  
  /**
   * Get a list of all the modules that have templates installed.
   *
   * @return array The list of modules with templates
   * @author Ted Kulp
   **/
  public static function get_modules()
  {
    $result = array();
    $cms_db_prefix = CMS_DB_PREFIX;
    
    $rows = cms_db()->GetAll("SELECT distinct module_name as module FROM {$cms_db_prefix}module_templates order by module_name");
    if ($rows)
    {
      foreach ($rows as $one_row)
      {
        $result[] = $one_row['module'];
      }
    }
    
    return $result;
  }
  
  /**
   * Get a list of all the template types for the given module name. If no
   * module name is given, then it will return a nested array with module name
   * and a list of templates as their children.
   *
   * @return array The list of templates and optionally, modules
   * @author Ted Kulp
   **/
  public static function get_template_types($module_name = '')
  {
    $result = array();
    $cms_db_prefix = CMS_DB_PREFIX;
    
    if ($module_name != '')
    {
      $rows = cms_db()->GetAll("SELECT distinct template_type as type FROM {$cms_db_prefix}module_templates WHERE module_name = ? order by module_name", array($module_name));
      if ($rows)
      {
        foreach ($rows as $one_row)
        {
          if ($one_row['type'] != null && $one_row['type'] != '')
            $result[] = $one_row['type'];
        }
      }
    }
    else
    {
      $rows = cms_db()->GetAll("SELECT template_type as type, module_name as module FROM {$cms_db_prefix}module_templates group by template_type order by module_name");
      if ($rows)
      {
        foreach ($rows as $one_row)
        {
          if ($one_row['type'] != null && $one_row['type'] != '')
            $result[$one_row['module']][] = $one_row['type'];
        }
      }
    }
    
    return $result;
  }
  
  /**
   * Return a list of all the template objects, nested inside template types, which are in turn
   * nested inside module names. This allows for easy display by using nested foreach statements
   * for display. If you just want the objects themselves, then the regular orm is the way to do.
   *
   * @return array The templates, template types and modules
   * @author Ted Kulp
   **/
  public static function get_all_templates()
  {
    $result = array();
 
    $templates = cms_orm('CmsModuleTemplate')->find_all(array('conditions' => array("template_type <> ?", ''), 'order' => 'module ASC, template_type ASC'));
    foreach ($templates as $one_template)
    {
      $result[$one_template->module][$one_template->template_type][] = $one_template;
    }
    
    return $result;
  }
}
 
# vim:ts=4 sw=4 noet
?>