]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Parsers/SearchFormMetaParser.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarFields / Parsers / SearchFormMetaParser.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 require_once('include/SugarFields/Parsers/MetaParser.php');
38
39 class SearchFormMetaParser extends MetaParser {
40
41 function SearchFormMetaParser() {
42         $this->mView = 'Search';
43 }
44
45 /**
46  * parse
47  * @param $mixed 
48  * @return $obj A MetaDataBean instance
49  **/
50 function parse($filePath, $vardefs = array(), $moduleDir = '', $merge=false, $masterCopy=null) {
51
52    $contents = file_get_contents($filePath);
53    $contents = $this->trimHTML($contents);
54    
55    // Get the second table in the page and onward
56    $tables = $this->getElementsByType("table", $contents);
57    //basic search table
58    $basicSection = $this->processSection("basic", $tables[0], $filePath, $vardefs);
59    $advancedSection = $this->processSection("advanced", $tables[1], $filePath, $vardefs);
60    if(file_exists($masterCopy)) {
61            require($masterCopy);
62            $layouts = $searchdefs[$moduleDir]['layout'];
63         
64            if(isset($layouts['basic_search'])) {
65                   $basicSection = $this->mergeSection($basicSection, $layouts['basic_search']);
66               $basicSection = $this->applyRules($moduleDir, $basicSection);
67            }
68         
69            if(isset($layouts['advanced_search'])) {
70                   $advancedSection = $this->mergeSection($advancedSection, $layouts['advanced_search']);
71               $advancedSection = $this->applyRules($moduleDir, $advancedSection);
72            }
73    } //if
74
75 $header = "<?php\n\n";
76 $header .= "\$searchdefs['$moduleDir'] = array(
77     'templateMeta' => array('maxColumns' => '3', 'widths' => array('label' => '10', 'field' => '30')),
78     'layout' => array(                                          
79 \n\t'basic_search' =>";   
80 $header .= "\t" . var_export($basicSection, true);
81 $header .= "\n\t,'advanced_search' =>";
82 $header .= "\t" . var_export($advancedSection, true);
83 $header .= "
84      ),\n
85 );
86 ?>";
87
88 $header = preg_replace('/(\d+)[\s]=>[\s]?/',"",$header);
89 return $header;
90
91 }
92
93 function mergeSection($section, $masterSection) {
94
95   // Get all the names in the panel
96   $existingElements = array();
97   $existingLocation = array();
98   
99   foreach($section as $rowKey=>$row) {
100            if(is_array($row) && !empty($row['name'])) {
101                    $existingElements[$row['name']] = $row['name'];
102                    $existingLocation[$row['name']] = array("row"=>$rowKey);     
103            } else if(!is_array($row) && !empty($row)) {
104                    $existingElements[$row] = $row;
105                    $existingLocation[$row] = array("row"=>$rowKey);
106            }
107   } //foreach
108   
109   // Now check against the $masterCopy
110   foreach($masterSection as $row) {
111         
112          $addEntry = '';
113          $id = is_array($row) ? $row['name'] : $row;
114
115      /*
116          if(!isset($existingElements[$id])) {
117                 //$addEntry = $row;
118          }
119          */
120          
121          if(isset($existingElements[$id])) {
122                 //Use master copy instead
123                 $section[$existingLocation[$id]['row']] = $row;
124          }
125      
126          // Add it to the $panels 
127          /*
128          if(!empty($addEntry)) {
129                 $section[] = $addEntry;
130          }
131          */
132   } //foreach
133
134   return $section;
135 }
136
137 function processSection($section, $table, $filePath, $vardefs=array()) {
138    
139    $toptr = $this->getElementsByType("tr", $table);
140    
141    if(!is_array($toptr) || empty($toptr)) {
142           $GLOBALS['log']->error("Could not process top row (<tr>) for $section section");
143           $GLOBALS['log']->error($table);
144           return array(); 
145    }
146    
147    $tabledata = $this->getElementsByType("table", $toptr[0]);
148    
149    if(empty($tabledata)) {
150           $GLOBALS['log']->error("Error: HTML format for SearchForm.html not as expected, results may not be accurate");
151           $GLOBALS['log']->error($toptr[0]);
152           $tabledata[0] = "<table>{$table}</table>";
153    }
154
155    if(is_array($tabledata) && !empty($tabledata[0])) {
156       $rows = $this->getElementsByType("tr", $tabledata[0]);
157    } else {
158           $rows = $toptr[0];
159    }
160
161    if(!is_array($rows)) {
162           return array();
163    }
164    
165    $metarow = array();
166    foreach($rows as $trow) {
167            $tablecolumns = $this->getElementsByType("td", $trow);
168           
169        $emptyCount = 0;   
170        $metacolumn = array();
171        $col = null;
172
173            foreach($tablecolumns as $tcols) {
174
175                          $spanValue = strtolower($this->getElementValue("span", $tcols));
176              $spanValue2 = strtolower($this->getElementValue("slot", $tcols));  
177              $spanValue = !empty($spanValue2) ? $spanValue2 : $spanValue;
178              $spanValue3 = strtolower($this->getElementValue("td", $tcols));
179              $spanValue = !empty($spanValue3) ? $spanValue3 : $spanValue; 
180              
181              //Get all the editable form elements' names
182                          $formElementNames = $this->getFormElementsNames($spanValue);            
183                          $customField = $this->getCustomField($formElementNames);
184                          
185                          $name = '';
186              $fields = null;
187              $customCode = null;
188                         
189              if(!empty($customField)) {           
190                // If it's a custom field we just set the name
191                $name = $customField;
192                          } else if(is_array($formElementNames) && count($formElementNames) == 1 
193                                    && (isset($vardefs[$formElementNames[0]]) || $formElementNames[0] == 'current_user_only')) {
194                            $name = $formElementNames[0];
195                          } 
196
197                          //Skip and continue if $name is empty
198                          if(empty($name)) {
199                             continue;   
200                          }
201                          
202                          // Build the entry
203                          if(preg_match("/<textarea/si", $spanValue)) {
204                                 //special case for textarea form elements (add the displayParams)
205                                 $displayParams = array();
206                             $displayParams['rows'] = $this->getTagAttribute("rows", $spanValue);
207                             $displayParams['cols'] = $this->getTagAttribute("cols", $spanValue);
208
209                             if(!empty($displayParams['rows']) && !empty($displayParams['cols'])) {
210                                     $field = array();
211                                     $field['name'] = $name;
212                                         $field['displayParams'] = $displayParams;
213                             } else {
214                                 $field = $name; 
215                             }
216                             
217                             $col = $field;
218                            
219                          } else {
220
221                                 if(isset($fields)) {
222                                    $field = array();
223                                    $field['name'] = $name;
224                                    if(isset($fields)) {
225                                           $field['fields'] = $fields;
226                                    }
227                                    
228                                    if(!empty($customCode)) {
229                                           $field['customCode'] = $customCode;
230                                    }
231                    
232                                    $col = $field;
233                                 } else {
234                                    $emptyCount = $name == '' ? $emptyCount + 1 : $emptyCount;
235                                    $col = $name;
236                                 }       
237                          } //if-else if-else block
238              $metarow[] = $col;
239            } //foreach
240
241            // One last final check.  If $emptyCount does not equal Array $col count, don't add 
242            if($emptyCount != count($col)) {
243               //$metarow[] = $col;
244            } //if
245
246    } //foreach  
247         
248    return $metarow;
249 }
250
251 function applyRules($moduleDir, $section=array()) {
252    require_once('include/SugarFields/Parsers/Rules/BaseRule.php');
253    $baseRule = new BaseRule();
254    if(!is_array($section)) {
255           $GLOBALS['log']->error("Error: SearchFormMetaParser->applyRules expects Array");
256           return $section;
257    }
258    
259    foreach($section as $key=>$row) {
260           //Override email1 fields
261           if($baseRule->matches($row, '/^email1$/si')) {
262                  $section[$key] = array('name' => 'email', 'label' =>'LBL_ANY_EMAIL', 'type' => 'name');
263           }
264    }
265    return $section;
266 }
267
268 }
269 ?>
270