MBModule::getDBName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/*********************************************************************************
3
 * SugarCRM Community Edition is a customer relationship management program developed by
4
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5
6
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
7
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
8
 *
9
 * This program is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU Affero General Public License version 3 as published by the
11
 * Free Software Foundation with the addition of the following permission added
12
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15
 *
16
 * This program is distributed in the hope that it will be useful, but WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License along with
22
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
23
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
 * 02110-1301 USA.
25
 *
26
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28
 *
29
 * The interactive user interfaces in modified source and object code versions
30
 * of this program must display Appropriate Legal Notices, as required under
31
 * Section 5 of the GNU Affero General Public License version 3.
32
 *
33
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34
 * these Appropriate Legal Notices must retain the display of the "Powered by
35
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
37
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38
 ********************************************************************************/
39
40
define ( 'MB_TEMPLATES', 'include/SugarObjects/templates' ) ;
41
define ( 'MB_IMPLEMENTS', 'include/SugarObjects/implements' ) ;
42
require_once ('modules/ModuleBuilder/MB/MBVardefs.php') ;
43
require_once ('modules/ModuleBuilder/MB/MBRelationship.php') ;
44
require_once ('modules/ModuleBuilder/MB/MBLanguage.php') ;
45
46
class MBModule
47
{
48
    public $name = '' ;
49
    public $config = array (
50
    'assignable' => 1 , 'security_groups' => 1, 'acl' => 1 , 'has_tab' => 1 , 'studio' => 1 , 'audit' => 1 ) ;
51
    public $mbpublicdefs ;
52
    public $errors = array ( ) ;
53
    public $path = '' ;
54
    public $implementable = array (
55
    'has_tab' => 'Navigation Tab' ) ;
56
    public $always_implement = array ( 'assignable' => 'Assignable', 'security_groups' => 'Security Groups' , 'acl' => 'Access Controls' , 'studio' => 'Studio Support' , 'audit' => 'Audit Table' ) ;
57
    public $iTemplate = array (
58
    'assignable', 'security_groups' ) ;
59
60
    public $config_md5 = null ;
61
62
    function __construct ($name , $path , $package , $package_key)
63
    {
64
        global $mod_strings;
65
    	$this->config [ 'templates' ] = array ( 'basic' => 1 ) ;
66
67
        $this->name = MBModule::getDBName ( $name ) ;
68
        $this->key_name = $package_key . '_' . $name ;
69
        $this->package = $package ;
70
        $this->package_key = $package_key ;
71
        $this->package_path = $path ;
72
73
        $this->implementable = array (
74
        'has_tab' => !empty($mod_strings[ 'LBL_NAV_TAB' ]) ? $mod_strings[ 'LBL_NAV_TAB' ] : false) ;
75
        $this->path = $this->getModuleDir () ;
76
        //		$this->mbrelationship = new MBRelationship($this->name, $this->path, $this->key_name);
77
        $this->relationships = new UndeployedRelationships ( $this->path ) ;
78
        $this->mbvardefs = new MBVardefs ( $this->name, $this->path, $this->key_name ) ;
79
80
        $this->load () ;
81
    }
82
83
    function getDBName ($name)
84
    {
85
        return preg_replace ( "/[^\w]+/", "_", $name ) ;
86
    }
87
88
    function getModuleName()
89
    {
90
        return $this->name;
91
    }
92
93
    function getPackageName()
94
    {
95
        return $this->package;
96
    }
97
98
    /**
99
     * @return UndeployedRelationships
100
     */
101
    function getRelationships()
102
    {
103
        return $this->relationships;
104
    }
105
106
    /**
107
     * Loads the module based on the module name
108
     *
109
     */
110
    function load ()
111
    {
112
        if (file_exists ( $this->path . '/config.php' ))
113
        {
114
            include ($this->path . '/config.php') ;
115
            $this->config = $config ;
116
        }
117
        $label = (! empty ( $this->config [ 'label' ] )) ? $this->config [ 'label' ] : $this->name ;
118
        $this->mblanguage = new MBLanguage ( $this->name, $this->path, $label, $this->key_name ) ;
119
        foreach ( $this->iTemplate as $temp )
120
        {
121
            if (! empty ( $this->config [ $temp ] ))
122
            {
123
                $this->mbvardefs->iTemplates [ $temp ] = 1 ;
124
                $this->mblanguage->iTemplates [ $temp ] = $temp ;
125
            }
126
        }
127
        $this->mbvardefs->templates = $this->config [ 'templates' ] ;
128
        $this->mblanguage->templates = $this->config [ 'templates' ] ;
129
        $this->mbvardefs->load () ;
130
        $this->mblanguage->load () ;
131
132
    }
133
134
    function addTemplate ($template)
135
    {
136
        $this->config [ 'templates' ] [ $template ] = 1 ;
137
    }
138
139
    function getModuleDir ()
140
    {
141
        return $this->package_path . '/modules/' . $this->name ;
142
    }
143
144
    function removeTemplate ($template)
145
    {
146
        unset ( $this->config [ 'templates' ] [ $template ] ) ;
147
    }
148
149
    function getVardefs ($by_group = false)
150
    {
151
        $this->mbvardefs->updateVardefs ( $by_group ) ;
152
        return $this->mbvardefs->getVardefs () ;
153
    }
154
155
    function addField ($vardef)
156
    {
157
        $this->mbvardefs->addFieldVardef ( $vardef ) ;
158
    }
159
160
    function addFieldObject ($field)
161
    {
162
        $vardef = $field->get_field_def () ;
163
		$this->mbvardefs->mergeVardefs();
164
		$existingVardefs = $this->mbvardefs->getVardefs () ;
165
		//Merge with the existing vardef if it already exists
166
		if(!empty($existingVardefs['fields'][$vardef['name']])){
167
			$vardef = array_merge( $existingVardefs['fields'][$vardef['name']], $vardef);
168
		}
169
        if (! empty ( $vardef [ 'source' ] ) && $vardef [ 'source' ] == 'custom_fields')
170
            unset ( $vardef [ 'source' ] ) ;
171
172
	    $this->mbvardefs->load();
173
        $this->addField ( $vardef ) ;
174
        $this->mbvardefs->save();
175
    }
176
177
    function deleteField ($name)
178
    {
179
        $this->mbvardefs->deleteField ( $name ) ;
180
    }
181
182
    function fieldExists ($name = '' , $type = '')
183
    {
184
        $vardefs = $this->getVardefs();
185
        if (! empty ( $vardefs ))
186
        {
187
            if (empty ( $type ) && empty ( $name ))
188
                return false ; else if (empty ( $type ))
189
                return ! empty ( $vardefs [ 'fields' ] [ $name ] ) ; else if (empty ( $name ))
190
            {
191
                foreach ( $vardefs [ 'fields' ] as $def )
192
                {
193
                    if ($def [ 'type' ] == $type)
194
                        return true ;
195
                }
196
                return false ;
197
            } else
198
                return (! empty ( $vardefs [ 'fields' ] [ $name ] ) && ($vardefs [ 'fields' ] [ $name ] [ 'type' ] == $type)) ;
199
        } else
200
        {
201
            return false ;
202
        }
203
    }
204
205
    function getModStrings ($language = 'en_us')
206
    {
207
        return $this->mblanguage->getModStrings ( $language ) ;
208
    }
209
210
    function setModStrings ($language = 'en_us' , $mod_strings)
211
    {
212
        $language .= '.lang.php' ;
213
        $this->mblanguage->strings [ $language ] = $mod_strings ;
214
    }
215
216
	function setLabel ($language = 'en_us' , $key , $value)
217
    {
218
    	$language .= '.lang.php' ;
219
        $this->mblanguage->strings [ $language ] [ $key ] = $value ;
220
        //Ensure this key exists in all languages
221
        foreach ($this->mblanguage->strings as $lang => $values) {
222
        	if (empty($values[$key])) {
223
        		$this->mblanguage->strings[$lang][$key] = $value;
224
        	}
225
        }
226
    }
227
228
    function deleteLabel ($language = 'en_us' , $key)
229
    {
230
   		foreach ($this->mblanguage->strings as $lang => $values) {
231
        	if (!empty($values[$key])) {
232
        		unset($this->mblanguage->strings[$lang][$key]);
233
        	}
234
        }
235
    }
236
237
    /**
238
     * Required for an MB module to work with Dynamic fields
239
     */
240
	function addLabel ( $displayLabel)
241
    {
242
        $this->setLabel('en_us', $this->getDBName($displayLabel, false), translate($displayLabel));
0 ignored issues
show
Unused Code introduced by
The call to MBModule::getDBName() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
243
        $this->save();
244
    }
245
246
    function getLabel ($language = 'en_us' , $key)
247
    {
248
        $language .= '.lang.php' ;
249
        if (empty ( $this->mblanguage->strings [ $language ] [ $key ] ))
250
        {
251
252
            return '' ;
253
        }
254
        return $this->mblanguage->strings [ $language ] [ $key ] ;
255
256
    }
257
258
    function getAppListStrings ($language = 'en_us')
259
    {
260
        return $this->mblanguage->getAppListStrings ( $language ) ;
261
    }
262
263
    function setAppListStrings ($language = 'en_us' , $app_list_strings)
264
    {
265
        $language .= '.lang.php' ;
266
        $this->mblanguage->appListStrings [ $language ] = $app_list_strings ;
267
    }
268
269
    function setDropDown ($language = 'en_us' , $key , $value)
270
    {
271
        $language .= '.lang.php' ;
272
        $this->mblanguage->appListStrings [ $language ] [ $key ] = $value ;
273
    }
274
275
    function deleteDropDown ($language = 'en_us' , $key)
276
    {
277
        $language .= '.lang.php' ;
278
        unset ( $this->mblanguage->appListStrings [ $language ] [ $key ] ) ;
279
    }
280
281
    function save ()
282
    {
283
        $this->path = $this->getModuleDir () ;
284
        if (mkdir_recursive ( $this->path ))
285
        {
286
287
            $this->setConfigMD5 () ;
288
            $old_config_md5 = $this->config_md5 ;
289
            $this->saveConfig () ;
290
            $this->getVardefs () ;
291
            $this->mbvardefs->save ( $this->key_name ) ;
292
            //           $this->mbrelationship->save ( $this->key_name ) ;
293
            $this->relationships->save () ;
294
            $this->copyMetaData () ;
295
            $this->copyDashlet () ;
296
            $this->copyViews() ;
297
            if (0 != strcmp ( $old_config_md5, $this->config_md5 ))
298
            {
299
                $this->mblanguage->reload () ;
300
            }
301
            $this->mblanguage->label = $this->config [ 'label' ] ;
302
            //pass in the key_name incase it has changed mblanguage will check if it is different and handle it accordingly
303
            $this->mblanguage->save ( $this->key_name ) ;
304
305
            if (! file_exists ( $this->package_path . "/icons/icon_" . ucfirst ( $this->key_name ) . ".gif" ))
306
            {
307
                $this->createIcon () ;
308
            }
309
            $this->errors = array_merge ( $this->errors, $this->mbvardefs->errors ) ;
310
311
        }
312
    }
313
314
    function copyDashlet() {
315
    	$templates = array_reverse ( $this->config [ 'templates' ], true ) ;
316
        foreach ( $templates as $template => $a )
317
        {
318
            if (file_exists ( MB_TEMPLATES . '/' . $template . '/Dashlets/Dashlet' ))
319
            {
320
321
            	$this->copyMetaRecursive ( MB_TEMPLATES . '/' . $template . '/Dashlets/Dashlet', $this->path . '/Dashlets/' . $this->key_name . 'Dashlet/' ) ;
322
323
            }
324
        }
325
    }
326
327
    function copyViews() {
328
        $templates = array_reverse ( $this->config [ 'templates' ], true ) ;
329
        foreach ( $templates as $template => $a )
330
        {
331
            if (file_exists ( MB_TEMPLATES . '/' . $template . '/views' ))
332
            {
333
                $this->copyMetaRecursive ( MB_TEMPLATES . '/' . $template . '/views', $this->path . '/views/' ) ;
334
            }
335
        }
336
    }
337
338
    function copyCustomFiles ( $from , $to )
339
    {
340
    	$d = dir ( $from ) ;
341
        while ( $filename = $d->read () )
342
        {
343
        	if (substr ( $filename, 0, 1 ) == '.')
344
            	continue ;
345
           	if ( $filename != 'metadata' && $filename != 'Dashlets' && $filename != 'relationships' && $filename != 'language' && $filename != 'config.php' && $filename != 'relationships.php' && $filename != 'vardefs.php' )
346
           		copy_recursive ( "$from/$filename" , "$to/$filename" ) ;
347
        }
348
    }
349
350
    function copyMetaData ()
351
    {
352
        $templates = array_reverse ( $this->config [ 'templates' ], true ) ;
353
        foreach ( $templates as $template => $a )
354
        {
355
            if (file_exists ( MB_TEMPLATES . '/' . $template . '/metadata' ))
356
            {
357
                $this->copyMetaRecursive ( MB_TEMPLATES . '/' . $template . '/metadata', $this->path . '/metadata/' ) ;
358
359
            }
360
        }
361
    }
362
363
    function copyMetaRecursive ($from , $to , $overwrite = false)
364
    {
365
        if (! file_exists ( $from ))
366
            return ;
367
        if (is_dir ( $from ))
368
        {
369
            $findArray = array ( '<module_name>' , '<_module_name>' , '<MODULE_NAME>' , '<object_name>' , '<_object_name>' , '<OBJECT_NAME>' );
370
            $replaceArray = array ( $this->key_name , strtolower ( $this->key_name ) , strtoupper ( $this->key_name ) ,
371
            						$this->key_name , strtolower ( $this->key_name ) , strtoupper ( $this->key_name ) );
372
        	mkdir_recursive ( $to ) ;
373
            $d = dir ( $from ) ;
374
            while ( $e = $d->read () )
375
            {
376
                if (substr ( $e, 0, 1 ) == '.')
377
                    continue ;
378
                $nfrom = $from . '/' . $e ;
379
                $nto = $to . '/' . str_replace ( 'm-n-', $this->key_name, $e ) ;
380
                if (is_dir ( $nfrom ))
381
                {
382
                    $this->copyMetaRecursive ( $nfrom, $nto, $overwrite ) ;
383
                } else
384
                {
385
                    if ($overwrite || ! file_exists ( $nto ))
386
                    {
387
                        $contents = file_get_contents ( $nfrom ) ;
388
                        $contents = str_replace ( $findArray, $replaceArray, $contents ) ;
389
                        $fw = sugar_fopen ( $nto, 'w' ) ;
390
                        fwrite ( $fw, $contents ) ;
391
                        fclose ( $fw ) ;
392
                    }
393
                }
394
            }
395
396
        }
397
    }
398
399
    function saveConfig ()
400
    {
401
        $header = file_get_contents ( 'modules/ModuleBuilder/MB/header.php' ) ;
402
        if (! write_array_to_file ( 'config', $this->config, $this->path . '/config.php', 'w', $header ))
403
        {
404
            $this->errors [] = 'Could not save config to ' . $this->path . '/config.php' ;
405
        }
406
        $this->setConfigMD5 () ;
407
    }
408
409
    function setConfigMD5 ()
410
    {
411
        if (file_exists ( $this->path . '/config.php' ))
412
            $this->config_md5 = md5 ( base64_encode ( serialize ( $this->config ) ) ) ;
413
    }
414
415
    function build ($basepath)
416
    {
417
        global $app_list_strings;
418
        $path = $basepath . '/modules/' . $this->key_name ;
419
        if (mkdir_recursive ( $path ))
420
        {
421
            $this->createClasses ( $path ) ;
422
            if( $this->config['importable'] || in_array ( 'person', array_keys($this->config[ 'templates' ]) ) )
423
                $this->createMenu ( $path ) ;
424
            $this->copyCustomFiles ( $this->path , $path ) ;
425
            $this->copyMetaRecursive ( $this->path . '/metadata/', $path . '/metadata/', true ) ;
426
            $this->copyMetaRecursive ( $this->path . '/Dashlets/' . $this->key_name . 'Dashlet/',
427
            						   $path . '/Dashlets/' . $this->key_name . 'Dashlet/', true ) ;
428
            $app_list_strings['moduleList'][$this->key_name] = $this->mblanguage->label;
429
            $this->relationships->build ( $basepath ) ;
430
            $this->mblanguage->build ( $path ) ;
431
        }
432
    }
433
434
    function createClasses ($path)
435
    {
436
        $class = array ( ) ;
437
        $class [ 'name' ] = $this->key_name ;
438
        $class [ 'table_name' ] = strtolower ( $class [ 'name' ] ) ;
439
        $class [ 'extends' ] = 'Basic' ;
440
        $class [ 'requires' ] [] = MB_TEMPLATES . '/basic/Basic.php' ;
441
        $class [ 'requires' ] = array ( ) ;
442
        $class [ 'audited' ] = (! empty ( $this->config [ 'audit' ] )) ? 'true' : 'false' ;
443
        $class [ 'acl' ] = ! empty ( $this->config [ 'acl' ] ) ;
444
        $class [ 'templates' ] = "'basic'" ;
445
        foreach ( $this->iTemplate as $template )
446
        {
447
            if (! empty ( $this->config [ $template ] ))
448
            {
449
                $class [ 'templates' ] .= ",'$template'" ;
450
            }
451
        }
452
        foreach ( $this->config [ 'templates' ] as $template => $a )
453
        {
454
            if ($template == 'basic')
455
                continue ;
456
            $class [ 'templates' ] .= ",'$template'" ;
457
            $class [ 'extends' ] = ucFirst ( $template ) ;
458
            $class [ 'requires' ] [] = MB_TEMPLATES . '/' . $template . '/' . ucfirst ( $template ) . '.php' ;
459
        }
460
        $class [ 'importable' ] = $this->config [ 'importable' ] ;
461
        $class [ 'inline_edit' ] = $this->config [ 'inline_edit' ] ;
462
        $this->mbvardefs->updateVardefs () ;
463
        $class [ 'fields' ] = $this->mbvardefs->vardefs [ 'fields' ] ;
464
        $class [ 'fields_string' ] = var_export_helper ( $this->mbvardefs->vardef [ 'fields' ] ) ;
465
        $relationship = array ( ) ;
466
        $class [ 'relationships' ] = var_export_helper ( $this->mbvardefs->vardef [ 'relationships' ] ) ;
467
        $smarty = new Sugar_Smarty ( ) ;
468
        $smarty->left_delimiter = '{{' ;
469
        $smarty->right_delimiter = '}}' ;
470
        $smarty->assign ( 'class', $class ) ;
471
        //write sugar generated class
472
        $fp = sugar_fopen ( $path . '/' . $class [ 'name' ] . '_sugar.php', 'w' ) ;
473
        fwrite ( $fp, $smarty->fetch ( 'modules/ModuleBuilder/tpls/MBModule/Class.tpl' ) ) ;
474
        fclose ( $fp ) ;
475
        //write vardefs
476
        $fp = sugar_fopen ( $path . '/vardefs.php', 'w' ) ;
477
        fwrite ( $fp, $smarty->fetch ( 'modules/ModuleBuilder/tpls/MBModule/vardef.tpl' ) ) ;
478
        fclose ( $fp ) ;
479
480
        if (! file_exists ( $path . '/' . $class [ 'name' ] . '.php' ))
481
        {
482
            $fp = sugar_fopen ( $path . '/' . $class [ 'name' ] . '.php', 'w' ) ;
483
            fwrite ( $fp, $smarty->fetch ( 'modules/ModuleBuilder/tpls/MBModule/DeveloperClass.tpl' ) ) ;
484
            fclose ( $fp ) ;
485
        }
486
        if (! file_exists ( $path . '/metadata' ))
487
            mkdir_recursive ( $path . '/metadata' ) ;
488
        if (! empty ( $this->config [ 'studio' ] ))
489
        {
490
            $fp = sugar_fopen ( $path . '/metadata/studio.php', 'w' ) ;
491
            fwrite ( $fp, $smarty->fetch ( 'modules/ModuleBuilder/tpls/MBModule/Studio.tpl' ) ) ;
492
            fclose ( $fp ) ;
493
        } else
494
        {
495
            if (file_exists ( $path . '/metadata/studio.php' ))
496
                unlink ( $path . '/metadata/studio.php' ) ;
497
        }
498
    }
499
500
    function createMenu ($path)
501
    {
502
        $smarty = new Sugar_Smarty ( ) ;
503
        $smarty->assign ( 'moduleName', $this->key_name ) ;
504
        $smarty->assign ( 'showvCard', in_array ( 'person', array_keys($this->config[ 'templates' ]) ) ) ;
505
        $smarty->assign ( 'showimport', $this->config['importable'] );
506
        //write sugar generated class
507
        $fp = sugar_fopen ( $path . '/' . 'Menu.php', 'w' ) ;
508
        fwrite ( $fp, $smarty->fetch ( 'modules/ModuleBuilder/tpls/MBModule/Menu.tpl' ) ) ;
509
        fclose ( $fp ) ;
510
    }
511
512
    function addInstallDefs (&$installDefs)
513
    {
514
        $name = $this->key_name ;
515
        $installDefs [ 'copy' ] [] = array ( 'from' => '<basepath>/SugarModules/modules/' . $name , 'to' => 'modules/' . $name ) ;
516
        $installDefs [ 'beans' ] [] = array ( 'module' => $name , 'class' => $name , 'path' => 'modules/' . $name . '/' . $name . '.php' , 'tab' => $this->config [ 'has_tab' ] ) ;
517
        $this->relationships->addInstallDefs ( $installDefs ) ;
518
    }
519
520
    function getNodes ()
521
    {
522
523
        $lSubs = array ( ) ;
524
        $psubs = $this->getProvidedSubpanels () ;
525
        foreach ( $psubs as $sub )
526
        {
527
            $subLabel = $sub ;
528
            if ($subLabel == 'default')
529
            {
530
                $subLabel = $GLOBALS [ 'mod_strings' ] [ 'LBL_DEFAULT' ] ;
531
            }
532
            $lSubs [] = array ( 'name' => $subLabel , 'type' => 'list' , 'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view=ListView&view_module=' . $this->name . '&view_package=' . $this->package . '&subpanel=' . $sub . '&subpanelLabel=' . $subLabel . '&local=1' ) ;
533
        }
534
535
        $searchSubs = array ( ) ;
536
        $searchSubs [] = array ( 'name' => translate('LBL_BASIC_SEARCH') , 'type' => 'list' , 'action' => "module=ModuleBuilder&MB=true&action=editLayout&view=basic_search&view_module={$this->name}&view_package={$this->package}"  ) ;
537
        $searchSubs [] = array ( 'name' => translate('LBL_ADVANCED_SEARCH') , 'type' => 'list' , 'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view=advanced_search&view_module=' . $this->name . '&view_package=' . $this->package  ) ;
538
        $dashlets = array( );
539
        $dashlets [] = array('name' => translate('LBL_DASHLETLISTVIEW') , 'type' => 'dashlet' , 'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view=dashlet&view_module=' . $this->name . '&view_package=' . $this->package );
540
		$dashlets [] = array('name' => translate('LBL_DASHLETSEARCHVIEW') , 'type' => 'dashletsearch' , 'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view=dashletsearch&view_module=' . $this->name . '&view_package=' . $this->package );
541
542
		$popups = array( );
543
        $popups [] = array('name' => translate('LBL_POPUPLISTVIEW') , 'type' => 'popuplistview' , 'action' => 'module=ModuleBuilder&action=editLayout&view=popuplist&view_module=' . $this->name . '&view_package=' . $this->package );
544
		$popups [] = array('name' => translate('LBL_POPUPSEARCH') , 'type' => 'popupsearch' , 'action' => 'module=ModuleBuilder&action=editLayout&view=popupsearch&view_module=' . $this->name . '&view_package=' . $this->package );
545
		
546
        $layouts = array (
547
        	array ( 'name' => translate('LBL_EDITVIEW') , 'type' => 'edit' , 'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view='.MB_EDITVIEW.'&view_module=' . $this->name . '&view_package=' . $this->package ) ,
548
        	array ( 'name' => translate('LBL_DETAILVIEW') , 'type' => 'detail' , 'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view='.MB_DETAILVIEW.'&view_module=' . $this->name . '&view_package=' . $this->package ) ,
549
        	array ( 'name' => translate('LBL_LISTVIEW') , 'type' => 'list' , 'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view='.MB_LISTVIEW.'&view_module=' . $this->name . '&view_package=' . $this->package ) ,
550
        	array ( 'name' => translate('LBL_QUICKCREATE') , 'type' => MB_QUICKCREATE,  'action' => 'module=ModuleBuilder&MB=true&action=editLayout&view='.MB_QUICKCREATE.'&view_module=' . $this->name . '&view_package=' . $this->package ) ,
551
        	array ( 'name' => translate('LBL_DASHLET') , 'type' => 'Folder', 'children' => $dashlets, 'action' => 'module=ModuleBuilder&MB=true&action=wizard&view=dashlet&view_module=' . $this->name . '&view_package=' . $this->package  ),
552
        	array ( 'name' => translate('LBL_POPUP') , 'type' => 'Folder', 'children' => $popups, 'action' => 'module=ModuleBuilder&MB=true&action=wizard&view=popup&view_module=' . $this->name . '&view_package=' . $this->package  ),
553
        	array ( 'name' => translate('LBL_SEARCH_FORMS') , 'action' => 'module=ModuleBuilder&MB=true&action=wizard&view=search&view_module=' . $this->name . '&view_package=' . $this->package , 'type' => 'folder' , 'children' => $searchSubs )
554
        	) ;
555
556
        $children = array (
557
        	array ( 'name' => translate('LBL_FIELDS') , 'action' => 'module=ModuleBuilder&action=modulefields&view_module=' . $this->name . '&view_package=' . $this->package ) ,
558
        	array ( 'name' => translate('LBL_LABELS') , 'action' => 'module=ModuleBuilder&action=modulelabels&view_module=' . $this->name . '&view_package=' . $this->package ) ,
559
        	array ( 'name' => translate('LBL_RELATIONSHIPS') , 'action' => 'module=ModuleBuilder&action=relationships&view_module=' . $this->name . '&view_package=' . $this->package ) ,
560
        	array ( 'name' => translate('LBL_LAYOUTS') , 'type' => 'Folder' , 'action' => "module=ModuleBuilder&action=wizard&view_module={$this->name}&view_package={$this->package}&MB=1" , 'children' => $layouts ) ,
561
        	) ;
562
563
        if (count ( $lSubs ) > 0)
564
        {
565
            $children [] = array ( 'name' => translate('LBL_AVAILABLE_SUBPANELS') , 'type' => 'folder' , 'children' => $lSubs ) ;
566
        }
567
568
        $nodes = array ( 'name' => $this->name , 'children' => $children , 'action' => 'module=ModuleBuilder&action=module&view_module=' . $this->name . '&view_package=' . $this->package ) ;
569
570
        return $nodes ;
571
    }
572
573
574
    function getProvidedSubpanels ()
575
    {
576
        $this->providedSubpanels = array () ;
577
578
        $subpanelDir = $this->getModuleDir () . '/metadata/subpanels/' ;
579
        if (file_exists ( $subpanelDir ))
580
        {
581
            $f = dir ( $subpanelDir ) ;
582
            require_once 'modules/ModuleBuilder/parsers/relationships/AbstractRelationships.php' ;
583
584
            while ( $g = $f->read () )
585
            {
586
                // sanity check to confirm that this is a usable subpanel...
587
                if (substr ( $g, 0, 1 ) != '.' && AbstractRelationships::validSubpanel ( $subpanelDir . $g ))
588
                {
589
                    $subname = str_replace ( '.php', '', $g ) ;
590
                    $this->providedSubpanels [ $subname ] = $subname ;
591
                }
592
            }
593
        }
594
595
        return $this->providedSubpanels;
596
    }
597
598
    function getTypes ()
599
    {
600
        $types = array ( ) ;
601
        $d = dir ( MB_TEMPLATES ) ;
602
        while ( $e = $d->read () )
603
        {
604
            if (substr ( $e, 0, 1 ) != '.')
605
            {
606
                $types [ $e ] = $e ;
607
            }
608
        }
609
610
        return $types ;
611
    }
612
613
    function rename ($new_name)
614
    {
615
        $old = $this->getModuleDir () ;
616
        $old_name = $this->key_name;
617
        $this->name = $new_name ;
618
        $this->key_name = $this->package_key . '_' . $this->name ;
619
        $new = $this->getModuleDir () ;
620
        if (file_exists ( $new ))
621
        {
622
            return false ;
623
        }
624
        $renamed = rename ( $old, $new ) ;
625
        if ($renamed)
626
        {
627
            $this->renameMetaData ( $new , $old_name) ;
628
            $this->renameLanguageFiles ( $new ) ;
629
630
        }
631
        return $renamed ;
632
    }
633
634
	function renameLanguageFiles ($new_dir , $duplicate = false)
635
    {
636
637
        $this->mblanguage->name = $this->name ;
638
        $this->mblanguage->path = $new_dir ;
639
        $this->mblanguage->generateAppStrings () ;
640
        $this->mblanguage->save ( $this->key_name, $duplicate, true) ;
641
    }
642
643
    /**
644
     * Rename module name in metadata
645
     * @param string $new_dir
646
     * @param string $old_name
647
     */
648
    public function renameMetaData ($new_dir, $old_name)
649
    {
650
        $GLOBALS [ 'log' ]->debug ( 'MBModule.php->renameMetaData: new_dir=' . $new_dir ) ;
651
        if (! file_exists ( $new_dir ))
652
            return ;
653
        $dir = dir ( $new_dir ) ;
654
        while ( $e = $dir->read () )
655
        {
656
            if (substr ( $e, 0, 1 ) != '.')
657
            {
658
                if (is_dir ( $new_dir . '/' . $e ))
659
                {
660
                    $this->renameMetaData ( $new_dir . '/' . $e,  $old_name) ;
661
                }
662
                if (is_file ( $new_dir . '/' . $e ))
663
                {
664
                    $contents = file_get_contents ( $new_dir . '/' . $e ) ;
665
                    $search_array = array(
666
                        '/(\$module_name[ ]*=[ ]*\').*(\'[ ]*;)/',
667
                        '/(\$_module_name[ ]*=[ ]*\').*(\'[ ]*;)/',
668
                        '/(\$MODULE_NAME[ ]*=[ ]*\').*(\'[ ]*;)/',
669
                        '/(\$object_name[ ]*=[ ]*\').*(\'[ ]*;)/',
670
                        '/(\$_object_name[ ]*=[ ]*\').*(\'[ ]*;)/',
671
                        '/(\$OBJECT_NAME[ ]*=[ ]*\').*(\'[ ]*;)/'
672
                    );
673
                    $replace_array = array(
674
                        '$1' . $this->key_name . '$2',
675
                        '$1' . strtolower ( $this->key_name ) . '$2',
676
                        '$1' . strtoupper ( $this->key_name ) . '$2',
677
                        '$1' . $this->key_name . '$2',
678
                        '$1' . strtolower ( $this->key_name ) . '$2',
679
                        '$1' . strtoupper ( $this->key_name ) . '$2',
680
                    );
681
                    $contents = preg_replace($search_array, $replace_array, $contents);
682
                    $search_array = array(
683
                        "{$old_name}_",
684
                        "{$old_name}Dashlet"
685
                    );
686
                    $replace_array = array(
687
                        $this->key_name . '_',
688
                        $this->key_name . 'Dashlet'
689
                    );
690
                    $contents = str_replace($search_array, $replace_array, $contents );
691
                    
692
                    
693
                    if ("relationships.php" == $e) 
694
                    {
695
                        //bug 39598 Relationship Name Is Not Updated If Module Name Is Changed In Module Builder
696
                        $contents = str_replace  ( "'{$old_name}'", "'{$this->key_name}'" , $contents ) ;
697
                    }
698
                    
699
                    $fp = sugar_fopen ( $new_dir . '/' . $e, 'w' ) ;
700
                    fwrite ( $fp, $contents ) ;
701
                    fclose ( $fp ) ;
702
                }
703
            }
704
        }
705
    }
706
707
    function copy ($new_name)
708
    {
709
        $old = $this->getModuleDir () ;
710
711
        $count = 0 ;
712
        $old_name = $this->key_name;
713
        $this->name = $new_name ;
714
        $this->key_name = $this->package_key . '_' . $this->name ;
715
        $new = $this->getModuleDir () ;
716
        while ( file_exists ( $new ) )
717
        {
718
            $count ++ ;
719
            $this->name = $new_name . $count ;
720
            $this->key_name = $this->package_key . '_' . $this->name ;
721
            $new = $this->getModuleDir () ;
722
        }
723
724
        $new = $this->getModuleDir () ;
725
        $copied = copy_recursive ( $old, $new ) ;
726
727
        if ($copied)
728
        {
729
            $this->renameMetaData ( $new , $old_name) ;
730
            $this->renameLanguageFiles ( $new, true ) ;
731
        }
732
        return $copied ;
733
734
    }
735
736
    function delete ()
737
    {
738
        return rmdir_recursive ( $this->getModuleDir () ) ;
739
    }
740
741
    function populateFromPost ()
742
    {
743
        foreach ( $this->implementable as $key => $value )
744
        {
745
            $this->config [ $key ] = ! empty ( $_REQUEST [ $key ] ) ;
746
        }
747
        foreach ( $this->always_implement as $key => $value )
748
        {
749
            $this->config [ $key ] = true ;
750
        }
751
        if (! empty ( $_REQUEST [ 'type' ] ))
752
        {
753
            $this->addTemplate ( $_REQUEST [ 'type' ] ) ;
754
        }
755
756
        if (! empty ( $_REQUEST [ 'label' ] ))
757
        {
758
            $this->config [ 'label' ] = $_REQUEST [ 'label' ] ;
759
        }
760
761
        $this->config [ 'importable' ] = ! empty( $_REQUEST[ 'importable' ] ) ;
762
763
    }
764
765
    function getAvailibleSubpanelDef ($panelName)
766
    {
767
        $filepath = $this->getModuleDir () . "/metadata/subpanels/{$panelName}.php" ;
768
        if (file_exists ( $filepath ))
769
        {
770
            include ($filepath) ;
771
            return $subpanel_layout ;
772
        }
773
        return array ( ) ;
774
775
    }
776
777
    function saveAvailibleSubpanelDef ($panelName , $layout)
778
    {
779
        $dir = $this->getModuleDir () . "/metadata/subpanels" ;
780
        $filepath = "$dir/{$panelName}.php" ;
781
        if (mkdir_recursive ( $dir ))
782
        {
783
            // preserve any $module_name entry if one exists
784
            if (file_exists ( $filepath ))
785
            {
786
                include ($filepath) ;
787
            }
788
            $module_name = (isset ( $module_name )) ? $module_name : $this->key_name ;
0 ignored issues
show
Bug introduced by
The variable $module_name seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
789
            $layout = "<?php\n" . '$module_name=\'' . $module_name . "';\n" . '$subpanel_layout = ' . var_export_helper ( $layout ) . ";" ;
790
            $GLOBALS [ 'log' ]->debug ( "About to save this file to $filepath" ) ;
791
            $GLOBALS [ 'log' ]->debug ( $layout ) ;
792
            $fw = sugar_fopen ( $filepath, 'w' ) ;
793
            fwrite ( $fw, $layout ) ;
794
            fclose ( $fw ) ;
795
        }
796
    }
797
798
    function getLocalSubpanelDef ($panelName)
799
    {
800
801
    }
802
803
    function createIcon ()
804
    {
805
        $icondir = $this->package_path . "/icons" ;
806
        mkdir_recursive ( $icondir ) ;
807
        $template = "" ;
808
        foreach ( $this->config [ 'templates' ] as $temp => $val )
809
            $template = $temp ;
810
        copy ( "themes/default/images/icon_$template.gif", "$icondir/icon_" . ucfirst ( $this->key_name ) . ".gif" ) ;
811
        copy ( "include/SugarObjects/templates/$template/icons/$template.gif", "$icondir/" . $this->key_name . ".gif" ) ;
812
        if (file_exists("include/SugarObjects/templates/$template/icons/Create$template.gif"))
813
        	copy ( "include/SugarObjects/templates/$template/icons/Create$template.gif", "$icondir/Create" . $this->key_name . ".gif" ) ;
814
        if (file_exists("include/SugarObjects/templates/$template/icons/{$template}_32.gif"))
815
        	copy ( "include/SugarObjects/templates/$template/icons/{$template}_32.gif", "$icondir/icon_" . $this->key_name . "_32.gif" ) ;
816
    }
817
818
    function removeFieldFromLayouts ( $fieldName )
819
    {
820
        // hardcoded list of types for now, as also hardcoded in a different form in getNodes
821
        // TODO: replace by similar mechanism to StudioModule to determine the list of available views for this module
822
        $views = array ( 'editview' , 'detailview' , 'listview' , 'basic_search' , 'advanced_search' , 'dashlet' , 'popuplist');
823
        
824
    	foreach ($views as $type )
825
        {
826
            $parser = ParserFactory::getParser( $type , $this->name , $this->package ) ;
827
            if ($parser->removeField ( $fieldName ) )
828
                $parser->handleSave(false) ; // don't populate from $_REQUEST, just save as is...
829
        }
830
		//Remove the fields in subpanel
831
        $psubs = $this->getProvidedSubpanels() ; 
832
        foreach ( $psubs as $sub )
833
        {
834
			$parser = ParserFactory::getParser( MB_LISTVIEW , $this->name, $this->package ,  $sub) ;
835
			if ($parser->removeField ( $fieldName ) )
836
	            $parser->handleSave(false) ; 
837
        }
838
    }
839
840
    /**
841
     * Returns an array of fields defs with all the link fields for this module.
842
     * @return array
843
     */
844
    public function getLinkFields(){
845
        $list = $this->relationships->getRelationshipList();
846
        $field_defs = array();
847
        foreach($list as $name){
848
            $rel = $this->relationships->get($name);
849
            $relFields = $rel->buildVardefs();
850
            $relDef = $rel->getDefinition();
851
            $relLabels = $rel->getLabels();
852
            $relatedModule = $this->key_name == $relDef['rhs_module'] ? $relDef['lhs_module'] : $relDef['rhs_module'];
853
            if (!empty($relFields[$this->key_name]))
854
            {
855
                //Massage the result of getVardefs to look like field_defs
856
                foreach($relFields[$this->key_name] as $def) {
857
                    $def['module'] = $relatedModule;
858
                    $def['translated_label'] = empty($relLabels[$this->key_name][$def['vname']]) ?
859
                        $name : $relLabels[$this->key_name][$def['vname']];
860
                    $field_defs[$def['name']] = $def;
861
                }
862
            }
863
        }
864
        
865
        return $field_defs;
866
    }
867
868
    /**
869
     * Returns a TemplateField object by name
870
     * Returns a TemplateField object by name or null if field not exists. If type not set use text type as default
871
     *
872
     * @param string $name
873
     * @return TemplateField|null
874
     *
875
     */
876
    public function getField($name)
877
    {
878
        $field = null;
879
        $varDefs = $this->getVardefs();
880
        if (isset($varDefs['fields'][$name])){
881
            $fieldVarDefs = $varDefs['fields'][$name];
882
            if (!isset($fieldVarDefs['type'])){
883
                $fieldVarDefs['type'] = 'varchar';
884
            }
885
            $field = get_widget($fieldVarDefs['type']);
886
            foreach($fieldVarDefs AS $key => $opt){
887
                $field->$key = $opt;
888
            }
889
        }
890
        return $field;
891
    }
892
893
}
894
?>
895