2 /*********************************************************************************
3 * SugarCRM is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
37 require_once('include/SugarFields/Parsers/MetaParser.php');
39 class SearchFormMetaParser extends MetaParser {
41 function SearchFormMetaParser() {
42 $this->mView = 'Search';
48 * @return $obj A MetaDataBean instance
50 function parse($filePath, $vardefs = array(), $moduleDir = '', $merge=false, $masterCopy=null) {
52 $contents = file_get_contents($filePath);
53 $contents = $this->trimHTML($contents);
55 // Get the second table in the page and onward
56 $tables = $this->getElementsByType("table", $contents);
58 $basicSection = $this->processSection("basic", $tables[0], $filePath, $vardefs);
59 $advancedSection = $this->processSection("advanced", $tables[1], $filePath, $vardefs);
60 if(file_exists($masterCopy)) {
62 $layouts = $searchdefs[$moduleDir]['layout'];
64 if(isset($layouts['basic_search'])) {
65 $basicSection = $this->mergeSection($basicSection, $layouts['basic_search']);
66 $basicSection = $this->applyRules($moduleDir, $basicSection);
69 if(isset($layouts['advanced_search'])) {
70 $advancedSection = $this->mergeSection($advancedSection, $layouts['advanced_search']);
71 $advancedSection = $this->applyRules($moduleDir, $advancedSection);
75 $header = "<?php\n\n";
76 $header .= "\$searchdefs['$moduleDir'] = array(
77 'templateMeta' => array('maxColumns' => '3', 'widths' => array('label' => '10', 'field' => '30')),
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);
88 $header = preg_replace('/(\d+)[\s]=>[\s]?/',"",$header);
93 function mergeSection($section, $masterSection) {
95 // Get all the names in the panel
96 $existingElements = array();
97 $existingLocation = array();
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);
109 // Now check against the $masterCopy
110 foreach($masterSection as $row) {
113 $id = is_array($row) ? $row['name'] : $row;
116 if(!isset($existingElements[$id])) {
121 if(isset($existingElements[$id])) {
122 //Use master copy instead
123 $section[$existingLocation[$id]['row']] = $row;
126 // Add it to the $panels
128 if(!empty($addEntry)) {
129 $section[] = $addEntry;
137 function processSection($section, $table, $filePath, $vardefs=array()) {
139 $toptr = $this->getElementsByType("tr", $table);
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);
147 $tabledata = $this->getElementsByType("table", $toptr[0]);
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>";
155 if(is_array($tabledata) && !empty($tabledata[0])) {
156 $rows = $this->getElementsByType("tr", $tabledata[0]);
161 if(!is_array($rows)) {
166 foreach($rows as $trow) {
167 $tablecolumns = $this->getElementsByType("td", $trow);
170 $metacolumn = array();
173 foreach($tablecolumns as $tcols) {
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;
181 //Get all the editable form elements' names
182 $formElementNames = $this->getFormElementsNames($spanValue);
183 $customField = $this->getCustomField($formElementNames);
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];
197 //Skip and continue if $name is empty
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);
209 if(!empty($displayParams['rows']) && !empty($displayParams['cols'])) {
211 $field['name'] = $name;
212 $field['displayParams'] = $displayParams;
223 $field['name'] = $name;
225 $field['fields'] = $fields;
228 if(!empty($customCode)) {
229 $field['customCode'] = $customCode;
234 $emptyCount = $name == '' ? $emptyCount + 1 : $emptyCount;
237 } //if-else if-else block
241 // One last final check. If $emptyCount does not equal Array $col count, don't add
242 if($emptyCount != count($col)) {
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");
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');