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.
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 ********************************************************************************/
40 require_once ('modules/ModuleBuilder/parsers/views/ListLayoutMetaDataParser.php') ;
41 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
43 class SearchViewMetaDataParser extends ListLayoutMetaDataParser
45 static $variableMap = array (
46 MB_BASICSEARCH => 'basic_search' ,
47 MB_ADVANCEDSEARCH => 'advanced_search' ,
49 // Columns is used by the view to construct the listview - each column is built by calling the named function
50 public $columns = array ( 'LBL_DEFAULT' => 'getDefaultFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
51 protected $allowParent = true;
56 * $this->columns Array of 'Column LBL'=>function_to_retrieve_fields_for_this_column() - expected by the view
57 * @param string searchLayout The type of search layout, e.g., MB_BASICSEARCH or MB_ADVANCEDSEARCH
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 ($searchLayout, $moduleName , $packageName = '')
63 $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . ": __construct( $searchLayout , $moduleName , $packageName )" ) ;
66 if (! isset ( self::$variableMap [ $searchLayout ] ) )
68 sugar_die ( get_class ( $this ) . ": View $searchLayout is not supported" ) ;
72 $this->_searchLayout = $searchLayout ;
74 // unsophisticated error handling for now...
77 if (empty ( $packageName ))
79 require_once 'modules/ModuleBuilder/parsers/views/DeployedMetaDataImplementation.php' ;
80 $this->implementation = new DeployedMetaDataImplementation ( $searchLayout, $moduleName ) ;
83 require_once 'modules/ModuleBuilder/parsers/views/UndeployedMetaDataImplementation.php' ;
84 $this->implementation = new UndeployedMetaDataImplementation ( $searchLayout, $moduleName, $packageName ) ;
86 } catch (Exception $e)
91 $this->_saved = array_change_key_case ( $this->implementation->getViewdefs () ) ; // force to lower case so don't have problems with case mismatches later
92 if(isset($this->_saved['templatemeta'])) {
93 $this->_saved['templateMeta'] = $this->_saved['templatemeta'];
94 unset($this->_saved['templatemeta']);
97 if ( ! isset ( $this->_saved [ 'layout' ] [ self::$variableMap [ $this->_searchLayout ] ] ) )
99 // attempt to fallback on a basic_search layout...
101 if ( ! isset ( $this->_saved [ 'layout' ] [ self::$variableMap [ MB_BASICSEARCH ] ] ) )
102 throw new Exception ( get_class ( $this ) . ": {$this->_searchLayout} does not exist for module $moduleName" ) ;
104 $this->_saved [ 'layout'] [ MB_ADVANCEDSEARCH ] = $this->_saved [ 'layout' ] [ MB_BASICSEARCH ] ;
107 $this->view = $searchLayout;
108 // convert the search view layout (which has its own unique layout form) to the standard listview layout so that the parser methods and views can be reused
109 $this->_viewdefs = $this->convertSearchViewToListView ( $this->_saved [ 'layout' ] [ self::$variableMap [ $this->_searchLayout ] ] ) ;
110 $this->_fielddefs = $this->implementation->getFielddefs () ;
111 $this->_standardizeFieldLabels( $this->_fielddefs );
115 public function isValidField($key, $def)
117 if(isset($def['type']) && $def['type'] == "assigned_user_name")
119 $origDefs = $this->getOriginalViewDefs();
120 if (isset($def['group']) && isset($origDefs[$def['group']]))
122 if (!isset($def [ 'studio' ]) || (is_array($def [ 'studio' ]) && !isset($def [ 'studio' ]['searchview'])))
126 if (isset($def [ 'studio' ]) && is_array($def [ 'studio' ]) && isset($def [ 'studio' ]['searchview']))
128 return $def [ 'studio' ]['searchview'] !== false &&
129 ($def [ 'studio' ]['searchview'] === true || $def [ 'studio' ]['searchview'] != 'false');
132 if (!parent::isValidField($key, $def))
135 //Special case to prevent multiple copies of assigned, modified, or created by user on the search view
136 if (empty ($def[ 'studio' ] ) && $key == "assigned_user_name")
138 $origDefs = $this->getOriginalViewDefs();
139 if ($key == "assigned_user_name" && isset($origDefs['assigned_user_id']))
142 if (substr($key, -8) == "_by_name" && isset($def['rname']) && $def['rname'] == "user_name")
145 //Remove image fields (unless studio was set)
146 if (!empty($def [ 'studio' ]) && isset($def['type']) && $def['type'] == "image")
153 * Save the modified searchLayout
154 * Have to preserve the original layout format, which is array('metadata'=>array,'layouts'=>array('basic'=>array,'advanced'=>array))
156 function handleSave ($populate = true)
159 $this->_populateFromRequest() ;
162 $this->_saved [ 'layout' ] [ self::$variableMap [ $this->_searchLayout ] ] = $this->convertSearchViewToListView($this->_viewdefs);;
163 $this->implementation->deploy ( $this->_saved ) ;
166 private function convertSearchViewToListView ($viewdefs)
169 foreach ( $viewdefs as $key => $value )
171 if (! is_array ( $value ))
175 $def[ 'name' ] = $key;
179 if (!isset ( $value [ 'name' ] ))
181 $value [ 'name' ] = $key;
185 $key = $value [ 'name' ] ; // override key with name, needed when the entry lacks a key
187 // now add in the standard listview default=>true
188 $value [ 'default' ] = true ;
189 $temp [ strtolower ( $key ) ] = $value ;
195 function normalizeDefs($defs) {
197 foreach ($defs as $def)
199 if (is_array($def) && isset($def['name']))
201 $out[strtolower($def['name'])] = $def;
207 function getOriginalViewDefs() {
208 $defs = $this->implementation->getOriginalViewdefs ();
210 if (!empty($defs) && !empty($defs['layout']) && !empty($defs['layout'][$this->_searchLayout]))
212 if($this->_searchLayout == "basic_search" && !empty($defs['layout']["advanced_search"]))
214 $out = $this->normalizeDefs($defs['layout']["advanced_search"]);
216 $out = array_merge($out, $this->normalizeDefs($defs['layout'][$this->_searchLayout]));