]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/SearchViewMetaDataParser.php
Release 6.5.0
[Github/sugarcrm.git] / modules / ModuleBuilder / parsers / views / SearchViewMetaDataParser.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
40 require_once ('modules/ModuleBuilder/parsers/views/ListLayoutMetaDataParser.php') ;
41 require_once 'modules/ModuleBuilder/parsers/constants.php' ;
42
43 class SearchViewMetaDataParser extends ListLayoutMetaDataParser
44 {
45     static $variableMap = array (
46                                                 MB_BASICSEARCH => 'basic_search' ,
47                                                 MB_ADVANCEDSEARCH => 'advanced_search' ,
48                                                 ) ;
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;
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      * @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
60      */
61     function __construct ($searchLayout, $moduleName , $packageName = '')
62     {
63         $GLOBALS [ 'log' ]->debug ( get_class ( $this ) . ": __construct( $searchLayout , $moduleName , $packageName )" ) ;
64
65         // BEGIN ASSERTIONS
66         if (! isset ( self::$variableMap [ $searchLayout ] ) )
67         {
68             sugar_die ( get_class ( $this ) . ": View $searchLayout is not supported" ) ;
69         }
70         // END ASSERTIONS
71
72         $this->_searchLayout = $searchLayout ;
73
74         // unsophisticated error handling for now...
75         try
76         {
77                 if (empty ( $packageName ))
78                 {
79                 require_once 'modules/ModuleBuilder/parsers/views/DeployedMetaDataImplementation.php' ;
80                 $this->implementation = new DeployedMetaDataImplementation ( $searchLayout, $moduleName ) ;
81                 } else
82                 {
83                 require_once 'modules/ModuleBuilder/parsers/views/UndeployedMetaDataImplementation.php' ;
84                 $this->implementation = new UndeployedMetaDataImplementation ( $searchLayout, $moduleName, $packageName ) ;
85                 }
86         } catch (Exception $e)
87         {
88                 throw $e ;
89         }
90
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']);
95         }
96
97         if ( ! isset ( $this->_saved [ 'layout' ] [ self::$variableMap [ $this->_searchLayout ] ] ) )
98         {
99                 // attempt to fallback on a basic_search layout...
100
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" ) ;
103
104                 $this->_saved [ 'layout'] [ MB_ADVANCEDSEARCH ] = $this->_saved [ 'layout' ] [ MB_BASICSEARCH ] ;
105         }
106
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 );
112
113     }
114
115     public function isValidField($key, $def)
116     {
117                 if(isset($def['type']) && $def['type'] == "assigned_user_name")
118                 {
119                         $origDefs = $this->getOriginalViewDefs();
120                         if (isset($def['group']) && isset($origDefs[$def['group']]))
121                                 return false;
122                         if (!isset($def [ 'studio' ]) || (is_array($def [ 'studio' ]) && !isset($def [ 'studio' ]['searchview'])))
123                                 return true;
124                 }
125                 
126     if (isset($def [ 'studio' ]) && is_array($def [ 'studio' ]) && isset($def [ 'studio' ]['searchview']))
127        {
128            return $def [ 'studio' ]['searchview'] !== false &&
129                   ($def [ 'studio' ]['searchview'] === true || $def [ 'studio' ]['searchview'] != 'false');
130        }
131                 
132         if (!parent::isValidField($key, $def))
133             return false;
134         
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")
137         {
138                 $origDefs = $this->getOriginalViewDefs();
139                 if ($key == "assigned_user_name" && isset($origDefs['assigned_user_id']))
140                         return false;
141         }
142         if (substr($key, -8) == "_by_name" &&  isset($def['rname']) && $def['rname'] == "user_name")
143                 return false;
144
145         //Remove image fields (unless studio was set)
146         if (!empty($def [ 'studio' ]) && isset($def['type']) && $def['type'] == "image")
147            return false;
148         
149        return true;
150     }
151
152     /*
153      * Save the modified searchLayout
154      * Have to preserve the original layout format, which is array('metadata'=>array,'layouts'=>array('basic'=>array,'advanced'=>array))
155      */
156     function handleSave ($populate = true)
157     {
158         if ($populate)
159             $this->_populateFromRequest() ;
160             
161             
162         $this->_saved [ 'layout' ] [ self::$variableMap [ $this->_searchLayout ] ] = $this->convertSearchViewToListView($this->_viewdefs);;
163         $this->implementation->deploy ( $this->_saved ) ;
164     }
165
166     private function convertSearchViewToListView ($viewdefs)
167     {
168         $temp = array ( ) ;
169         foreach ( $viewdefs as $key => $value )
170         {
171             if (! is_array ( $value ))
172             {
173                 $key = $value ;
174                 $def = array ( ) ;
175                 $def[ 'name' ] = $key;
176                 $value = $def ;
177             }
178
179             if (!isset ( $value [ 'name' ] ))
180             {
181                 $value [ 'name' ] = $key;
182             }
183             else
184             {
185                 $key = $value [ 'name' ] ; // override key with name, needed when the entry lacks a key
186             }
187             // now add in the standard listview default=>true
188             $value [ 'default' ] = true ;
189             $temp [ strtolower ( $key ) ] = $value ;
190         }
191         return $temp ;
192     }
193
194
195     function normalizeDefs($defs) {
196         $out = array();
197         foreach ($defs as $def)
198         {
199             if (is_array($def) && isset($def['name']))
200             {
201                 $out[strtolower($def['name'])] = $def;
202             }
203         }
204         return $out;
205     }
206
207     function getOriginalViewDefs() {
208         $defs = $this->implementation->getOriginalViewdefs ();
209         $out = array();
210         if (!empty($defs) && !empty($defs['layout']) && !empty($defs['layout'][$this->_searchLayout]))
211         {
212             if($this->_searchLayout == "basic_search" &&  !empty($defs['layout']["advanced_search"]))
213             {
214                 $out = $this->normalizeDefs($defs['layout']["advanced_search"]);
215             }
216             $out = array_merge($out, $this->normalizeDefs($defs['layout'][$this->_searchLayout]));
217         }
218
219         return $out;
220     }
221 }
222 ?>