]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/PopupMetaDataParser.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / views / PopupMetaDataParser.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 PopupMetaDataParser 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         public static $reserveProperties = array('moduleMain', 'varName' , 'orderBy', 'whereClauses', 'searchInputs', 'create','addToReserve');
50         
51         public static $defsMap = array(MB_POPUPSEARCH => 'searchdefs' , MB_POPUPLIST => 'listviewdefs');
52
53         /*
54          * Constructor
55          * Must set:
56          * $this->columns   Array of 'Column LBL'=>function_to_retrieve_fields_for_this_column() - expected by the view
57          *
58          * @param string moduleName     The name of the module to which this listview belongs
59          * @param string packageName    If not empty, the name of the package to which this listview belongs
60          */
61          function __construct ($view, $moduleName , $packageName = '')
62          {
63                 $this->search = ($view == MB_POPUPSEARCH) ? true : false;
64                 $this->_moduleName = $moduleName;
65                 $this->_packageName = $packageName;
66                 $this->_view = $view ;
67                 $this->columns = array ( 'LBL_DEFAULT' => 'getDefaultFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
68                 
69                 if ($this->search)
70                 {
71                         $this->columns = array ( 'LBL_DEFAULT' => 'getSearchFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
72                         parent::__construct ( MB_POPUPSEARCH, $moduleName, $packageName ) ;
73                 } else
74                 {
75                         parent::__construct ( MB_POPUPLIST, $moduleName, $packageName ) ;
76                 }
77                 
78                 $this->_viewdefs = $this->mergeFieldDefinitions($this->_viewdefs, $this->_fielddefs);
79          }
80
81          /**
82           * Dashlets contain both a searchview and list view definition, therefore we need to merge only the relevant info
83           */
84     function mergeFieldDefinitions ( $viewdefs, $fielddefs ) {
85         $viewdefs = $this->_viewdefs = array_change_key_case($viewdefs );
86                 $viewdefs = $this->_viewdefs = $this->convertSearchToListDefs($viewdefs);
87         return $viewdefs;
88     }
89         
90     function convertSearchToListDefs($defs) {
91         $temp = array();
92         foreach($defs as $key=>$value) {
93                         if(!is_array($value)){
94                                 $temp[$value] = array('name'=>$value);
95                         }else{
96                                 $temp[$key] = $value;
97                                 if(isset($value['name']) && $value['name'] != $key){
98                                         $temp[$value['name']] = $value;
99                                         unset($temp[$key] );
100                                 }else if( !isset($value['name']) ){
101                                         $temp[$key]['name'] = $key;
102                                 }
103                         }
104         }
105         return $temp;
106     }
107         
108         function getOriginalViewDefs(){
109                 $defs = parent::getOriginalViewDefs();
110                 return $this->convertSearchToListDefs($defs);
111         }
112         
113         public function getSearchFields()
114         {
115                 $searchFields = array ( ) ;
116         foreach ( $this->_viewdefs as $key => $def )
117         {
118             if (isset($this->_fielddefs [ $key ] )) {
119                                 $searchFields [ $key ] = self::_trimFieldDefs ( $this->_fielddefs [ $key ] ) ;
120                                 if (!empty($def['label']))
121                                    $searchFields [ $key ]['label'] = $def['label'];
122             }
123                         else {
124                                 $searchFields [ $key ] = $def;
125                         }
126         }
127
128         return $searchFields ;
129         }
130
131     function handleSave ($populate = true)
132    {
133         if (empty (  $this->_packageName ))
134         {
135                 foreach(array(MB_CUSTOMMETADATALOCATION , MB_BASEMETADATALOCATION) as $value){
136                         $file = $this->implementation->getFileName(MB_POPUPLIST, $this->_moduleName, $value);
137                         if(file_exists($file)){
138                                 break;
139                         }
140                 }
141                 $writeFile = $this->implementation->getFileName(MB_POPUPLIST, $this->_moduleName);
142                 if(!file_exists($writeFile)){
143                         mkdir_recursive ( dirname ( $writeFile ) ) ;
144                 }
145         }
146         else{
147                 $writeFile = $file = $this->implementation->getFileName(MB_POPUPLIST, $this->_moduleName, $this->_packageName);
148         }
149         $this->implementation->_history->append ( $file ) ;
150         if ($populate)
151            $this->_populateFromRequest() ;
152         $out = "<?php\n" ;
153
154                 //Load current module languages
155                 global $mod_strings , $current_language;
156                 $oldModStrings = $mod_strings;
157                 $GLOBALS['mod_strings'] = return_module_language($current_language , $this->_moduleName);
158         require($file);
159         if (!isset($popupMeta)) {
160                 sugar_die ("unable to load Module Popup Definition");
161         }
162         
163         if ($this->_view == MB_POPUPSEARCH)
164         {
165                 foreach($this->_viewdefs as $k => $v){
166                         if(isset($this->_viewdefs[$k]) && isset($this->_viewdefs[$k]['default'])){
167                                 unset($this->_viewdefs[$k]['default']);
168                         }
169                 }
170                 $this->_viewdefs = $this->convertSearchToListDefs($this->_viewdefs);
171                 $popupMeta['searchdefs'] = $this->_viewdefs;
172                 $this->addNewSearchDef($this->_viewdefs , $popupMeta);
173         } else
174         {
175                 $popupMeta['listviewdefs'] = array_change_key_case($this->_viewdefs , CASE_UPPER );
176         }
177
178         //provide a way for users to add to the reserve properties list via the 'addToReserve' element
179         $totalReserveProps = self::$reserveProperties;
180         if(!empty($popupMeta['addToReserve'])){
181            $totalReserveProps =  array_merge(self::$reserveProperties,$popupMeta['addToReserve']);
182         }
183         $allProperties = array_merge($totalReserveProps , array('searchdefs', 'listviewdefs'));
184         
185         $out .= "\$popupMeta = array (\n";
186         foreach( $allProperties as $p){
187                 if(isset($popupMeta[$p])){
188                         $out .= "    '$p' => ". var_export_helper ($popupMeta[$p]) . ",\n";
189                 }
190         }
191         $out .= ");\n";
192         file_put_contents($writeFile, $out);
193         
194         //return back mod strings
195         $GLOBALS['mod_strings'] = $oldModStrings;
196     }
197     
198     public function addNewSearchDef($searchDefs, &$popupMeta){
199         if(!empty($searchDefs)){
200                         $this->__diffAndUpdate( $searchDefs , $popupMeta['whereClauses'] , true);
201                         $this->__diffAndUpdate( $searchDefs , $popupMeta['searchInputs'] );
202         }
203     }
204     
205     private function __diffAndUpdate($newDefs , &$targetDefs , $forWhere = false){
206         if(!is_array($targetDefs)){
207                 $targetDefs = array();
208         }
209         foreach($newDefs as $key =>$def){
210                 if(!isset($targetDefs[$key]) && $forWhere){
211                         $targetDefs[$key] = $this->__getTargetModuleName($def).'.'.$key;
212                 }else if( !in_array($key , $targetDefs ) && !$forWhere){
213                                 array_push($targetDefs , $key);
214                 }
215         }
216         
217         if($forWhere){
218                 foreach(array_diff(  array_keys($targetDefs) , array_keys($newDefs) ) as $key ){
219                         unset($targetDefs[$key]);
220                 }
221         }else{
222                 foreach($targetDefs as $key =>$value){
223                         if(!isset($newDefs[$value])) 
224                                 unset($targetDefs[$key]);
225                 }
226         }
227         
228     }
229     
230     private function __getTargetModuleName($def){
231         $dir = strtolower($this->implementation->getModuleDir());
232         if(isset($this->_fielddefs[$def['name']]) && isset($this->_fielddefs[$def['name']]['source']) && $this->_fielddefs[$def['name']]['source'] == 'custom_fields'){
233                 return $dir.'_cstm';
234         }
235         
236         return $dir;
237     }
238     
239  }
240  ?>