1
|
|
|
<?php |
2
|
|
|
//------------------------------------------------------------------------- |
3
|
|
|
// OVIDENTIA http://www.ovidentia.org |
4
|
|
|
// Ovidentia is free software; you can redistribute it and/or modify |
5
|
|
|
// it under the terms of the GNU General Public License as published by |
6
|
|
|
// the Free Software Foundation; either version 2, or (at your option) |
7
|
|
|
// any later version. |
8
|
|
|
// |
9
|
|
|
// This program is distributed in the hope that it will be useful, but |
10
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12
|
|
|
// See the GNU General Public License for more details. |
13
|
|
|
// |
14
|
|
|
// You should have received a copy of the GNU General Public License |
15
|
|
|
// along with this program; if not, write to the Free Software |
16
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
17
|
|
|
// USA. |
18
|
|
|
//------------------------------------------------------------------------- |
19
|
|
|
/** |
20
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL) |
21
|
|
|
* @copyright Copyright (c) 2018 by CANTICO ({@link http://www.cantico.fr}) |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
require_once dirname(__FILE__) . '/define.php'; |
25
|
|
|
require_once dirname(__FILE__) . '/functions.php'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Provides extensible functionalities to manage an application. |
29
|
|
|
*/ |
30
|
|
|
class Func_App extends bab_Functionality |
31
|
|
|
{ |
32
|
|
|
public $addonPrefix; |
33
|
|
|
public $classPrefix; |
34
|
|
|
public $addonName; |
35
|
|
|
public $controllerTg; |
36
|
|
|
public $phpPath; |
37
|
|
|
public $recordSetPath; |
38
|
|
|
public $ctrlPath; |
39
|
|
|
public $uiPath; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $language = null; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
$this->addonPrefix = 'libapp'; |
50
|
|
|
$this->addonName = 'libapp'; |
51
|
|
|
|
52
|
|
|
$this->classPrefix = $this->addonPrefix . '_'; |
53
|
|
|
$this->controllerTg = 'addon/' . $this->addonName . '/main'; |
54
|
|
|
|
55
|
|
|
$addon = bab_getAddonInfosInstance($this->addonName); |
56
|
|
|
$this->phpPath = $addon->getPhpPath(); |
57
|
|
|
$this->recordSetPath = $this->phpPath; |
58
|
|
|
$this->ctrlPath = $this->phpPath; |
59
|
|
|
$this->uiPath = $this->phpPath . 'ui/'; |
60
|
|
|
|
61
|
|
|
$babDB = bab_getDB(); |
62
|
|
|
|
63
|
|
|
$LibOrm = bab_Functionality::get('LibOrm'); |
64
|
|
|
/*@var $LibOrm Func_LibOrm */ |
65
|
|
|
|
66
|
|
|
$LibOrm->initMysql(); |
67
|
|
|
ORM_RecordSet::setBackend(new ORM_MySqlBackend($babDB)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $class |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
|
|
public function loadObject($class) |
76
|
|
|
{ |
77
|
|
|
if (substr($class, 0, strlen($this->classPrefix)) === $this->classPrefix) { |
78
|
|
|
if (substr($class, -3) === 'Set') { |
79
|
|
|
$file = strtolower(substr($class, strlen($this->classPrefix), -3)) . '.class.php'; |
80
|
|
|
if (file_exists($this->recordSetPath . $file)) { |
81
|
|
|
require $this->recordSetPath . $file; |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$file = strtolower(substr($class, strlen($this->classPrefix))) . '.class.php'; |
86
|
|
|
if (file_exists($this->recordSetPath . $file)) { |
87
|
|
|
require $this->recordSetPath . $file; |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getUiPath() |
100
|
|
|
{ |
101
|
|
|
return APP_UI_PATH; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getSetPath() |
108
|
|
|
{ |
109
|
|
|
return APP_SET_PATH; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getCtrlPath() |
116
|
|
|
{ |
117
|
|
|
return APP_CTRL_PATH; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getPhpPath() |
124
|
|
|
{ |
125
|
|
|
return APP_PHP_PATH; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
* |
132
|
|
|
*/ |
133
|
|
|
public function getDescription() |
134
|
|
|
{ |
135
|
|
|
return 'Application framework.'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the addon name |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getAddonName() |
144
|
|
|
{ |
145
|
|
|
return $this->addonName; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return bab_addonInfos |
151
|
|
|
*/ |
152
|
|
|
public function getAddon() |
153
|
|
|
{ |
154
|
|
|
return bab_getAddonInfosInstance($this->getAddonName()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Register myself as a functionality. |
160
|
|
|
* |
161
|
|
|
*/ |
162
|
|
|
public static function register() |
163
|
|
|
{ |
164
|
|
|
require_once $GLOBALS['babInstallPath'].'utilit/functionalityincl.php'; |
165
|
|
|
$functionalities = new bab_functionalities(); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$addon = bab_getAddonInfosInstance('libapp'); |
168
|
|
|
|
169
|
|
|
$addon->registerFunctionality('App', 'app.php'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Synchronize sql tables for all classes found in Application object |
175
|
|
|
* using methods 'includeXxxxxClassName' |
176
|
|
|
* sychronize if the two correspond methods are met and the set classname match the prefix from parameter |
177
|
|
|
* do not synchronize if the set method has a VueSet suffix, in this case the table si juste a readonly vue |
178
|
|
|
* |
179
|
|
|
* @param string $prefix tables and classes prefix |
180
|
|
|
* @return bab_synchronizeSql |
181
|
|
|
*/ |
182
|
|
|
public function synchronizeSql($prefix) |
183
|
|
|
{ |
184
|
|
|
if (!$prefix) { |
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
189
|
|
|
$mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']); |
190
|
|
|
$sql = 'SET FOREIGN_KEY_CHECKS=0; |
191
|
|
|
'; |
192
|
|
|
|
193
|
|
|
foreach (get_class_methods($this) as $method) { |
194
|
|
|
|
195
|
|
|
if (substr($method, 0, 7) === 'include' && substr($method, -3) === 'Set') { |
196
|
|
|
$incl = $method; |
197
|
|
|
$classNameMethod = substr($method, strlen('include')) . 'ClassName'; |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
$classname = $this->$classNameMethod(); |
201
|
|
|
|
202
|
|
|
if ($prefix === substr($classname, 0, strlen($prefix)) |
203
|
|
|
&& 'Set' === substr($classname, -3) |
204
|
|
|
&& 'ViewSet' !== substr($classname, -7)) { |
205
|
|
|
if (method_exists($this, $incl)) { |
206
|
|
|
$this->$incl(); |
207
|
|
|
|
208
|
|
|
$call = substr($classname, strlen($prefix)); |
209
|
|
|
|
210
|
|
|
if (class_exists($classname)) { |
211
|
|
|
|
212
|
|
|
/* @var $set ORM_RecordSet */ |
213
|
|
|
$set = $this->$call(); |
214
|
|
|
if (method_exists($set, 'useLang')) { |
215
|
|
|
// This is necessary if the recordSet constructor uses a setLang(). |
216
|
|
|
// We need to revert to multilang fields before synchronizing. |
217
|
|
|
$set->useLang(false); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
$sql .= $mysqlbackend->setToSql($set) . "\n"; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
227
|
|
|
$synchronize = new bab_synchronizeSql(); |
228
|
|
|
|
229
|
|
|
$synchronize->fromSqlString($sql); |
230
|
|
|
|
231
|
|
|
return $synchronize; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
public function getSynchronizeSql($prefix) |
237
|
|
|
{ |
238
|
|
|
if (!$prefix) { |
239
|
|
|
return null; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
require_once $GLOBALS['babInstallPath'].'utilit/devtools.php'; |
243
|
|
|
$mysqlbackend = new ORM_MySqlBackend($GLOBALS['babDB']); |
244
|
|
|
$sql = ''; |
245
|
|
|
|
246
|
|
|
foreach (get_class_methods($this) as $method) { |
247
|
|
|
|
248
|
|
|
if (substr($method, 0, strlen('include')) === 'include' && substr($method, -strlen('Set')) === 'Set') { |
249
|
|
|
$incl = $method; |
250
|
|
|
$classNameMethod = substr($method, strlen('include')) . 'ClassName'; |
251
|
|
|
|
252
|
|
|
$classname = $this->$classNameMethod(); |
253
|
|
|
|
254
|
|
|
if ($prefix === substr($classname, 0, strlen($prefix)) |
255
|
|
|
&& 'Set' === substr($classname, -3) |
256
|
|
|
&& 'ViewSet' !== substr($classname, -6) |
257
|
|
|
) { |
258
|
|
|
if (method_exists($this, $incl)) { |
259
|
|
|
$this->$incl(); |
260
|
|
|
$call = substr($classname, strlen($prefix)); |
261
|
|
|
|
262
|
|
|
if (class_exists($classname) && method_exists($this, $call)) { |
263
|
|
|
$set = $this->$call(); |
264
|
|
|
$sql .= $mysqlbackend->setToSql($set) . "\n"; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $sql; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
|
276
|
|
|
public function includeBase() |
277
|
|
|
{ |
278
|
|
|
require_once APP_PHP_PATH . 'base.class.php'; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Includes app_RecordSet class definition. |
283
|
|
|
*/ |
284
|
|
|
public function includeRecordSet() |
285
|
|
|
{ |
286
|
|
|
require_once APP_SET_PATH . 'record.class.php'; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Includes app_TraceableRecordSet class definition. |
292
|
|
|
*/ |
293
|
|
|
public function includeTraceableRecordSet() |
294
|
|
|
{ |
295
|
|
|
require_once APP_SET_PATH . 'traceablerecord.class.php'; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* SET DEFINITION |
302
|
|
|
*/ |
303
|
|
|
|
304
|
|
|
//Log |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Includes LogSet class definition. |
308
|
|
|
*/ |
309
|
|
|
public function includeLogSet() |
310
|
|
|
{ |
311
|
|
|
require_once APP_SET_PATH . 'log.class.php'; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public function LogClassName() |
318
|
|
|
{ |
319
|
|
|
return 'app_Log'; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return string |
324
|
|
|
*/ |
325
|
|
|
public function LogSetClassName() |
326
|
|
|
{ |
327
|
|
|
return $this->LogClassName() . 'Set'; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return app_LogSet |
332
|
|
|
*/ |
333
|
|
|
public function LogSet() |
334
|
|
|
{ |
335
|
|
|
$this->includeLogSet(); |
336
|
|
|
$className = $this->LogSetClassName(); |
337
|
|
|
$set = new $className($this); |
338
|
|
|
return $set; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
//Link |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Includes LinkSet class definition. |
346
|
|
|
*/ |
347
|
|
|
public function includeLinkSet() |
348
|
|
|
{ |
349
|
|
|
require_once APP_SET_PATH . 'link.class.php'; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @return string |
354
|
|
|
*/ |
355
|
|
|
public function LinkClassName() |
356
|
|
|
{ |
357
|
|
|
return 'app_Link'; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @return string |
362
|
|
|
*/ |
363
|
|
|
public function LinkSetClassName() |
364
|
|
|
{ |
365
|
|
|
return $this->LinkClassName() . 'Set'; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return app_LinkSet |
370
|
|
|
*/ |
371
|
|
|
public function LinkSet() |
372
|
|
|
{ |
373
|
|
|
$this->includeLinkSet(); |
374
|
|
|
$className = $this->LinkSetClassName(); |
375
|
|
|
$set = new $className($this); |
376
|
|
|
return $set; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
//CustomField |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Includes CustomFieldSet class definition. |
383
|
|
|
*/ |
384
|
|
|
public function includeCustomFieldSet() |
385
|
|
|
{ |
386
|
|
|
require_once APP_SET_PATH . 'customfield.class.php'; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @return string |
391
|
|
|
*/ |
392
|
|
|
public function CustomFieldClassName() |
393
|
|
|
{ |
394
|
|
|
return 'app_CustomField'; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @return string |
399
|
|
|
*/ |
400
|
|
|
public function CustomFieldSetClassName() |
401
|
|
|
{ |
402
|
|
|
return $this->CustomFieldClassName() . 'Set'; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @return app_CustomFieldSet |
407
|
|
|
*/ |
408
|
|
|
public function CustomFieldSet() |
409
|
|
|
{ |
410
|
|
|
$this->includeCustomFieldSet(); |
411
|
|
|
$className = $this->CustomFieldSetClassName(); |
412
|
|
|
$set = new $className($this); |
413
|
|
|
return $set; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
|
417
|
|
|
//CustomSection |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Includes CustomSectionSet class definition. |
421
|
|
|
*/ |
422
|
|
|
public function includeCustomSectionSet() |
423
|
|
|
{ |
424
|
|
|
require_once APP_SET_PATH . 'traceablerecord.class.php'; |
425
|
|
|
require_once APP_SET_PATH . 'customsection.class.php'; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @return string |
430
|
|
|
*/ |
431
|
|
|
public function CustomSectionClassName() |
432
|
|
|
{ |
433
|
|
|
return 'app_CustomSection'; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @return string |
438
|
|
|
*/ |
439
|
|
|
public function CustomSectionSetClassName() |
440
|
|
|
{ |
441
|
|
|
return $this->CustomSectionClassName() . 'Set'; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @return app_CustomSectionSet |
446
|
|
|
*/ |
447
|
|
|
public function CustomSectionSet() |
448
|
|
|
{ |
449
|
|
|
$this->includeCustomSectionSet(); |
450
|
|
|
$className = $this->CustomSectionSetClassName(); |
451
|
|
|
$set = new $className($this); |
452
|
|
|
return $set; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
|
456
|
|
|
//Tag |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Includes TagSet class definition. |
460
|
|
|
*/ |
461
|
|
|
public function includeTagSet() |
462
|
|
|
{ |
463
|
|
|
require_once APP_SET_PATH . 'tag.class.php'; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @return string |
468
|
|
|
*/ |
469
|
|
|
public function TagClassName() |
470
|
|
|
{ |
471
|
|
|
return 'app_Tag'; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @return string |
476
|
|
|
*/ |
477
|
|
|
public function TagSetClassName() |
478
|
|
|
{ |
479
|
|
|
return $this->TagClassName() . 'Set'; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @return app_TagSet |
|
|
|
|
484
|
|
|
*/ |
485
|
|
|
public function TagSet() |
486
|
|
|
{ |
487
|
|
|
$this->includeTagSet(); |
488
|
|
|
$className = $this->TagSetClassName(); |
489
|
|
|
$set = new $className($this); |
490
|
|
|
return $set; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
|
494
|
|
|
|
495
|
|
|
|
496
|
|
|
|
497
|
|
|
|
498
|
|
|
|
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Returns the app_Record corresponding to the specified |
502
|
|
|
* reference $ref. |
503
|
|
|
* |
504
|
|
|
* @param string $ref A reference string (e.g. Contact:12) |
505
|
|
|
* @return app_Record or null if no corresponding record is found. |
506
|
|
|
*/ |
507
|
|
|
public function getRecordByRef($ref) |
508
|
|
|
{ |
509
|
|
|
$refParts = explode(':', $ref); |
510
|
|
|
if (count($refParts) !== 2) { |
511
|
|
|
return null; |
512
|
|
|
} |
513
|
|
|
list($classname, $id) = $refParts; |
514
|
|
|
$classSet = $classname . 'Set'; |
515
|
|
|
$set = $this->$classSet(); |
516
|
|
|
if (isset($set)) { |
517
|
|
|
return $set->get($id); |
518
|
|
|
} |
519
|
|
|
return null; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Returns the reference corresponding to the specified |
524
|
|
|
* app_Record $record (e.g. Contact:12 or Deal:125) |
525
|
|
|
* |
526
|
|
|
* @param app_Record $record |
527
|
|
|
* @return string |
528
|
|
|
*/ |
529
|
|
|
public function getRecordRef(app_Record $record) |
530
|
|
|
{ |
531
|
|
|
$fullClassName = get_class($record); |
532
|
|
|
list(, $className) = explode('_', $fullClassName); |
533
|
|
|
return $className . ':' . $record->id; |
|
|
|
|
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Specifies the language to use for translation. |
539
|
|
|
* |
540
|
|
|
* If $language is null, the language is reset to |
541
|
|
|
* the current logged user language. |
542
|
|
|
* |
543
|
|
|
* @param string|null $language |
544
|
|
|
*/ |
545
|
|
|
public function setTranslateLanguage($language) |
546
|
|
|
{ |
547
|
|
|
$this->language = $language; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
|
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Translates the string. |
554
|
|
|
* |
555
|
|
|
* @param string $str |
556
|
|
|
* @return string |
557
|
|
|
*/ |
558
|
|
|
public function translate($str, $str_plurals = null, $number = null) |
559
|
|
|
{ |
560
|
|
|
require_once APP_PHP_PATH . 'functions.php'; |
561
|
|
|
$translation = $str; |
562
|
|
|
if ($translate = bab_functionality::get('Translate/Gettext')) { |
563
|
|
|
/* @var $translate Func_Translate_Gettext */ |
564
|
|
|
$translate->setAddonName('libapp'); |
565
|
|
|
$translation = $translate->translate($str, $str_plurals, $number); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
return $translation; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* @param string $str |
573
|
|
|
* @param string $str_plurals |
574
|
|
|
* @param int $number |
575
|
|
|
* @return string |
576
|
|
|
*/ |
577
|
|
|
public function translatable($str, $str_plurals = null, $number = null) |
578
|
|
|
{ |
579
|
|
|
return $str; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Translates all the string in an array and returns a new array. |
584
|
|
|
* |
585
|
|
|
* @param array $arr |
586
|
|
|
* @return array |
587
|
|
|
*/ |
588
|
|
|
public function translateArray($arr) |
589
|
|
|
{ |
590
|
|
|
$newarr = $arr; |
591
|
|
|
|
592
|
|
|
foreach ($newarr as &$str) { |
593
|
|
|
$str = $this->translate($str); |
594
|
|
|
} |
595
|
|
|
return $newarr; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
|
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Returns a link for writting an email to the specified email address. |
602
|
|
|
* |
603
|
|
|
* @param string $addr |
604
|
|
|
* @param string $subject |
605
|
|
|
* @param string $body |
606
|
|
|
* |
607
|
|
|
* @return string |
608
|
|
|
*/ |
609
|
|
|
public function mailTo($addr, $subject = null, $body = null) |
610
|
|
|
{ |
611
|
|
|
$mailTo = 'mailto:' . $addr; |
612
|
|
|
$parameters = array(); |
613
|
|
|
if (isset($subject)) { |
614
|
|
|
$parameters[] = 'subject=' . $subject; |
615
|
|
|
} |
616
|
|
|
if (isset($body)) { |
617
|
|
|
$parameters[] = 'body=' . $body; |
618
|
|
|
} |
619
|
|
|
if (!empty($parameters)) { |
620
|
|
|
$mailTo .= '?' . implode('&', $parameters); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
return $mailTo; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
|
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Format a number for display |
630
|
|
|
* |
631
|
|
|
* @param float|string|null $number Numeric value with decimal |
632
|
|
|
* @return string |
633
|
|
|
*/ |
634
|
|
|
public function numberFormat($number, $decimals = 2) |
635
|
|
|
{ |
636
|
|
|
if (is_null($number)) { |
637
|
|
|
return '#,##'; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
$number = number_format(floatval($number), $decimals, ',', ' '); |
641
|
|
|
return str_replace(' ', bab_nbsp(), $number); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Format a number with an optional unit. |
647
|
|
|
* |
648
|
|
|
* If the value is >= 1000 the value is shortened and the corresponding prexif (k or M) is used. |
649
|
|
|
* |
650
|
|
|
* @param float|string|null $number |
651
|
|
|
* @param string $unitSymbol (For example $, m2, Wh) |
652
|
|
|
* @param int $decimals |
653
|
|
|
* @return string|mixed |
654
|
|
|
*/ |
655
|
|
|
public function shortFormatWithUnit($number, $unitSymbol = '', $decimals = 2) |
656
|
|
|
{ |
657
|
|
|
if (is_null($number)) { |
658
|
|
|
return '#,##'; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
$prefix = ''; |
662
|
|
|
if ($number >= 1000000) { |
663
|
|
|
$number /= 1000000; |
664
|
|
|
$prefix = 'M'; |
665
|
|
|
} elseif ($number >= 1000) { |
666
|
|
|
$number /= 1000; |
667
|
|
|
$prefix = 'k'; |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
$number = number_format($number, $decimals, ',', ' '); |
|
|
|
|
671
|
|
|
return str_replace(' ', bab_nbsp(), $number . ' ' . $prefix . $unitSymbol); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Reformat a phone number in the specified format. |
677
|
|
|
* |
678
|
|
|
* @param string $phone The phone number to be formatted |
679
|
|
|
* @param int $format The format the phone number should be formatted into |
680
|
|
|
* |
681
|
|
|
* @return string The formatted phone number |
682
|
|
|
*/ |
683
|
|
|
public function phoneNumberFormat($phone, $format = null) |
684
|
|
|
{ |
685
|
|
|
$PhoneNumber = bab_Functionality::get('PhoneNumber'); |
686
|
|
|
if ($PhoneNumber === false) { |
|
|
|
|
687
|
|
|
return $phone; |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
if (!isset($format)) { |
691
|
|
|
$format = bab_registry::get('/' . $this->addonName . '/numberFormat', $PhoneNumber->getDefaultFormat()); |
|
|
|
|
692
|
|
|
} |
693
|
|
|
$phoneNumberUtil = $PhoneNumber->PhoneNumberUtil(); |
|
|
|
|
694
|
|
|
|
695
|
|
|
try { |
696
|
|
|
$phoneNumber = $phoneNumberUtil->parse($phone, 'FR'); |
697
|
|
|
$phone = $phoneNumberUtil->format($phoneNumber, $format); |
698
|
|
|
} catch (\libphonenumber\NumberParseException $e) { |
|
|
|
|
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
return $phone; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
|
705
|
|
|
|
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Includes Controller class definition. |
709
|
|
|
*/ |
710
|
|
|
public function includeController() |
711
|
|
|
{ |
712
|
|
|
require_once APP_PHP_PATH . '/controller.class.php'; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* Includes RecordController class definition. |
718
|
|
|
*/ |
719
|
|
|
public function includeRecordController() |
720
|
|
|
{ |
721
|
|
|
require_once APP_CTRL_PATH . '/record.ctrl.php'; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Instanciates the controller. |
727
|
|
|
* |
728
|
|
|
* @return app_Controller |
729
|
|
|
*/ |
730
|
|
|
public function Controller() |
731
|
|
|
{ |
732
|
|
|
$this->includeController(); |
733
|
|
|
return bab_getInstance($this->classPrefix.'Controller')->setApp($this); |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* Instanciates a controller class. |
739
|
|
|
* |
740
|
|
|
* @return bab_Controller |
741
|
|
|
*/ |
742
|
|
|
public function ControllerProxy($className, $proxy = true) |
743
|
|
|
{ |
744
|
|
|
$this->includeController(); |
745
|
|
|
|
746
|
|
|
if ($proxy) { |
747
|
|
|
return app_Controller::getProxyInstance($this, $className); |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
return new $className($this); |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
|
754
|
|
|
|
755
|
|
|
/** |
756
|
|
|
* Include class app_Ui |
757
|
|
|
* |
758
|
|
|
*/ |
759
|
|
|
public function includeUi() |
760
|
|
|
{ |
761
|
|
|
require_once APP_UI_PATH . 'ui.class.php'; |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
|
765
|
|
|
/** |
766
|
|
|
* The app_Ui object propose an access to all ui files and ui objects (widgets) |
767
|
|
|
* |
768
|
|
|
* @return app_Ui |
769
|
|
|
*/ |
770
|
|
|
public function Ui() |
771
|
|
|
{ |
772
|
|
|
$this->includeUi(); |
773
|
|
|
return bab_getInstance($this->classPrefix . 'Ui');//->setApp($this); |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
|
777
|
|
|
|
778
|
|
|
protected function includeAccess() |
779
|
|
|
{ |
780
|
|
|
require_once WORKSPACE_PHP_PATH . '/access.class.php'; |
|
|
|
|
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
|
784
|
|
|
|
785
|
|
|
|
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* Get upload path |
789
|
|
|
* if the method return null, no upload functionality |
790
|
|
|
* |
791
|
|
|
* @return bab_Path |
792
|
|
|
*/ |
793
|
|
|
public function getUploadPath() |
794
|
|
|
{ |
795
|
|
|
require_once $GLOBALS['babInstallPath'].'utilit/path.class.php'; |
796
|
|
|
return new bab_Path(bab_getAddonInfosInstance($this->getAddonName())->getUploadPath()); |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* |
802
|
|
|
* @param string $name |
803
|
|
|
* @param mixed $arguments |
804
|
|
|
* @return mixed |
805
|
|
|
*/ |
806
|
|
|
public function __call($name, $arguments) |
807
|
|
|
{ |
808
|
|
|
switch (true) { |
809
|
|
|
|
810
|
|
|
case substr($name, -strlen('SetClassName')) === 'SetClassName': |
811
|
|
|
$setName = substr($name, 0, strlen($name) - strlen('ClassName')); |
812
|
|
|
return $this->classPrefix . $setName; |
813
|
|
|
|
814
|
|
|
case substr($name, -strlen('ClassName')) === 'ClassName': |
815
|
|
|
$recordName = substr($name, 0, strlen($name) - strlen('ClassName')); |
816
|
|
|
return $this->classPrefix . $recordName; |
817
|
|
|
|
818
|
|
|
case substr($name, 0, strlen('include')) === 'include' && substr($name, -strlen('Set')) === 'Set': |
819
|
|
|
$fileNameBase = strtolower(substr(substr($name, 0, strlen($name) - strlen('Set')), strlen('include'))); |
820
|
|
|
require_once PHP_SET_PATH . $fileNameBase . '.class.php'; |
|
|
|
|
821
|
|
|
return; |
822
|
|
|
|
823
|
|
|
case substr($name, -strlen('Set')) === 'Set': |
824
|
|
|
$includeMethod = 'include' . $name; |
825
|
|
|
$this->$includeMethod(); |
826
|
|
|
$setClassNameMethod = $name . 'ClassName'; |
827
|
|
|
$className = $this->$setClassNameMethod(); |
828
|
|
|
$set = new $className($this); |
829
|
|
|
return $set; |
830
|
|
|
|
831
|
|
|
//case method_exists($this, $name . 'Set'): |
832
|
|
|
default: |
833
|
|
|
$setName = $name . 'Set'; |
834
|
|
|
$recordClassNameMethod = $name . 'ClassName'; |
835
|
|
|
$recordClassName = $this->$recordClassNameMethod(); |
836
|
|
|
if (isset($arguments[0])) { |
837
|
|
|
if ($arguments[0] instanceof $recordClassName) { |
838
|
|
|
return $arguments[0]; |
839
|
|
|
} |
840
|
|
|
$set = $this->$setName(); |
841
|
|
|
return $set->get($arguments[0]); |
842
|
|
|
} |
843
|
|
|
return null; |
844
|
|
|
} |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
|
848
|
|
|
/** |
849
|
|
|
* Test if this App implementation handles the specified object class. |
850
|
|
|
* |
851
|
|
|
* The default is to consider that an object Xxxx implemented if the includeXxxxSet() method is |
852
|
|
|
* declared public on this App. |
853
|
|
|
* |
854
|
|
|
* @param string $objectClassName App object name (eg. 'Contact' or 'CatalogItem') |
855
|
|
|
* @return bool |
856
|
|
|
*/ |
857
|
|
|
public function __isset($objectClassName) |
858
|
|
|
{ |
859
|
|
|
if (null === $this->implementedObjects) { |
860
|
|
|
|
861
|
|
|
$this->implementedObjects = array(); |
|
|
|
|
862
|
|
|
|
863
|
|
|
$className = get_class($this); |
864
|
|
|
$rClass = new ReflectionClass($className); |
865
|
|
|
|
866
|
|
|
foreach ($rClass->getMethods(ReflectionMethod::IS_PUBLIC) as $m) { |
867
|
|
|
|
868
|
|
|
// We consider object Xxxx implemented if the includeXxxxSet() method is |
869
|
|
|
// declared public on this. |
870
|
|
|
|
871
|
|
|
if ($m->getDeclaringClass()->name !== $className) { |
872
|
|
|
// The method is declared on an ancestor class. |
873
|
|
|
continue; |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
if (substr($m->name, 0, 7) === 'include' && substr($m->name, -3) === 'Set') { |
877
|
|
|
$this->implementedObjects[substr($m->name, 7, -3)] = 1; |
878
|
|
|
} |
879
|
|
|
} |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
return isset($this->implementedObjects[$objectClassName]); |
883
|
|
|
} |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
|