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