CMS  Version 3.9
setting_auto_form.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("auto_form");
40 
41 class SettingAutoForm extends AutoForm
42 {
43  var $settings;
44  var $customSettings = array();
45  var $filter = null;
47  {
48  $this->filter = $filter;
49 
50  ComponentManager::fireEvent("RegisterSettingsFormExtension");
51 
52  $this->component = $component;
53 
54  $this->settings = Query::create(Settings, "WHERE component=:component ORDER BY category, weight, name")
55  ->bind(":component", $component->name)
56  ->execute();
57 
58  $this->buildForm($component->component_id);
59 
60  }
61 
62  function buildForm($component_id = 0)
63  {
64  $componentSettings = new ComponentSettings();
65 
66  $fields = array();
67 
68  foreach($this->settings as $setting)
69  {
70  if ($this->filter && $this->filter->isExcluded($setting->name)) continue;
71 
72  $fields[$setting->name] = ($setting->field_type) ? $setting->field_type : "String";
73  if ($setting->weight < 0) $this->hide($setting->name);
74  if ($setting->category) $this->group($setting->category, $setting->name);
75 
76  }
77 
78  $componentSettings->fields = $fields;
79  parent::AutoForm($componentSettings, "POST", "?component_id={$component_id}");
80 
81  foreach($this->settings as $setting)
82  {
83  if ($this->filter && $this->filter->isExcluded($setting->name)) continue;
84 
85  $this->data->set($setting->name, $setting->value);
86  if($setting->annotation)
87  $this->annotate($setting->name, $setting->annotation);
88 
89  /*
90  * Create custom renderers. If option is not
91  * a custom renderer name, then assume it is
92  * a set of options for SelectFieldRenderer.
93  */
94  if($setting->options == "FileUploadFieldRenderer")
95  {
96  $renderers[$setting->settings_id] = new FileUploadFieldRenderer($this, $setting->name, prettify($setting->name), uploadSettingFile);
97  $this->annotate($setting->name, "<a href='/action/settings/download?setting_id={$setting->settings_id}'>{$setting->value}</a>");
98 
99  }
100  else if ($setting->options == "Permissions")
101  {
102  $renderer = new ChecklistFieldRenderer($this, $setting->name, $setting->annotation ? $setting->annotation : prettify($setting->name), SiteRole::getRolesArray());
103  if ($setting->annotation) $this->annotate($setting->name, "");
104  $renderer->dropdown = true;
105  $renderer->setSize(200, 100);
106  $renderer->dropdownMaxWidth = 500;
107  $renderer->dropdownMessage = "Select User Roles";
108  }
109  else if($setting->options)
110  {
111  $optionList = $setting->formatOptionList();
112  if(isset($optionList))
113  $renderers[$setting->settings_id] = new SelectFieldRenderer($this, $setting->name, prettify($setting->name), $optionList);
114  }
115  }
116 
117  $extension = SettingsManager::getExtension($this->component->name);
118  if ($extension)
119  {
120  call_user_func($extension, $this);
121  }
122 
123  $this->submitLabel = "Save Changes";
124  }
125 
126  function writeSettings($settings)
127  {
128  foreach($settings as $setting)
129  {
130  if(array_key_exists($setting->field_type, Settings::$customTypes))
131  {
132  $type = Settings::$customTypes[$setting->field_type];
134  }
135  else
136  $type = ($setting->field_type) ? $setting->field_type : "String";
137 
138  $fields[$setting->name] = $type;
139  }
140 
141  return $fields;
142  }
143 
144  function save()
145  {
146  $this->patchUpReferences();
147 
148  $this->configureValidators();
149 
150  $obj =& $this->data;
151  $obj->fromPOST();
152 
153  if (isset($_POST["#FORMID#"]))
154  {
155  $this->id = $_POST["#FORMID#"];
156  }
157 
158  $this->msg = $this->validator->validate();
159  if ($this->msg != "") return false;
160 
161  $this->preProcessFields($pk, $filter);
162 
163  $indexedSettings = reindexList($this->settings, "name");
164 
165  foreach($obj->getFields() as $name => $type)
166  {
167  if(array_key_exists($name, $indexedSettings))
168  {
169  $setting = $indexedSettings[$name];
170  $setting->value = $obj->$name;
171  $setting->filter = new InclusionFilter("value");
172  $setting->save();
173  Cache::put("setting_{$setting->component}_{$setting->name}", $setting->value);
174 
175  }
176  }
177  Cache::invalidate("auto_form_defaults");
178  Cache::invalidate("config");
179 
180  // onComplete event is fired once all processing has been completed
181 
182  if ($this->onSaveComplete)
183  {
184  call_user_func_array($this->onSaveComplete, array($this));
185  }
186 
187  trace("Settings auto form returning true", 3);
188 
189  ComponentManager::fireEventTo($this->component->name, "ClearCache");
190 
191  return true;
192  }
193 }
194 
196 {
197  global $config;
198 
199  trace("uploadSettingFile called for $field", 3);
200 
201  if (!$_FILES[$field])
202  {
203  trace("No upload record for $field", 2);
204  return false;
205  }
206  if ($_FILES[$field]["name"]=="")
207  {
208  trace("Upload name is empty", 2);
209  return false;
210  }
211 
212  /* Copy across the uploaded file */
213 
214  trace("Upload Base: {$config['uploadbase']}", 3);
215  trace("Upload Directory: {$config['uploaddir']}", 3);
216 
217  $dir = $config["uploaddir"];
218  $name = $_FILES[$field]["name"];
219  $file = "$dir/$name";
220 
221  trace ("Uploading file to {$config['uploadbase']}/$file", 3);
222 
223  if (!file_exists("{$config['uploadbase']}/$dir"))
224  {
225  // If the directory does not exist, create it
226  mkdir("{$config['uploadbase']}/$dir");
227  }
228  else if (file_exists("{$config['uploadbase']}/$file"))
229  {
230  // If a previous copy of the file already exists, remove it
231  unlink("{$config['uploadbase']}/$file");
232  }
233 
234  move_uploaded_file($_FILES[$field]["tmp_name"], "{$config['uploadbase']}/$file");
235  chmod("{$config['uploadbase']}/$file", 0755);
236 
237  $setting->$field = "$file";
238 
239  return true;
240 }
241 
242 
243 /*
244  * Shows all settings on one page.
245  */
247 {
249  {
250  $this->settings = $settings;
251  $this->buildForm();
252  }
253 }?>
$table filter
$_POST["owner_id"]
Definition: blog_form.inc:54
if(!Settings::getValue("blog", "enable_extended_subscription_fields")) $form submitLabel
$component_id
$component
Definition: help.inc:38
$dir
Definition: delete.inc:44
$file
Definition: delete.inc:47
$name
Definition: upload.inc:54
$extension
Definition: upload.inc:70
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
static fireEventTo($event, $component, $parameter=null)
Fire an event to the specificed component.
Dummy data model to render a component's settings as a form, using the "type" field of the setting re...
Definition: settings.inc:379
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
buildForm($component_id=0)
writeSettings($settings)
SettingAutoForm($component, $filter=null)
The Settings class provides components with a common API for specifying application settings and conf...
Definition: settings.inc:49
static getExtension($component)
static getRolesArray()
Retrieves the array of roles and their names for the current site for use in a CMS form.
Definition: role.inc:75
if( $method=="POST")
$form onSaveComplete
uploadSettingFile($field, $setting)
global $config
Definition: import.inc:4
$renderer
$setting