CMS  Version 3.9
template_manager.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 Fakoli::usingFeature("directory_tree");
40 
42 {
43  var $base;
44  var $folder;
45  var $tree;
46 
48  {
49  $this->base = $base;
50  $this->folder = $folder;
51  }
52 
54  {
55  if (basename($folder) == ".svn") return false;
56  return true;
57  }
58 
59  function fileFilter($file)
60  {
61  return endsWith($file, ".css") ||
62  endsWith($file, ".tpl") ||
63  endsWith($file, ".gif") ||
64  endsWith($file, ".png") ||
65  endsWith($file, ".jpg");
66  }
67 
68  function writeScript()
69  {
70  global $dialogs;
71 
72  ob_start();
73 ?>
74 <div class="dialog" id="templateManagerDlg">
75  <div class="dialog_header" id="templateManagerDlgHeader">
76  <div style="padding: 4px;">
77  <div style="float: right">&nbsp;<a id='closeDocumentManagerDlg' href="javascript:templateManagerDlg.hide();"">Close &times;</a></div>
78  <span"style="font-weight: bold" id="templateManagerDlgTitle">Template Manager</span>
79  </div>
80  </div>
81  <div id="templateManagerDlgBody" class="dialog_body">
82  </div>
83 </div>
84 <?
85  $dialogs .= ob_get_contents();
86  ob_end_clean();
87 
88  ob_start();
89 ?>
90 <script type="text/javascript">
91 
92 var templateManagerDlg;
93 
94 window.addEvent('domready', function()
95 {
96  templateManagerDlg = new ModalDialog(document.id('templateManagerDlg'), {width: '690px', handle: document.id('templateManagerDlgHeader'), draggable: true, body: document.id('templateManagerDlgBody')});
97 });
98 
99 function editFile()
100 {
101  file = document.id('files_value').value;
102  document.id('templateManagerDlgTitle').set('text', file);
103  templateManagerDlg.show(function() {}, "/action/template/edit?file=" + encodeURIComponent(file));
104 }
105 
106 function editResult(result)
107 {
108  if (result == "1")
109  {
110  templateManagerDlg.hide();
111  }
112  else
113  {
114  //document.id('Document_form__error').set({'text': result, 'display': 'table-cell'});
115  }
116 }
117 
118 
119 function uploadFile()
120 {
121  selected = document.id('files_value').value;
122 
123  node = document.id(document.id('files_node').value + "_link");
124 
125  var parent_folder = "templates";
126 
127  if (node)
128  {
129  if (node.hasClass("folder"))
130  {
131  parent_folder = selected;
132  }
133  else if (node.hasClass("file"))
134  {
135  parent_folder = dirname(selected);
136  }
137  }
138 
139  templateManagerDlg.show(function() {}, "/action/template/upload?parent_folder=" + encodeURIComponent(parent_folder));
140 }
141 
142 
143 function deleteFile()
144 {
145  if (confirm("Are you sure you want to delete this file?"))
146  {
147  file = document.id('files_value').value;
148 
149  var request = new Request({'url': '/action/template/delete?file=' + encodeURIComponent(file),
150  'method': 'get',
151  'onSuccess': function(responseText)
152  {
153  if (responseText == "1")
154  {
155  window.location.reload();
156  }
157  else
158  {
159  alert(responseText);
160  }
161  },
162  'onFailure': function() { alert("Failed to communicate with server");}});
163  request.send();
164  }
165 }
166 
167 function selectionChanged()
168 {
169  var file = document.id('files_value').value;
170  var node = document.id(document.id('files_node').value + "_link");
171 
172  if (file != "")
173  {
174  if (node.hasClass("folder") || !file.match(/(\.css|\.tpl)$/))
175  {
176  document.id('edit').addProperty("disabled", "disabled");
177  }
178  else
179  {
180  document.id('edit').removeProperty("disabled");
181  }
182  if (node.hasClass("folder"))
183  {
184  document.id('deleteButton').addProperty("disabled", "disabled");
185  }
186  else
187  {
188  document.id('deleteButton').removeProperty("disabled");
189  }
190  if (node.hasClass("file"))
191  {
192  document.id('deleteFolder').addProperty("disabled", "disabled");
193  }
194  else
195  {
196  document.id('deleteFolder').removeProperty("disabled");
197  }
198  }
199  else
200  {
201  document.id('edit').addProperty("disabled", "disabled");
202  document.id('deleteButton').addProperty("disabled", "disabled");
203  }
204 }
205 
206 function createFolder()
207 {
208  templateManagerDlg.show(function() {}, "/action/template/create_folder");
209 }
210 
211 function createFolderResult(result)
212 {
213  if (result == 1)
214  {
215  window.location.reload();
216  }
217  else
218  {
219  alert(result);
220  }
221 }
222 
223 function deleteFolder()
224 {
225  templateManagerDlg.show(function() {}, "/action/template/delete_folder");
226 }
227 
228 function deleteFolderResult(result)
229 {
230  if (result == 1)
231  {
232  window.location.reload();
233  }
234  else
235  {
236  alert(result);
237  }
238 }
239 
240 </script>
241 <?
242  $script .= ob_get_contents();
243  ob_end_clean();
244  return $script;
245  }
246 
247  function drawView()
248  {
249  global $config;
250 
251  $tree = new DirectoryTreeControl("files");
252  $tree->width = 500;
253  $tree->height = 400;
254  $tree->onSelectionChanged = "selectionChanged";
255  $tree->onDoubleClick = "editFile";
256  $tree->permissionCallback = array($this, excludeVersionControl);
257  $tree->icons[".tpl"] = "/fakoli/images/tpl_icon.png";
258  $tree->icons[".css"] = "/fakoli/images/css_icon.png";
259 
260  $tree->selectMode = "both";
261 
262  $tree->buildFolderTree($this->folder, $this->base);
263  $tree->addFiles($this->folder, $this->base, array($this, fileFilter));
264 
265  $tree->children[$this->folder]->open = true;
266  $tree->writeHTML();
267 
268 ?>
269  <br/>
270  <button id="edit" class="button" disabled="disabled" onclick="editFile()">Edit File...</button>&nbsp;&nbsp;
271  <button id="deleteButton" class="button" disabled="disabled" onclick="deleteFile()">Delete File</button>&nbsp;&nbsp;
272  <button id="upload" class="button" onclick="uploadFile()">Upload a File...</button>
273 <?
274  if (checkRole($this->library->manage_folders))
275  {
276 ?>
277  &nbsp;&nbsp;<button id="createFolder" class="button" onclick="createFolder()">Create Folder...</button>&nbsp;&nbsp;
278  <button id="deleteFolder" class="button" onclick="deleteFolder()">Delete Folder</button>
279 <?
280  }
281 
282  }
283 
291  {
292 
293  if (!$_FILES[$field])
294  {
295  die("No upload record found.");
296  }
297 
298  if ($_FILES[$field]["name"]=="")
299  {
300  die("Upload name is empty");
301  }
302 
303  /* Copy across the uploaded file */
304  $folder = sanitizePath($folder);
305 
306  $name = $_FILES[$field]["name"];
307 
308  $filepath = $folder . DIRECTORY_SEPARATOR . $name;
309  $dir = $this->base . DIRECTORY_SEPARATOR . $folder;
310  $fullpath = $dir . DIRECTORY_SEPARATOR . $name;
311 
312  trace("Upload Directory: {$dir}", 3);
313 
314  trace("Uploading file to $fullpath", 3);
315 
316  if (!file_exists($dir))
317  {
318  // If the directory does not exist, create it
319  mkdir($dir);
320  }
321 
322  if (file_exists($fullpath))
323  {
324  // If a previous copy of the file already exists, remove it
325  unlink($fullpath);
326  }
327 
328  move_uploaded_file($_FILES[$field]["tmp_name"], $fullpath);
329  chmod($fullpath, 0755);
330 
331  return str_replace(DIRECTORY_SEPARATOR, "/", $filepath);
332  }
333 
339  function deleteFile($file)
340  {
341  $fullPath = $this->base . DIRECTORY_SEPARATOR . $file;
342  if (file_exists($fullPath))
343  {
344  unlink($fullPath);
345  }
346 
347  return true;
348  }
349 }
350 ?>
& nbsp
Definition: index.inc:49
$helpTree style
Definition: tree.inc:46
$dir
Definition: delete.inc:44
$file
Definition: delete.inc:47
$filepath
Definition: download.inc:42
$name
Definition: upload.inc:54
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
uploadFile($field, $folder)
Upload a file from the specified form field to the specified folder within the current template base.
deleteFile($file)
Delete the specified document (assuming the user has permission).
TemplateManager($base, $folder)
excludeVersionControl($folder)
global $config
Definition: import.inc:4