This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathext.nsm_site_generator.php
455 lines (372 loc) · 11.1 KB
/
ext.nsm_site_generator.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require PATH_THIRD.'nsm_site_generator/config.php';
/**
* NSM Site Generator Extension
*
* @package NsmSiteGenerator
* @version 0.0.1
* @author Leevi Graham <http://leevigraham.com>
* @copyright Copyright (c) 2007-2010 Newism <http://newism.com.au>
* @license Commercial - please see LICENSE file included with this distribution
* @link http://expressionengine-addons.com/nsm-site-generator
* @see http://expressionengine.com/public_beta/docs/development/extensions.html
*/
class Nsm_site_generator_ext
{
public $addon_id = NSM_SITE_GENERATOR_ADDON_ID;
public $version = NSM_SITE_GENERATOR_VERSION;
public $name = NSM_SITE_GENERATOR_NAME;
public $description = 'NSM Site Generator extension settings';
public $docs_url = '';
public $settings_exist = TRUE;
public $settings = array();
// At leaset one hook is needed to install an extension
// In some cases you may want settings but not actually use any hooks
// In those cases we just use a dummy hook
public $hooks = array('nsm_site_generator_process_field');
public $default_site_settings = array(
'enabled' => TRUE,
'bundle_server_path' => ''
);
// ====================================
// = Delegate & Constructor Functions =
// ====================================
/**
* PHP5 constructor function.
*
* @access public
* @return void
**/
function __construct() {
$EE =& get_instance();
// define a constant for the current site_id rather than calling $PREFS->ini() all the time
if (defined('SITE_ID') == FALSE) {
define('SITE_ID', $EE->config->item('site_id'));
}
// Load the addons model and check if the the extension is installed
// Get the settings if it's installed
$EE->load->model('addons_model');
if($EE->addons_model->extension_installed($this->addon_id)) {
$this->settings = $this->_getSettings();
}
// Init the cache
$this->_initCache();
}
function nsm_site_generator_process_field($field) {
return $field;
}
/**
* Initialises a cache for the addon
*
* @access private
* @return void
*/
private function _initCache() {
$EE =& get_instance();
// Sort out our cache
// If the cache doesn't exist create it
if (! isset($EE->session->cache[$this->addon_id])) {
$EE->session->cache[$this->addon_id] = array();
}
// Assig the cache to a local class variable
$this->cache =& $EE->session->cache[$this->addon_id];
}
// ===============================
// = Hook Functions =
// ===============================
public function dummy_hook_function(){}
// ===============================
// = Setting Functions =
// ===============================
/**
* Render the custom settings form and processes post vars
*
* @access public
* @return The settings form HTML
*/
public function settings_form() {
$EE =& get_instance();
$EE->lang->loadfile($this->addon_id);
$EE->load->library($this->addon_id."_helper");
$EE->load->helper('form');
// Create the variable array
$vars = array(
'addon_id' => $this->addon_id,
'error' => FALSE,
'input_prefix' => __CLASS__,
'message' => FALSE,
);
// Are there settings posted from the form?
if($data = $EE->input->post(__CLASS__)) {
if(!isset($data["enabled"])) {
$data["enabled"] = TRUE;
}
// No errors ?
if(! $vars['error'] = validation_errors()) {
$this->settings = $this->_saveSettings($data);
$EE->session->set_flashdata('message_success', $this->name . ": ". $EE->lang->line('alert.success.extension_settings_saved'));
$EE->functions->redirect(BASE.AMP.'C=addons_extensions');
}
} else {
// Sometimes we may need to parse the settings
$data = $this->settings;
}
$vars["data"] = $data;
// Return the view.
return $EE->load->view('extension/settings', $vars, TRUE);
}
/**
* Builds default settings for the site
*
* @access private
* @param int $site_id The site id
* @param array The default site settings
*/
private function _buildDefaultSiteSettings($site_id = FALSE) {
$EE =& get_instance();
$site_settings = $this->default_site_settings;
// No site id, use the current one.
if(!$site_id) {
$site_id = SITE_ID;
}
// return settings
return $site_settings;
}
// ===============================
// = Class and Private Functions =
// ===============================
/**
* Called by ExpressionEngine when the user activates the extension.
*
* @access public
* @return void
**/
public function activate_extension() {
$this->_createSettingsTable();
$this->settings = $this->_getSettings();
$this->_registerHooks();
}
/**
* Called by ExpressionEngine when the user disables the extension.
*
* @access public
* @return void
**/
public function disable_extension() {
$this->_unregisterHooks();
}
/**
* Called by ExpressionEngine updates the extension
*
* @access public
* @return void
**/
public function update_extension($current=FALSE){}
// ======================
// = Settings Functions =
// ======================
/**
* The settings table
*
* @access private
**/
private static $settings_table = 'nsm_addon_settings';
/**
* The settings table fields
*
* @access private
**/
private static $settings_table_fields = array(
'id' => array( 'type' => 'int',
'constraint' => '10',
'unsigned' => TRUE,
'auto_increment' => TRUE,
'null' => FALSE),
'site_id' => array( 'type' => 'int',
'constraint' => '5',
'unsigned' => TRUE,
'default' => '1',
'null' => FALSE),
'addon_id' => array( 'type' => 'varchar',
'constraint' => '255',
'null' => FALSE),
'settings' => array( 'type' => 'mediumtext',
'null' => FALSE)
);
/**
* Creates the settings table table if it doesn't already exist.
*
* @access protected
* @return void
**/
protected function _createSettingsTable() {
$EE =& get_instance();
$EE->load->dbforge();
$EE->dbforge->add_field(self::$settings_table_fields);
$EE->dbforge->add_key('id', TRUE);
if (!$EE->dbforge->create_table(self::$settings_table, TRUE)) {
show_error("Unable to create settings table for ".__CLASS__.": " . $EE->config->item('db_prefix') . self::$settings_table);
log_message('error', "Unable to create settings table for ".__CLASS__.": " . $EE->config->item('db_prefix') . self::$settings_table);
}
}
/**
* Get the addon settings
*
* 1. Load settings from the session
* 2. Load settings from the DB
* 3. Create new settings and save them to the DB
*
* @access private
* @param boolean $refresh Load the settings from the DB not the session
* @return mixed The addon settings
*/
private function _getSettings($refresh = FALSE) {
$EE =& get_instance();
$settings = FALSE;
if (
// if there are settings in the settings cache
isset($this->cache[SITE_ID]['settings']) === TRUE
// and we are not forcing a refresh
AND $refresh != TRUE
) {
// get the settings from the session cache
$settings = $this->cache[SITE_ID]['settings'];
} else {
$settings_query = $EE->db->get_where(
self::$settings_table,
array(
'addon_id' => $this->addon_id,
'site_id' => SITE_ID
)
);
// there are settings in the DB
if ($settings_query->num_rows()) {
if ( ! function_exists('json_decode')) {
$$EE->load->library('Services_json');
}
$settings = json_decode($settings_query->row()->settings, TRUE);
$this->_saveSettingsToSession($settings);
log_message('info', __CLASS__ . " : " . __METHOD__ . ' getting settings from session');
}
// no settings for the site
else {
$settings = $this->_buildDefaultSiteSettings(SITE_ID);
$this->_saveSettings($settings);
log_message('info', __CLASS__ . " : " . __METHOD__ . ' creating new site settings');
}
}
// Merge config settings
foreach ($settings as $key => $value) {
if($EE->config->item($this->addon_id . "_" . $key)) {
$settings[$key] = $EE->config->item($this->addon_id . "_" . $key);
}
}
return $settings;
}
/**
* Save settings to DB and to the session
*
* @access private
* @param array $settings
*/
private function _saveSettings($settings) {
$this->_saveSettingsToDatabase($settings);
$this->_saveSettingsToSession($settings);
return $settings;
}
/**
* Save settings to DB
*
* @access private
* @param array $settings
* @return array The settings
*/
private function _saveSettingsToDatabase($settings) {
$EE =& get_instance();
$EE->load->library('javascript');
$data = array(
'settings' => $EE->javascript->generate_json($settings, true),
'addon_id' => $this->addon_id,
'site_id' => SITE_ID
);
$settings_query = $EE->db->get_where(
'nsm_addon_settings',
array(
'addon_id' => $this->addon_id,
'site_id' => SITE_ID
), 1);
if ($settings_query->num_rows() == 0) {
$query = $EE->db->insert('exp_nsm_addon_settings', $data);
log_message('info', __METHOD__ . ' Inserting settings: $query => ' . $query);
} else {
$query = $EE->db->update(
'exp_nsm_addon_settings',
$data,
array(
'addon_id' => $this->addon_id,
'site_id' => SITE_ID
));
log_message('info', __METHOD__ . ' Updating settings: $query => ' . $query);
}
return $settings;
}
/**
* Save the settings to the session
*
* @access private
* @param array $settings The settings to push to the session
* @return array the settings unmodified
*/
private function _saveSettingsToSession($settings) {
$this->cache[SITE_ID]['settings'] = $settings;
return $settings;
}
// ======================
// = Hook Functions =
// ======================
/**
* Sets up and subscribes to the hooks specified by the $hooks array.
*
* @access private
* @param array $hooks A flat array containing the names of any hooks that this extension subscribes to. By default, this parameter is set to FALSE.
* @return void
* @see http://expressionengine.com/public_beta/docs/development/extension_hooks/index.html
**/
private function _registerHooks($hooks = FALSE) {
$EE =& get_instance();
if($hooks == FALSE && isset($this->hooks) == FALSE) {
return;
}
if (!$hooks) {
$hooks = $this->hooks;
}
$hook_template = array(
'class' => __CLASS__,
'settings' => "a:0:{}",
'version' => $this->version,
);
foreach ($hooks as $key => $hook) {
if (is_array($hook)) {
$data['hook'] = $key;
$data['method'] = (isset($hook['method']) === TRUE) ? $hook['method'] : $key;
$data = array_merge($data, $hook);
} else {
$data['hook'] = $data['method'] = $hook;
}
$hook = array_merge($hook_template, $data);
$EE->db->insert('exp_extensions', $hook);
}
}
/**
* Removes all subscribed hooks for the current extension.
*
* @access private
* @return void
* @see http://expressionengine.com/public_beta/docs/development/extension_hooks/index.html
**/
private function _unregisterHooks() {
$EE =& get_instance();
$EE->db->where('class', __CLASS__);
$EE->db->delete('exp_extensions');
}
}