]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Parsers/QuickCreateMetaParser.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarFields / Parsers / QuickCreateMetaParser.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  * EdtiViewMetaParser.php
41  * This is a utility file that attempts to provide support for parsing pre 5.0 SugarCRM 
42  * QuickCreate.html files and produce a best guess editviewdefs.php file equivalent.
43  * 
44  * @author Collin Lee
45  */
46   
47 require_once('include/SugarFields/Parsers/MetaParser.php');
48
49 class QuickCreateMetaParser extends MetaParser {
50
51 function QuickCreateMetaParser() {
52    $this->mView = 'QuickCreate';
53 }
54
55 /**
56  * parse
57  * 
58  * @param $filePath The file path of the HTML file to parse
59  * @param $vardefs The module's vardefs
60  * @param $moduleDir The module's directory
61  * @param $merge boolean value indicating whether or not to merge the parsed contents
62  * @param $masterCopy The file path of the mater copy of the metadata file to merge against
63  * @return String format of metadata contents
64  **/
65 function parse($filePath, $vardefs = array(), $moduleDir = '', $merge=false, $masterCopy=null) {
66
67    global $app_strings;
68    $contents = file_get_contents($filePath);
69    
70    // The contents are not well formed so we add this section to make it easier to parse
71    $contents = $this->trimHTML($contents) . '</td></tr></table>';
72    $moduleName = '';
73    
74    $forms = $this->getElementsByType("form", $contents);
75    $tables = $this->getElementsByType("table", $forms[0] . "</td></tr></table>");
76    $mainrow = $this->getElementsByType("tr", $tables[1]);
77    $rows = substr($mainrow[0], strpos($mainrow[0], "</tr>"));
78    $tablerows = $this->getElementsByType("tr", $rows);
79
80    foreach($tablerows as $trow) {
81         
82            $emptyCount = 0;
83            $tablecolumns = $this->getElementsByType("td", $trow);
84        $col = array();
85        $slot = 0;
86        
87            foreach($tablecolumns as $tcols) {
88                                   
89                   $sugarAttrLabel = $this->getTagAttribute("sugar", $tcols, "'^slot[^b]+$'");
90                   $sugarAttrValue = $this->getTagAttribute("sugar", $tcols, "'slot[0-9]+b$'");
91
92           // If there wasn't any slot numbering/lettering then just default to expect label->vallue pairs
93           $sugarAttrLabel = count($sugarAttrLabel) != 0 ? $sugarAttrLabel : ($slot % 2 == 0) ? true : false;
94           $sugarAttrValue = count($sugarAttrValue) != 0 ? $sugarAttrValue : ($slot % 2 == 1) ? true : false;
95           
96           $slot++;
97           
98           if($sugarAttrValue) {
99                          
100                          $spanValue = strtolower($this->getElementValue("span", $tcols));
101              if(empty($spanValue)) {
102                 $spanValue = strtolower($this->getElementValue("slot", $tcols));        
103              }
104              if(empty($spanValue)) {
105                 $spanValue = strtolower($this->getElementValue("td", $tcols));
106              }
107              
108              //Get all the editable form elements' names
109                          $formElementNames = $this->getFormElementsNames($spanValue);                            
110                          $customField = $this->getCustomField($formElementNames);
111                          
112                          $name = '';
113              $readOnly = false;
114              $fields = null;
115              $customCode = null;
116                         
117              if(!empty($customField)) {           
118                // If it's a custom field we just set the name
119                $name = $customField;
120                      
121              } else if(empty($formElementNames) && preg_match_all('/[\{]([^\}]*?)[\}]/s', $spanValue, $matches, PREG_SET_ORDER)) {
122                            // We are here if the $spanValue did not contain a form element for editing.
123                            // We will assume that it is read only (since there were no edit form elements)
124                            
125
126                    // If there is more than one matching {} value then try to find the right one to key off
127                    // based on vardefs.php file.  Also, use the entire spanValue as customCode
128                         if(count($matches) > 1) {
129                                $name = $matches[0][1];  
130                                $customCode = $spanValue;
131                                foreach($matches as $pair) {
132                                            if(preg_match("/^(mod[\.]|app[\.]).*?/s", $pair[1])) {
133                                                $customCode = str_replace($pair[1], '$'.strtoupper($pair[1]), $customCode);      
134                                            } else if(!empty($vardefs[$pair[1]])) {
135                                                $name = $pair[1];
136                                                $customCode = str_replace($pair[1], '$fields.'.$pair[1].'.value', $customCode);
137                                            }
138                            } //foreach
139                        } else {
140                            //If it is only a label, skip
141                            if(preg_match("/^(mod[\.]|app[\.]).*?/s", $matches[0][1])) {
142                                   continue;
143                            } else if(preg_match("/^[\$].*?/s", $matches[0][1])) {
144                                   $name = '{' . strtoupper($matches[0][1]) . '}';  
145                            } else {         
146                                           $name = $matches[0][1];
147                            }   
148                            }
149                                    
150                            $readOnly = true;  
151                          } else if(is_array($formElementNames)) {
152                   
153                               if(count($formElementNames) == 1) {
154                                  if(!empty($vardefs[$formElementNames[0]])) {
155                                     $name = $formElementNames[0];
156                                  } 
157                               } else {
158                                  $fields = array();
159                                  foreach($formElementNames as $elementName) {
160                                         // What we are doing here is saying that we will add all your fields assuming
161                                         // there are none that are of type relate or link.  However, if we find such a type
162                                         // we'll take the first one found and assume that is the field you want (the SugarFields
163                                         // library will handle rendering the popup and select and clear buttons for you).
164                                         if(isset($vardefs[$elementName])) {
165                                            $type = $vardefs[$elementName]['type'];
166                                            if($type != 'relate' && $type != 'link') {
167                                               $fields[] = $elementName;
168                                               $name = $elementName;
169                                            } else {
170                                           unset($fields);
171                                               $name = $elementName;
172                                               break;    
173                                            }
174                                         }
175                                  }
176                               } //if-else
177                          }
178                          
179                          // Build the entry
180                          if(preg_match("/<textarea/si", $spanValue)) {
181                                 //special case for textarea form elements (add the displayParams)
182                                 $displayParams = array();
183                             $displayParams['rows'] = $this->getTagAttribute("rows", $spanValue);
184                             $displayParams['cols'] = $this->getTagAttribute("cols", $spanValue);
185
186                             if(!empty($displayParams['rows']) && !empty($displayParams['cols'])) {
187                                     $field = array();
188                                     $field['name'] = $name;
189                                         $field['displayParams'] = $displayParams;
190                             } else {
191                                 $field = $name; 
192                             }
193                             $col[] = $field;
194                          } else if($readOnly) {
195                                 $field = array();
196                                 $field['name'] = $name;
197                                 $field['type'] = 'readonly';
198                                 if(isset($customCode)) {
199                                    $field['customCode'] = $customCode;
200                                 } //if
201                                 $col[] = $field;
202                          } else {
203
204                                 if(isset($fields) || isset($customCode)) {
205                                    $field = array();
206                                    $field['name'] = $name;
207                                    if(isset($fields)) {
208                                           $field['fields'] = $fields;
209                                    }
210                                    if(isset($customCode)) {
211                                           $field['customCode'] = $customCode;
212                                    }
213                    
214                                    $col[] = $field;
215                                 } else {
216                                    $emptyCount = $name == '' ? $emptyCount + 1 : $emptyCount;
217                                    $col[] = $name;
218                                 }       
219                          } //if-else if-else block
220                   } //if($sugarAttrValue)
221            } //foreach
222            
223            // One last final check.  If $emptyCount does not equal Array $col count, don't add 
224            if($emptyCount != count($col)) {
225               $metarow[] = $col;
226            } //if
227    } //foreach
228    
229    $templateMeta = array();
230    $templateMeta['form']['buttons'] = 'button';
231
232    preg_match_all("/(<input[^>]*?)>/si", $tables[0], $matches);
233    $buttons = array();
234    foreach($matches[0] as $button) {
235                 $buttons[] = array('customCode'=>$button);         
236    }
237    $templateMeta['form']['buttons'] = $buttons;
238    
239    $formElements = $this->getFormElements($contents);
240    $hiddenInputs = array();
241    foreach($formElements as $elem) {
242               $type = $this->getTagAttribute("type", $elem);
243               if(preg_match('/hidden/si',$type, $matches)) {
244                  $name = $this->getTagAttribute("name", $elem);
245                  $value = $this->getTagAttribute("value", $elem);
246                  $index = stripos($value, '$REQUEST');
247                  $value =  !empty($index) ? '$smarty.request.' . substr($value, 10) : $value;
248                  $hiddenInputs[] = '<input id="' . $name . '" name="' . $name . '" value="' . $value . '">';
249               } 
250    } //foreach
251    
252    $templateMeta['form']['hidden'] = $hiddenInputs;
253    $templateMeta['widths'] = array(array('label' => '10', 'field' => '30'),  array('label' => '10', 'field' => '30'));
254    $templateMeta['maxColumns'] = 2;
255    
256    $panels = array();
257    $panels['default'] = $metarow;
258    $panels = $this->appplyRules($moduleDir, $panels);
259    return $this->createFileContents($moduleDir, $panels, $templateMeta, $filePath);
260    
261
262 }
263
264
265 }
266 ?>