]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/DashletMetaDataParser.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / views / DashletMetaDataParser.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39  require_once ('modules/ModuleBuilder/parsers/views/ListLayoutMetaDataParser.php') ;
40  require_once ('modules/ModuleBuilder/parsers/views/SearchViewMetaDataParser.php') ;
41  require_once 'modules/ModuleBuilder/parsers/constants.php' ;
42
43  class DashletMetaDataParser extends ListLayoutMetaDataParser
44  {
45
46         // Columns is used by the view to construct the listview - each column is built by calling the named function
47         public $columns = array ( 'LBL_DEFAULT' => 'getDefaultFields' , 'LBL_AVAILABLE' => 'getAdditionalFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
48
49         /*
50          * Constructor
51          * Must set:
52          * $this->columns   Array of 'Column LBL'=>function_to_retrieve_fields_for_this_column() - expected by the view
53          *
54          * @param string moduleName     The name of the module to which this listview belongs
55          * @param string packageName    If not empty, the name of the package to which this listview belongs
56          */
57          function __construct ($view, $moduleName , $packageName = '')
58          {
59
60                 $this->search = ($view == MB_DASHLETSEARCH) ? true : false;
61                 $this->_moduleName = $moduleName;
62                 $this->_packageName = $packageName;
63                 $this->_view = $view ;
64                 if ($this->search)
65                 {
66                         $this->columns = array ( 'LBL_DEFAULT' => 'getAdditionalFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
67                         parent::__construct ( MB_DASHLETSEARCH, $moduleName, $packageName ) ;
68                 } else
69                 {
70                         parent::__construct ( MB_DASHLET, $moduleName, $packageName ) ;
71                 }
72                 $this->_viewdefs = $this->mergeFieldDefinitions($this->_viewdefs, $this->_fielddefs);
73          }
74
75          /**
76           * Dashlets contain both a searchview and list view definition, therefore we need to merge only the relevant info
77           */
78     function mergeFieldDefinitions ( $viewdefs, $fielddefs ) {
79         if ($this->_view == MB_DASHLETSEARCH && isset($viewdefs['searchfields']))
80         {
81                 //Remove any relate fields from the possible defs as they will break the homepage
82                 foreach($fielddefs as $id=>$def) {
83                         if (isset($def['type']) && $def['type'] == 'relate') {
84                                 if( isset($fielddefs[$id]['id_name'])){
85                                 $fielddefs[$fielddefs[$id]['id_name']] = $def;
86                                 unset($fielddefs[$id]);
87                         }
88                 }
89                 }
90                 $viewdefs = array_change_key_case($viewdefs['searchfields']);
91                 $viewdefs = $this->_viewdefs = $this->convertSearchToListDefs($viewdefs);
92         }
93         else if ($this->_view == MB_DASHLET && isset($viewdefs['columns']))
94         {
95                 $viewdefs = $this->_viewdefs = array_change_key_case($viewdefs['columns']);
96                 $viewdefs = $this->_viewdefs = $this->convertSearchToListDefs($viewdefs);
97         }
98         
99         return $viewdefs;
100     }
101
102     function convertSearchToListDefs($defs) {
103         $temp = array();
104         foreach($defs as $key=>$value) {
105                 $temp[$key] = $value;
106                 if (!isset ($temp[$key]['name'])) {
107                         $temp[$key]['name'] = $key;
108                 }
109         }
110         return $temp;
111     }
112
113         private function ConvertSearchToDashletDefs($defs) {
114                 $temp = array();
115         foreach($defs as $key=>$value) {
116                 if($value['default']) {
117                         //$temp[$key] = $value;
118                         $temp[$key] = array('default' => '');
119                 }else{
120                         $temp[$key] = $value;
121                 }
122         }
123         return $temp;
124     }
125
126     function handleSave ($populate = true)
127     {
128         if (empty (  $this->_packageName ))
129         {
130                 foreach(array(MB_CUSTOMMETADATALOCATION , MB_BASEMETADATALOCATION) as $value){
131                         $file = $this->implementation->getFileName(MB_DASHLET, $this->_moduleName, $value);
132                         if(file_exists($file)){
133                                 break;
134                         }
135                 }
136                 $writeTodashletName = $dashletName = $this->implementation->getLanguage().'Dashlet';
137                 if(!file_exists($file)){
138                         $file = "modules/{$this->_moduleName}/Dashlets/My{$this->_moduleName}Dashlet/My{$this->_moduleName}Dashlet.data.php";
139                         $dashletName = 'My'.$this->implementation->getLanguage().'Dashlet';
140                 }
141                 $writeFile = $this->implementation->getFileName(MB_DASHLET, $this->_moduleName);
142                 if(!file_exists($writeFile)){
143                         mkdir_recursive ( dirname ( $writeFile ) ) ;
144                 }
145         }
146         else{
147                 $writeFile = $file = $this->implementation->getFileName(MB_DASHLET, $this->_moduleName, $this->_packageName);
148                 $writeTodashletName = $dashletName =$this->implementation->module->key_name . 'Dashlet';
149         }
150         
151         $this->implementation->_history->append ( $file ) ;
152         if ($populate)
153            $this->_populateFromRequest() ;
154         $out = "<?php\n" ;
155
156         require($file);
157         if (!isset($dashletData[$dashletName])) {
158                 sugar_die ("unable to load Module Dashlet Definition");
159         }
160         if ($fh = sugar_fopen ( $writeFile, 'w' ))
161         {
162                 if ($this->_view == MB_DASHLETSEARCH)
163                 {
164                         $dashletData[$dashletName]['searchFields'] = $this->ConvertSearchToDashletDefs($this->_viewdefs);
165                 } else
166                 {
167                         $dashletData[$dashletName]['columns'] = $this->_viewdefs;
168                 }
169                 $out .= "\$dashletData['$writeTodashletName']['searchFields'] = " . var_export_helper ($dashletData[$dashletName]['searchFields']) . ";\n";
170                 $out .= "\$dashletData['$writeTodashletName']['columns'] = " . var_export_helper ($dashletData[$dashletName]['columns']) . ";\n";
171                 fputs ( $fh, $out) ;
172                 fclose ( $fh ) ;
173         }
174     }
175  }
176  ?>