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
Search Repo:
cmsmadesimple-2-0 / admin / addtemplateassoc.php
100644 169 lines (149 sloc) 5.386 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
<?php
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#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$
 
/**
* The goal of this page is to create a CSS association. So firts, what is a css
* association, and how it works.
*
* The idea came that CSS should be broken in small pieces, easily maintanable,
* instead of one big CSS. So, the user created several little CSS pieces :
* - css_menu
* - css_header
* - css_footer
* - css_whatever
*
* There is a table (css_assoc) on the DB with the following fields :
* - assoc_to_id  : the id of the element we associate the CSS with
*           it can be a template id, a page id, a what you want id
* - assoc_css_id  : the id of the CSS we link
* - assoc_type    : what do we link the CSS to ? for the moment, only template
* - create_date  : the create date of this association
* - modified_date  : the modified date of this association (which is not used
*           at the moment.
*
* This page takes arguments as GET variables. 3 arguments are necessary :
* - $id    : refers to "assoc_to_id", the id of the element
* - $css_id  : the id of the CSS we link, refers to "assoc_css_id"
* - $type    : the type of element $id refers to (only template for the
*         moment)
*
* @since  0.6
* @author  calexico
*/
 
  
$CMS_ADMIN_PAGE=1;
 
require_once("../include.php");
 
check_login();
 
#******************************************************************************
# global variables definition
#******************************************************************************
$db = cms_db();
# variable to check if we'll make the association or not
# will be set to false if an error is encountered
$doadd = true;
 
#******************************************************************************
# start of the treatment
#******************************************************************************
if (isset($_POST["template_id"]) && isset($_POST["id"]) && isset($_POST["type"]))
{
 
  # we get the arguments as local vars (easier)
  $template_id = $_POST["template_id"];
  $id    = $_POST["id"];
  $type  = $_POST["type"];
 
  # we then check permissions
  $userid = get_userid();
  $access = check_permission($userid, 'Add Stylesheet Assoc');
 
#******************************************************************************
# the user has permissions, and vars are set, we can go on
#******************************************************************************
  if ($access)
  {
   global $gCms; $db =& $gCms->GetDb();
 
          # first check if this association already exists
   $query = "SELECT * FROM ".cms_db_prefix().
     "css_assoc WHERE assoc_css_id = ? AND assoc_type = ? AND assoc_to_id = ?";
    $result = $db->Execute($query, array($id, $type, $template_id ));
 
    if ($result && $result->RecordCount() > 0)
    {
      $error = lang('associationexists');
      $doadd = false;
    }
 
    # we get the name of the element (for logging)
    if ("template" == $type && $doadd)
    {
      $query = "SELECT css_name FROM ".cms_db_prefix()."css WHERE css_id = ?";
      $result = $db->Execute($query, array($id));
      
      if ($result && $result->RecordCount() > 0)
      {
        $line = $result->FetchRow();
        $name = $line["css_name"];
      }
      else
      {
        $doadd = false;
        $error = lang('invalidtemplate');
      }
    }
 
    # everything is ok, we can insert the element.
    if ($doadd)
    {
      $time = $db->DBTimeStamp(time());
      $query = "INSERT INTO ".cms_db_prefix()."css_assoc (assoc_to_id,assoc_css_id,assoc_type,create_date,modified_date) VALUES (?, ?, ?, ".$time.", ".$time.")";
      $result = $db->Execute($query, array($template_id, $id, $type));
 
      if ($result)
      {
        audit($id, (isset($name)?$name:""), 'Added Stylesheet Association');
 
        if ("template" == $type)
        {
          $time = $db->DBTimeStamp(time());
          $tplquery = "UPDATE ".cms_db_prefix()."templates SET modified_date = ".$time." WHERE template_id = ?";
          $tplresult = $db->Execute($tplquery, array($template_id));
        }
      }
      else
      {
        $doadd = false;
        $error = lang('errorcreatingassociation');
      }
    } # enf od adding query to db
  } # end of "if has access"
  
  # user does not have the right to create association
  else
  {
    $doadd = false;
    $error = lang('noaccessto', array(lang('addcss')));
  }
} # end if vars are set
else
{
  $doadd = false;
  $error = lang('informationmissing');
}
 
#******************************************************************************
# end of treatment, we redirect
#******************************************************************************
if ($doadd)
{
  redirect("templatecss.php?id=$id&type=$type");
}
else
{
  redirect("templatecss.php?id=$id&type=$type&message=$error");
}
 
# vim:ts=4 sw=4 noet
?>