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-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
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' ;
43 class PopupMetaDataParser extends ListLayoutMetaDataParser
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' ) ;
49 public static $reserveProperties = array('moduleMain', 'varName' , 'orderBy', 'whereClauses', 'searchInputs', 'create');
51 public static $defsMap = array(MB_POPUPSEARCH => 'searchdefs' , MB_POPUPLIST => 'listviewdefs');
56 * $this->columns Array of 'Column LBL'=>function_to_retrieve_fields_for_this_column() - expected by the view
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
61 function __construct ($view, $moduleName , $packageName = '')
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' ) ;
71 $this->columns = array ( 'LBL_DEFAULT' => 'getSearchFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
72 parent::__construct ( MB_POPUPSEARCH, $moduleName, $packageName ) ;
75 parent::__construct ( MB_POPUPLIST, $moduleName, $packageName ) ;
78 $this->_viewdefs = $this->mergeFieldDefinitions($this->_viewdefs, $this->_fielddefs);
82 * Dashlets contain both a searchview and list view definition, therefore we need to merge only the relevant info
84 function mergeFieldDefinitions ( $viewdefs, $fielddefs ) {
85 $viewdefs = $this->_viewdefs = array_change_key_case($viewdefs );
86 $viewdefs = $this->_viewdefs = $this->convertSearchToListDefs($viewdefs);
90 function convertSearchToListDefs($defs) {
92 foreach($defs as $key=>$value) {
93 if(!is_array($value)){
94 $temp[$value] = array('name'=>$value);
97 if(isset($value['name']) && $value['name'] != $key){
98 $temp[$value['name']] = $value;
100 }else if( !isset($value['name']) ){
101 $temp[$key]['name'] = $key;
108 function getOriginalViewDefs(){
109 $defs = parent::getOriginalViewDefs();
110 return $this->convertSearchToListDefs($defs);
113 public function getSearchFields()
115 $searchFields = array ( ) ;
116 foreach ( $this->_viewdefs as $key => $def )
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'];
124 $searchFields [ $key ] = $def;
128 return $searchFields ;
131 function handleSave ($populate = true)
133 if (empty ( $this->_packageName ))
135 foreach(array(MB_CUSTOMMETADATALOCATION , MB_BASEMETADATALOCATION) as $value){
136 $file = $this->implementation->getFileName(MB_POPUPLIST, $this->_moduleName, $value);
137 if(file_exists($file)){
141 $writeFile = $this->implementation->getFileName(MB_POPUPLIST, $this->_moduleName);
142 if(!file_exists($writeFile)){
143 mkdir_recursive ( dirname ( $writeFile ) ) ;
147 $writeFile = $file = $this->implementation->getFileName(MB_POPUPLIST, $this->_moduleName, $this->_packageName);
149 $this->implementation->_history->append ( $file ) ;
151 $this->_populateFromRequest() ;
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);
159 if (!isset($popupMeta)) {
160 sugar_die ("unable to load Module Popup Definition");
163 if ($this->_view == MB_POPUPSEARCH)
165 foreach($this->_viewdefs as $k => $v){
166 if(isset($this->_viewdefs[$k]) && isset($this->_viewdefs[$k]['default'])){
167 unset($this->_viewdefs[$k]['default']);
170 $this->_viewdefs = $this->convertSearchToListDefs($this->_viewdefs);
171 $popupMeta['searchdefs'] = $this->_viewdefs;
172 $this->addNewSearchDef($this->_viewdefs , $popupMeta);
175 $popupMeta['listviewdefs'] = array_change_key_case($this->_viewdefs , CASE_UPPER );
177 $allProperties = array_merge(self::$reserveProperties , array('searchdefs', 'listviewdefs'));
179 $out .= "\$popupMeta = array (\n";
180 foreach( $allProperties as $p){
181 if(isset($popupMeta[$p])){
182 $out .= " '$p' => ". var_export_helper ($popupMeta[$p]) . ",\n";
186 file_put_contents($writeFile, $out);
188 //return back mod strings
189 $GLOBALS['mod_strings'] = $oldModStrings;
192 public function addNewSearchDef($searchDefs, &$popupMeta){
193 if(!empty($searchDefs)){
194 $this->__diffAndUpdate( $searchDefs , $popupMeta['whereClauses'] , true);
195 $this->__diffAndUpdate( $searchDefs , $popupMeta['searchInputs'] );
199 private function __diffAndUpdate($newDefs , &$targetDefs , $forWhere = false){
200 if(!is_array($targetDefs)){
201 $targetDefs = array();
203 foreach($newDefs as $key =>$def){
204 if(!isset($targetDefs[$key]) && $forWhere){
205 $targetDefs[$key] = $this->__getTargetModuleName($def).'.'.$key;
206 }else if( !in_array($key , $targetDefs ) && !$forWhere){
207 array_push($targetDefs , $key);
212 foreach(array_diff( array_keys($targetDefs) , array_keys($newDefs) ) as $key ){
213 unset($targetDefs[$key]);
216 foreach($targetDefs as $key =>$value){
217 if(!isset($newDefs[$value]))
218 unset($targetDefs[$key]);
224 private function __getTargetModuleName($def){
225 $dir = strtolower($this->implementation->getModuleDir());
226 if(isset($this->_fielddefs[$def['name']]) && isset($this->_fielddefs[$def['name']]['source']) && $this->_fielddefs[$def['name']]['source'] == 'custom_fields'){