XOOPS  2.6.0
xoopseditor.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
22 {
26  const CACHE_KEY_EDITOR_LIST = 'system/editorlist';
27 
31  public $isEnabled;
32 
36  public $configs;
37 
41  public $rootPath;
42 
48  protected $cols;
49 
55  protected $rows;
56 
60  public function __construct()
61  {
62  $this->rows = 5;
63  $this->cols = 50;
64  $this->setAttribute('rows', 5);
65  $this->setAttribute('cols', 50);
66 
67  $args = func_get_args();
68  $configs = array();
69  // For backward compatibility
70  if (!empty($args)) {
71  if (!is_array($args[0])) {
72  $i = 0;
73  foreach (array('caption', 'name', 'value', 'rows', 'cols', 'hiddentext') as $key) {
74  if (isset($args[$i])) {
75  $configs[$key] = $args[$i];
76  }
77  ++$i;
78  }
79  $configs = (isset($args[$i]) && is_array($args[$i])) ? array_merge($configs, $args[$i]) : $configs;
80  } else {
81  $configs = $args[0];
82  }
83  }
84  // TODO: switch to property_exists() as of PHP 5.1.0
85  $vars = get_class_vars(__CLASS__);
86  foreach ($configs as $key => $val) {
87  $method = "set" . ucfirst($key);
88  if (method_exists($this, $method)) {
89  $this->$method($val);
90  } else {
91  if (array_key_exists("_{$key}", $vars)) {
92  $this->{"_{$key}"} = $val;
93  } else {
94  if (array_key_exists($key, $vars)) {
95  $this->{$key} = $val;
96  } else {
97  $this->configs[$key] = $val;
98  }
99  }
100  }
101  }
102  $this->isActive();
103  }
104 
108  public function isActive()
109  {
110  $this->isEnabled = true;
111  return $this->isEnabled;
112  }
113 
118  public function setConfig($options)
119  {
120  foreach ($options as $key => $val) {
121  $this->$key = $val;
122  }
123  }
124 }
125 
136 {
140  public $root_path = "";
141 
145  public $nohtml = false;
146 
150  public $allowed_editors = array();
151 
155  private function __construct()
156  {
157  $this->root_path = \XoopsBaseConfig::get('root-path') . '/class/xoopseditor';
158  }
159 
167  static function getInstance()
168  {
169  static $instance;
170  if (!isset($instance)) {
171  $class = __CLASS__;
172  $instance = new $class();
173  }
174  return $instance;
175  }
176 
185  public function get($name = '', $options = null, $noHtml = false, $OnFailure = '')
186  {
187  if ($editor = $this->_loadEditor($name, $options)) {
188  return $editor;
189  }
190  $list = array_keys($this->getList($noHtml));
191  if (empty($OnFailure) || !in_array($OnFailure, $list)) {
192  $OnFailure = $list[0];
193  }
194  $editor = $this->_loadEditor($OnFailure, $options);
195  return $editor;
196  }
197 
198 
203  public function buildEditorList()
204  {
205  $list = array();
206  $order = array();
207  $fileList = XoopsLists::getDirListAsArray($this->root_path . '/');
208 
209  foreach ($fileList as $item) {
210  if (XoopsLoad::fileExists($file = $this->root_path . '/' . $item . '/language/' . XoopsLocale::getLegacyLanguage() . '.php')) {
211  include_once $file;
212  } else {
213  if (XoopsLoad::fileExists($file = $this->root_path . '/' . $item . '/language/english.php')) {
214  include_once $file;
215  }
216  }
217  if (XoopsLoad::fileExists($file = $this->root_path . '/' . $item . '/editor_registry.php')) {
218  include $file;
219  if (empty($config['order'])) {
220  continue;
221  }
222  $order[] = $config['order'];
223  $list[$item] = array('title' => $config['title'], 'nohtml' => $config['nohtml']);
224  }
225  }
226  array_multisort($order, $list);
227  return $list;
228  }
229 
234  public function getList($noHtml = false)
235  {
237  $list = $xoops->cache()->cacheRead(
239  array($this, 'buildEditorList')
240  );
241  $editors = array_keys($list);
242  if (!empty($this->allowed_editors)) {
243  $editors = array_intersect($editors, $this->allowed_editors);
244  }
245  $returnList = array();
246  foreach ($editors as $name) {
247  if (!empty($noHtml) && empty($list[$name]['nohtml'])) {
248  continue;
249  }
250  $returnList[$name] = $list[$name]['title'];
251  }
252  return $returnList;
253  }
254 
261  {
262  $editor->setConfig($options);
263  }
264 
272  private function _loadEditor($name, $options = null)
273  {
275  $editor = null;
276  if (empty($name) || !array_key_exists($name, $this->getList())) {
277  return $editor;
278  }
279  $editor_path = $this->root_path . '/' . $name;
280  if (XoopsLoad::fileExists($file = $editor_path . '/language/' . XoopsLocale::getLegacyLanguage() . '.php')) {
281  include_once $file;
282  } else {
283  if (XoopsLoad::fileExists($file = $editor_path . '/language/english.php')) {
284  include_once $file;
285  }
286  }
287  if (XoopsLoad::fileExists($file = $editor_path . '/editor_registry.php')) {
288  include $file;
289  } else {
290  return $editor;
291  }
292  if (empty($config['order'])) {
293  return $editor;
294  }
295  include_once $config['file'];
296  $editor = new $config['class']($options);
297  return $editor;
298  }
299 }
$editors
static getLegacyLanguage()
Definition: Abstract.php:76
setConfig(XoopsEditor $editor, $options)
$i
Definition: dialog.php:68
static getInstance()
Definition: Xoops.php:160
if(!isset($xoops->paths[$path_type])) if($path_type== 'var') $file
Definition: browse.php:55
_loadEditor($name, $options=null)
$options['editor']
setAttribute($name, $value=null)
Definition: Attributes.php:42
const CACHE_KEY_EDITOR_LIST
Definition: xoopseditor.php:26
setConfig($options)
$xoops
Definition: admin.php:25
static fileExists($file)
Definition: xoopsload.php:506
static get($name)
getList($noHtml=false)
return $config
$editor