CMS  Version 3.9
template_select_field_renderer.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 
40 Fakoli::usingFeature("field_renderers");
41 Fakoli::using("site");
42 
43 class TemplateSelectFieldRenderer extends SelectFieldRenderer
44 {
45  static $templateMapWritten = false;
46  var $mode = "site";
47  var $site_id = 0;
48 
49  function TemplateSelectFieldRenderer(&$form, $field, $label, $mode = "site", $onChange = "")
50  {
51  $this->mode = $mode;
52  $this->site_id = $site_id;
53 
54  $options = $this->getInitialOptions($form);
55 
56  $this->SelectFieldRenderer($form, $field, $label, $options, $onChange);
57  }
58 
59  private function getInitialOptions($form)
60  {
61  $site = $this->getSite($form);
62  $directory = $site->getThemeDirectory();
63  $options = $this->readFiles($directory, "");
64  $options = array_merge(array("" => ""), $options);
65 
66  return $options;
67  }
68 
69  private function getSite($form = null)
70  {
71  if (!$form) $form = $this->parent;
72 
73  if ($form->data->hasField("site_id"))
74  {
75  $site_id = $form->data->get("site_id");
76  if ($site_id)
77  {
78  $site = new Site($site_id);
79  return $site;
80  }
81  }
82 
83  $site = Site::getSite();
84  return $site;
85  }
86 
87  private function buildTemplateMap()
88  {
89  $map = array();
90 
91  switch($this->mode)
92  {
93  case "site":
94 
95  $sites = Query::create(Site, "")->execute();
96 
97  foreach($sites as $site)
98  {
99  $directory = $site->getThemeDirectory();
100 
101  $map[$site->site_id] = $this->readFiles($directory, "");
102  }
103  break;
104 
105  case "theme":
106 
107  $themes = SiteManager::getThemes();
108 
109  $map[""] = $this->readFiles(SiteManager::getThemeDirectory(""));
110 
111  foreach($themes as $component => $theme)
112  {
113  $map[$component] = $this->readFiles($theme["directory"]);
114  }
115 
116  break;
117  }
118 
119  return $map;
120  }
121 
122  function readFiles($home, $dir = "")
123  {
124  trace("Scanning Templates", 3);
125 
126  $options = array();
127 
128  $folders = array("$dir");
129 
130  while(($dir = array_shift($folders)) !== null)
131  {
132  $path = "$home/$dir";
133 
134  trace("Reading $path", 3);
135 
136  $handle = opendir($path);
137  while(false !== ($file = readdir($handle)))
138  {
139  trace("Checking $file", 4);
140 
141  if ($file != "." &&
142  $file != ".." &&
143  $file != ".svn" &&
144  $file != "CVS")
145  {
146  $f = "$path/$file";
147  $l = ($dir != '') ? "$dir/$file" : $file;
148 
149  if (is_dir($f))
150  {
151  array_push($folders, $l);
152  }
153  else if (preg_match('/.tpl$/i', $file))
154  {
155  $options[$l] = $l;
156  }
157  }
158  }
159  closedir($handle);
160  }
161  return $options;
162  }
163 
165  {
167  {
168 ?>
169 <script type='text/javascript'>
170 var TemplateSelectFieldRenderer_templateMap = <?echo json_encode($this->buildTemplateMap());?>;
171 </script>
172 <?
174  }
175 
176  switch($this->mode)
177  {
178  case "site":
179 
180  if ($this->parent->data->hasField("site_id"))
181  {
182 ?>
183 <script type='text/javascript'>
184 window.addEvent('domready', function()
185 {
186  var siteSelect = document.id("<?echo $this->parent->id?>_site_id");
187  siteSelect.addEvent('change', function()
188  {
189  var site_id = siteSelect.value;
190  var templateSelect = document.id("<?echo $this->parent->id?>_<?echo $field?>");
191  var val = templateSelect.value;
192 
193  var html = "";
194 
195  var options = TemplateSelectFieldRenderer_templateMap[site_id];
196  for (var o in options)
197  {
198  if (o == val)
199  {
200  html += "<option value='" + o + "' selected='selected'>" + o + "</option>";
201  }
202  else
203  {
204  html += "<option value='" + o + "'>" + o + "</option>";
205  }
206  }
207 
208  templateSelect.set('html', html);
209  });
210 });
211 </script>
212 <?
213  }
214  break;
215 
216  case "theme":
217 
218  if ($this->parent->data->hasField("theme"))
219  {
220 ?>
221 <script type='text/javascript'>
222 window.addEvent('domready', function()
223 {
224  var themeSelect = document.id("<?echo $this->parent->id?>_theme");
225  themeSelect.addEvent('change', function()
226  {
227  var theme = themeSelect.value;
228  var templateSelect = document.id("<?echo $this->parent->id?>_<?echo $field?>");
229  var val = templateSelect.value;
230 
231  var html = "";
232 
233  var options = TemplateSelectFieldRenderer_templateMap[theme];
234  for (var o in options)
235  {
236  if (o == val)
237  {
238  html += "<option value='" + o + "' selected='selected'>" + o + "</option>";
239  }
240  else
241  {
242  html += "<option value='" + o + "'>" + o + "</option>";
243  }
244  }
245 
246  templateSelect.set('html', html);
247  });
248 });
249 </script>
250 <?
251  }
252  break;
253 
254  case "none":
255  default:
256  break;
257  }
258 
259  return parent::renderScript(field);
260  }
261 }?>
$form
$component
Definition: help.inc:38
if(! $page) $path
Definition: page.inc:57
$parent
Definition: templates.inc:42
$dir
Definition: delete.inc:44
$file
Definition: delete.inc:47
$f
Definition: download.inc:46
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
Definition: site.inc:40
static getSite()
Returns the Site object that describes the currently active site (i.e.
Definition: site.inc:103
static getThemes()
static getThemeDirectory($theme="")
TemplateSelectFieldRenderer(&$form, $field, $label, $mode="site", $onChange="")
$mode
Dynamic update mode, either 'site', 'theme' or 'none'.
$table mode
$sites
Definition: event_edit.inc:83
$openData field
if(php_sapi_name() !="cli") $home
Definition: run.php:39