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 / admin / assignstylesheets.php
100644 187 lines (162 sloc) 5.027 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
<?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
#
 
$CMS_ADMIN_PAGE=1;
 
require_once("../include.php");
 
$id_to_assign = coalesce_key($_REQUEST, 'id_to_assign', '');
$template_id = coalesce_key($_REQUEST, 'template_id', '');
$stylesheet_id = coalesce_key($_REQUEST, 'stylesheet_id', '');
$type = coalesce_key($_REQUEST, 'type', ($stylesheet_id != '') ? 'stylesheet' : 'template');
 
if (isset($_REQUEST['cancel']))
{
  if ($type == 'stylesheet')
  {
    redirect('liststylesheets.php');
  }
  else
  {
    redirect('listtemplates.php');
  }
}
 
check_login();
 
//TODO: Check permissions
$modify_layout = true;
 
if (isset($_REQUEST['submitbutton']) && $modify_layout && $id_to_assign != '')
{
  if ($type == 'template')
  {
    $template = cms_orm('template')->find_by_id($template_id);
    if ($template)
    {
      $template->assign_stylesheet_by_id($id_to_assign);
    }
  }
  else if ($type == 'stylesheet')
  {
    $stylesheet = cms_orm('stylesheet')->find_by_id($stylesheet_id);
    if ($stylesheet)
    {
      $stylesheet->assign_template_by_id($id_to_assign);
    }
  }
}
 
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'remove' && $modify_layout && $type != '')
{
  if ($type == 'template')
  {
    $template = cms_orm('template')->find_by_id($template_id);
    if ($template)
    {
      $template->remove_assigned_stylesheet_by_id($stylesheet_id);
    }
  }
  else if ($type == 'stylesheet')
  {
    $stylesheet = cms_orm('stylesheet')->find_by_id($stylesheet_id);
    if ($stylesheet)
    {
      $stylesheet->remove_assigned_template_by_id($template_id);
    }
  }
}
 
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'move' && $modify_layout)
{
  $assoc = cms_orm('template_stylesheet_association')->find_by_id($_REQUEST['assoc_id']);
  if ($assoc != null)
  {
    if (isset($_REQUEST['dir']) && $_REQUEST['dir'] == 'up')
    {
      $assoc->move_up();
    }
    else if (isset($_REQUEST['dir']) && $_REQUEST['dir'] == 'down')
    {
      $assoc->move_down();
    }
  }
}
 
include_once("header.php");
 
$smarty = cms_smarty();
$smarty->assign('modify_layout', $modify_layout);
$smarty->assign('header_name', $themeObject->ShowHeader('assignedstylesheets'));
local_setup_smarty($page, $modify_layout, $type);
$smarty->display('assignstylesheets.tpl');
 
include_once("footer.php");
 
function orm_array_udiff_compare($a, $b)
{
  if ($a->id === $b->id)
  {
    return 0;
  }
  return ($a->id > $b->id) ? 1 : -1;
}
 
function local_setup_smarty($page, $modify_layout, $type)
{
  $smarty = cms_smarty();
  $userid = get_userid();
  
  $template_id = coalesce_key($_REQUEST, 'template_id', '');
  $stylesheet_id = coalesce_key($_REQUEST, 'stylesheet_id', '');
  
  $smarty->assign('template_id', $template_id);
  $smarty->assign('stylesheet_id', $stylesheet_id);
  
  //Are we assigning to a template or stylesheet?
  if ($type == 'template')
  {
    $template = cms_orm('template')->find_by_id($template_id);
    if ($template)
    {
      $assigned_stylesheets = $template->stylesheets;
      $all_stylesheets = cms_orm('stylesheet')->find_all(array('order' => 'name ASC'));
      $unassigned_stylesheets = array_udiff($all_stylesheets, $assigned_stylesheets->children, 'orm_array_udiff_compare');
      
      $smarty->assign('assigned', $template->stylesheet_associations);
      $smarty->assign('all', $all_stylesheets);
      
      //Generate stuff for dropdown
      $dd_stuff = array();
      foreach ($unassigned_stylesheets as $obj)
      {
        $dd_stuff[$obj->id] = $obj->name;
      }
      $smarty->assign('unassigned', $dd_stuff);
      
      $smarty->assign('type', 'template');
    }
  }
  else if ($type == 'stylesheet')
  {
    $stylesheet = cms_orm('stylesheet')->find_by_id($stylesheet_id);
    if ($stylesheet)
    {
      $assigned_templates = $stylesheet->templates;
      $all_templates = cms_orm('template')->find_all(array('order' => 'name ASC'));
      $unassigned_templates = array_udiff($all_templates, $assigned_templates->children, 'orm_array_udiff_compare');
      
      $smarty->assign('assigned', $stylesheet->template_associations);
      $smarty->assign('all', $all_templates);
      
      //Generate stuff for dropdown
      $dd_stuff = array();
      foreach ($unassigned_templates as $obj)
      {
        $dd_stuff[$obj->id] = $obj->name;
      }
      $smarty->assign('unassigned', $dd_stuff);
      
      $smarty->assign('type', 'stylesheet');
    }
  }
  else
  {
    return;
  }
}
 
# vim:ts=4 sw=4 noet
?>