-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
550 lines (447 loc) · 12.2 KB
/
Module.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
namespace GO\Base;
/**
* Group-Office
*
* Copyright Intermesh BV.
* This file is part of Group-Office. You should have received a copy of the
* Group-Office license along with Group-Office. See the file /LICENSE.TXT
*
* If you have questions write an e-mail to info@intermesh.nl
*
* @license AGPL/Proprietary http://www.group-office.com/LICENSE.TXT
* @link http://www.group-office.com
* @copyright Copyright Intermesh BV
* @version $Id: Number.php 7962 2011-08-24 14:48:45Z mschering $
* @author Merijn Schering <mschering@intermesh.nl>
* @package GO.base
*/
/**
* Module manager
*
* This class is used to manage a module. It performs tasks such as
* installing, uninstalling and initializing.
*
* @author Merijn Schering <mschering@intermesh.nl>
* @version $Id: config.class.inc.php 7687 2011-06-23 12:00:34Z mschering $
* @copyright Copyright Intermesh BV.
* @package GO.base
*/
class Module extends Observable {
const PACKAGE_UNSUPPORTED = '3rd party (Not supported by Intermesh)';
const PACKAGE_COMMUNITY = 'Community (AGPL)';
const PACKAGE_CUSTOM = 'Custom made';
const PACKAGE_IN_DEVELOPMENT = 'In development';
private $_id;
/**
* Get the id of the module which is identical to
* the folder name in the modules folder.
*
* eg. notes, calendar etc.
* @return string
*/
public function id() {
if(!isset($this->_id)){
$className = get_class($this);
$arr = explode('\\', $className);
$this->_id=strtolower($arr[1]);
}
return $this->_id;
}
public function setId($id){
$this->_id=$id;
}
/**
* Get the absolute filesystem path to the module.
*
* @return string
*/
public function path(){
return \GO::config()->root_path . 'modules/' . $this->id() . '/';
}
/**
* Return the localized name
*
* @return String
*/
public function name() {
$name = \GO::t('name', $this->id());
if($name=='name')
$name = $this->id();
return $name;
}
/**
* Get URL to module icon
*
* @return string
*/
public function icon(){
$icon = $this->_findIconByTheme(\GO::user()->theme);
if(!$icon)
$icon = $this->_findIconByTheme("Default");
if(!$icon)
$icon = \GO::config()->host.'views/Extjs3/themes/Default/images/16x16/unknown.png';
return $icon;
}
public function package(){
return self::PACKAGE_COMMUNITY;
}
private function _findIconByTheme($theme){
$path = $this->path();
if(file_exists($path.'/themes/'.$theme.'/images/'.$this->id().'.png')){
return \GO::config()->host.'modules/'.$this->id().'/themes/'.$theme.'/images/'.$this->id().'.png';
}elseif(file_exists($path.'views/Extjs3/themes/'.$theme.'/images/'.$this->id().'.png')){
return \GO::config()->host.'modules/'.$this->id().'/views/Extjs3/themes/'.$theme.'/images/'.$this->id().'.png';
} else {
return false;
}
}
/**
* Return the localized description
*
* @return String
*/
public function description() {
return \GO::t('description', $this->id());
}
/**
* Return the name of the author.
*
* @return String
*/
public function author(){
return '';
}
/**
* Return the e-mail address of the author.
*
* @return String
*/
public function authorEmail(){
return 'info@intermesh.nl';
}
/**
* Return copyright information
*
* @return String
*/
public function copyright(){
return 'Copyright Intermesh BV';
}
/**
* Return true if this module belongs in the admin menu.
*
* @return boolean
*/
public function adminModule(){
return false;
}
/**
* Return true if this module has a GUI
*
* @return boolean
*/
public function hasInterface(){
return true;
}
/**
* Return true if this module can be bought in the Group-Office app center
*
* @return boolean
*/
public function appCenter(){
return false;
}
/**
* Automatically install this module on installation.
*
* @return boolean
*/
public function autoInstall(){
return false;
}
/**
* Return an array of modules this module depends on.
*
* @return array
*/
public function depends(){
return array();
}
/**
* Override this function if for some reason this module can becomes
* unavailable.
*
* @return boolean
*/
public function isAvailable(){
return true;
}
/**
* Return false is for some reason this module is not instalable.
*
* @return boolean
*/
public function isInstallable(){
return true;
}
/**
* Find the module manager class by id.
*
* @param string $moduleId eg. "addressbook"
* @return \Module|boolean
*/
public static function findByModuleId($moduleId){
$className = 'GO\\'.ucfirst($moduleId).'\\'.ucfirst($moduleId).'Module';
if(class_exists($className))
return new $className;
else{
$modMan = new Module();
$modMan->setId($moduleId);
return $modMan;
}
}
/**
* Return the number of update queries.
*
* @return integer
*/
public function databaseVersion(){
$updatesFile = $this->path() . 'install/updates.php';
if(!file_exists($updatesFile))
$updatesFile = $this->path() . 'install/updates.inc.php';
return Util\Common::countUpgradeQueries($updatesFile);
}
public function checkDependenciesForInstallation($modulesToBeInstalled=array()){
$depends = $this->depends();
foreach($depends as $moduleId){
if(!\GO::modules()->isInstalled($moduleId) && !in_array($moduleId,$modulesToBeInstalled)){
$moduleNames = array();
foreach($depends as $moduleId){
$modManager = Module::findByModuleId($moduleId);
$moduleNames[]=$modManager ? $modManager->name () : $moduleId;
}
throw new \Exception("Module ".$this->name()." depends on ".implode(",",$moduleNames).". Please make sure all dependencies are installed.");
}
}
}
public function getDependencies(){
$depends = $this->depends();
$moduleIds = array();
foreach($depends as $moduleId){
if(!($module = \GO::modules()->isInstalled($moduleId)) || $module->isAvailable()){
foreach($depends as $moduleId){
$moduleIds[]=$moduleId;
}
}
}
return $moduleIds;
}
/**
* Installs the module's tables etc
*
* @return boolean
*/
public function install() {
$sqlFile = $this->path().'install/install.sql';
try{
if(file_exists($sqlFile))
{
$queries = Util\SQL::getSqlQueries($sqlFile);
foreach($queries as $query)
\GO::getDbConnection ()->query($query);
}
}catch(PDOException $e){
throw new \Exception("SQL query failed: ".$query."\n\n".$e->getMessage());
}
\GO::clearCache();
//call saveUser for each user
// $stmt = Model\User::model()->find(array('ignoreAcl'=>true));
// while($user = $stmt->fetch()){
// call_user_func(array(get_class($this),'saveUser'), $user, true);
// }
return true;
}
/**
* Delete's the module's tables etc.
*
* @return boolean
*/
public function uninstall() {
$oldIgnore = \GO::setIgnoreAclPermissions();
// //call deleteUser for each user
// $stmt = Model\User::model()->find(array('ignoreAcl'=>true));
// while($user = $stmt->fetch()){
// call_user_func(array(get_class($this),'deleteUser'), $user);
// }
//Uninstall cron jobs for this module
$cronClasses = $this->findClasses('cron');
foreach($cronClasses as $class){
$jobs = Cron\CronJob::model()->findByAttribute('job', $class->getName());
foreach($jobs as $job)
$job->delete();
}
//delete all models from the Model\ModelType table.
//They are used for faster linking and search cache. Each linkable model is mapped to an id in this table.
$models = $this->getModels();
$modelTypes = array();
foreach($models as $model){
$modelType = Model\ModelType::model()->findSingleByAttribute('model_name', $model->getName());
if($modelType){
$modelTypes[]=$modelType->id;
$modelType->delete();
}
}
if(!empty($modelTypes)){
$sql = "DELETE FROM `go_search_cache` WHERE model_type_id IN (".implode(',', $modelTypes).")";
\GO::getDbConnection()->query($sql);
$stmt = GO::getDbConnection()->query('SHOW TABLES');
while ($r = $stmt->fetch()) {
$tableName = $r[0];
if (substr($tableName, 0, 9) == 'go_links_' && !is_numeric(substr($tableName, 9, 1))) {
$sql = "DELETE FROM `$tableName` WHERE model_type_id IN (".implode(',', $modelTypes).")";
\GO::getDbConnection()->query($sql);
}
}
}
$sqlFile = $this->path().'install/uninstall.sql';
if(file_exists($sqlFile))
{
$queries = Util\SQL::getSqlQueries($sqlFile);
foreach($queries as $query)
\GO::getDbConnection ()->query($query);
}
\GO::clearCache();
\GO::setIgnoreAclPermissions($oldIgnore);
return true;
}
/**
* This class can be overriden by a module class to add listeners to objects
* that extend the Observable class.
*
* eg. Model\User::model()->addListener('save','SomeClass','someStaticFunction');
*
*/
public static function initListeners() {
}
/**
* This function is called when the first request is made to the module.
* Useful to check for a default calendar, tasklist etc.
*
* The response is added to the controller action parameters with index
* 'firstRun'.
*/
public static function firstRun(){
return '';
}
/**
* This function is called when the search index needs to be rebuilt.
*
* You want to use MyModel::model()->rebuildSearchCache();
*
* @param array $response Array of output lines
*/
public function buildSearchCache(&$response){
$response[] = "Building search cache for ".$this->id()."\n";
$models=$this->getModels();
foreach($models as $model){
if($model->isSubclassOf("GO\Base\Db\ActiveRecord")){
echo $response[] = "Processing ".$model->getName()."\n";
$stmt = \GO::getModel($model->getName())->rebuildSearchCache();
}
}
}
/**
* This function is called when a database check is performed
*
* @param array $response Array of output lines
*/
public function checkDatabase(&$response){
//echo "<pre>";
echo "Checking database for ".$this->id()."\n";
$models=$this->getModels();
foreach($models as $model){
if($model->isSubclassOf("GO\Base\Db\ActiveRecord")){
$m = \GO::getModel($model->getName());
if($m->checkDatabaseSupported()){
echo "Checking ".$model->getName()."\n";
flush();
$stmt = $m->find(array(
'ignoreAcl'=>true
));
$stmt->callOnEach('checkDatabase');
unset($stmt);
}else
{
echo "No check needed for ".$model->getName()."\n";
flush();
}
}
}
}
/**
* Get all model class names.
*
* @return ReflectionClass[] Names of all model classes
*/
public function getModels(){
$models=array();
$classes=$this->findClasses('model');
foreach($classes as $class){
if(!$class->isAbstract()){
$models[] = $class;
}
}
return $models;
}
/**
* Find all classes in a folder.
*
* @param string $subfolder
* @return \ReflectionClass array
*/
public function findClasses($subfolder){
$classes=array();
$folder = new Fs\Folder($this->path().$subfolder);
if($folder->exists()){
$items = $folder->ls();
foreach($items as $item){
if($item instanceof Fs\File){
$subParts = explode('/', $subfolder);
$subParts=array_map("ucfirst", $subParts);
$className = 'GO\\'.ucfirst($this->id()).'\\'.implode('\\',$subParts).'\\'.$item->nameWithoutExtension();
if(class_exists($className)){
$reflectionClass = new \ReflectionClass($className);
if(!$reflectionClass->isAbstract())
$classes[] = $reflectionClass;
}
}
}
}
return $classes;
}
/**
* Called when the main settings are loaded.
*
* @param \GO\Core\Controller\Settings $settingsController
* @param array $params Request params
*
* $params['id'] is the current logged in user id.
*
* @param array $response
*/
public static function loadSettings(&$settingsController, &$params, &$response, $user){
}
/**
* Called when the main settings are submitted.
*
* @param \GO\Core\Controller\Settings $settingsController
* @param array $params Request params
*
* $params['id'] is the current logged in user id.
*
* @param array $response
*/
public static function submitSettings(&$settingsController, &$params, &$response, $user){
}
}