]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/ModuleBuilder/parsers/views/SearchViewMetaDataParser.php
Release 6.2.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-2011 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 (!parent::isValidField($key, $def))
127             return false;
128
129         if (isset($def [ 'studio' ]) && is_array($def [ 'studio' ]) && isset($def [ 'studio' ]['searchview']))
130         {
131                 return $def [ 'studio' ]['searchview'] !== false && $def [ 'studio' ]['searchview'] != 'false';
132         }
133         
134         //Special case to prevent multiple copies of assigned, modified, or created by user on the search view
135         if (empty ($def[ 'studio' ] ) && $key == "assigned_user_name")
136         {
137                 $origDefs = $this->getOriginalViewDefs();
138                 if ($key == "assigned_user_name" && isset($origDefs['assigned_user_id']))
139                         return false;
140         }
141         if (substr($key, -8) == "_by_name" &&  isset($def['rname']) && $def['rname'] == "user_name")
142                 return false;
143
144         //Remove image fields (unless studio was set)
145         if (!empty($def [ 'studio' ]) && isset($def['type']) && $def['type'] == "image")
146            return false;
147         
148        return true;
149     }
150
151     /*
152      * Save the modified searchLayout
153      * Have to preserve the original layout format, which is array('metadata'=>array,'layouts'=>array('basic'=>array,'advanced'=>array))
154      */
155     function handleSave ($populate = true)
156     {
157         if ($populate)
158             $this->_populateFromRequest() ;
159             
160             
161         $this->_saved [ 'layout' ] [ self::$variableMap [ $this->_searchLayout ] ] = $this->convertSearchViewToListView($this->_viewdefs);;
162         $this->implementation->deploy ( $this->_saved ) ;
163     }
164
165     private function convertSearchViewToListView ($viewdefs)
166     {
167         $temp = array ( ) ;
168         foreach ( $viewdefs as $key => $value )
169         {
170             if (! is_array ( $value ))
171             {
172                 $key = $value ;
173                 $def = array ( ) ;
174                 $def[ 'name' ] = $key;
175                 $value = $def ;
176             }
177
178             if (!isset ( $value [ 'name' ] ))
179             {
180                 $value [ 'name' ] = $key;
181             }
182             else
183             {
184                 $key = $value [ 'name' ] ; // override key with name, needed when the entry lacks a key
185             }
186             // now add in the standard listview default=>true
187             $value [ 'default' ] = true ;
188             $temp [ strtolower ( $key ) ] = $value ;
189         }
190         return $temp ;
191     }
192     
193     function getOriginalViewDefs() {
194         $defs = $this->implementation->getOriginalViewdefs ();
195         $out = array();
196         if (!empty($defs) && !empty($defs['layout']) && !empty($defs['layout'][$this->_searchLayout]))
197         {
198                 $defs = $defs['layout'][$this->_searchLayout];
199                 foreach ($defs as $def)
200                 {
201                     if (is_array($def) && isset($def['name']))
202                     {
203                         $out[strtolower($def['name'])] = $def;
204                     }
205                 }
206         }
207
208         return $out;
209     }
210 }
211 ?>