]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Parsers/DetailViewMetaParser.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarFields / Parsers / DetailViewMetaParser.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  * DetailViewMetaParser.php
41  * This is a utility file that attempts to provide support for parsing pre 5.0 SugarCRM 
42  * DetailView.html files and produce a best guess detailviewdefs.php file equivalent.
43  * 
44  * @author Collin Lee
45  */
46 require_once('include/SugarFields/Parsers/MetaParser.php');
47
48 /**
49  * DetailViewMetaParser.php
50  * This class is responsible for handling the parsing of DetailView.html files from
51  * SugarCRM versions prior to 5.x.  It will make a best guess of translating the
52  * HTML file to a metadata format.
53  * 
54  * @author Collin Lee
55  */
56 class DetailViewMetaParser extends MetaParser {
57
58 function DetailViewMetaParser() {
59    $this->mView = 'DetailView';
60 }
61
62
63 /**
64  * parse
65  * 
66  * @param $filePath The file path of the HTML file to parse
67  * @param $vardefs The module's vardefs
68  * @param $moduleDir The module's directory
69  * @param $merge boolean value indicating whether or not to merge the parsed contents
70  * @param $masterCopy The file path of the mater copy of the metadata file to merge against
71  * @return String format of metadata contents
72  **/
73 function parse($filePath, $vardefs = array(), $moduleDir = '', $merge=false, $masterCopy=null) {
74    
75 // Grab file contents
76 $contents = file_get_contents($filePath);
77    
78 // Remove \n,\r characters to allow for better text parsing
79 $contents = $this->trimHTML($contents);
80 $contents = $this->stripFlavorTags($contents);
81
82
83 // Notes DetailView.html file is messed up
84 if($moduleDir == 'Notes') {
85    $contents = str_replace('{PAGINATION}<tr><td>', '{PAGINATION}', $contents); 
86    $contents = str_replace('</td></tr></table><script>', '</table><script>', $contents);
87    $contents = str_replace("<tr><div id='portal_flag_row' name='portal_flag_row' style='display:none'>", "<div id='portal_flag_row' name='portal_flag_row' style='display:none'>", $contents); 
88 }
89
90 $contents = $this->fixDuplicateTrTags($contents);
91 $contents = $this->fixRowsWithMissingTr($contents);
92
93 // Get all the tables
94 $tables = $this->getElementsByType("table", $contents);
95    
96 // Skip the first one
97 $tables = array_slice($tables, 1);
98
99 $panels = array();
100 $tableCount = 0;
101 $metarow = array();
102 foreach($tables as $table) {   
103    
104    $table = $this->fixTablesWithMissingTr($table);
105    $tablerows = $this->getElementsByType("tr", $table);
106
107    foreach($tablerows as $trow) {
108
109        $metacolumns = array();  
110            $columns = $this->getElementsByType("td", $trow);
111        $columns = array_reverse($columns, true);
112            foreach($columns as $tcols) {
113
114                   $sugarAttrValue = $this->getTagAttribute("sugar", $tcols, "'slot[0-9]+b$'");
115                   if(empty($sugarAttrValue)) {
116                          continue;
117                   }             
118                 
119           $def = '';
120                   $field = $this->getElementValue("span", $tcols);
121                   //If it's a space, simply add a blank string            
122                   if($field == '&nbsp;') {
123                          $metacolumns[] = "";
124                   } else if(!empty($field)) {
125                         
126                  preg_match_all('/[\{]([^\}].*?)[\}]/s', $field, $matches, PREG_SET_ORDER);
127                  if(!empty($matches)) {         
128                         if(count($matches) > 1) {
129                                   
130                                   $def = array();
131
132                                   $def['name'] = preg_match('/_c$/i', $matches[0][1]) ? $matches[0][1] : strtolower($matches[0][1]);
133                       foreach($matches as $m) {
134                          if(isset($vardefs[strtolower($m[1])])) {
135                                 $def['name'] = strtolower($m[1]);
136                          }
137                       }
138
139                                   $field = preg_replace('/<\{tag\.[a-z_]*?\}/i', '<a', $field);
140                                   $field = preg_replace('/<\/\{tag\.[a-z_]*?\}>/i', '</a>', $field);
141
142                                   foreach($matches as $tag[1]) {
143                                             if(preg_match("/^(mod[\.]|app[\.]).*?/i", $tag[1][1])) {
144                                                $field = str_replace($tag[1][1], '$'.$tag[1][1], $field);      
145                                             } else {
146                                                $theField = preg_match('/_c$/i', $tag[1][1]) ? $tag[1][1] : strtolower($tag[1][1]);
147                                                if(!empty($vardefs[$theField])) {
148                                                   $field = str_replace($tag[1][1], '$fields.'. $theField.'.value', $field);
149                                                } else {
150                                                   $phpName = $this->findAssignedVariableName($tag[1][1], $filePath);
151                                                   $field = str_replace($tag[1][1], '$fields.'. $theField.'.value', $field);
152                                                } //if-else
153                                             }
154                                           }
155
156                                           $def['customCode'] = $field;
157                                           $def['description'] = 'This field was auto generated';
158                         } else {
159                                   $def = strtolower($matches[0][1]);
160                         }
161                  } //if
162              $metacolumns[] = $def;
163           } //if
164            } //foreach($tablecolumns as $tcols)
165
166            $metarow[] = array_reverse($metacolumns);
167    } //foreach($tablerows as $trow) 
168    
169    
170    $id = $tableCount == 0 ? 'default' : $tableCount;
171    $tableCount++;
172    $panels[$id] = $metarow;
173    
174 } //foreach($tables as $table)
175
176 $this->mCustomPanels = $panels;
177 $panels = $this->applyPreRules($moduleDir, $panels);
178
179 $templateMeta = array();
180 if($merge && !empty($masterCopy) && file_exists($masterCopy)) {
181    $panels = $this->mergePanels($panels, $vardefs, $moduleDir, $masterCopy);
182    $templateMeta = $this->mergeTemplateMeta($templateMeta, $moduleDir, $masterCopy);
183 }
184
185 $panels = $this->applyRules($moduleDir, $panels);
186 return $this->createFileContents($moduleDir, $panels, $templateMeta, $filePath);
187
188 }
189
190 }
191 ?>
192