]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Studio/parsers/StudioParser.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Studio / parsers / StudioParser.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
41
42
43 /**
44  * interface for studio parsers
45  */
46
47 class StudioParser {
48         var $positions = array ();
49         var $rows = array ();
50         var $cols = array ();
51         var $curFile = '';
52         var $curText = '';
53         var $form;
54         var $labelEditor = true;
55         var $curType = 'detail';
56         var $fieldEditor = true;
57         var $oldMatches = array();
58
59         function getFileType($type, $setType=true){
60                 switch($type){
61                         case 'EditView':$type = 'edit'; break;
62                         case 'SearchForm': $type= 'search';break;
63                         case 'ListView': $type= 'list';break;
64                         default: $type= 'detail';
65                 }
66
67                 if($setType){
68                         $this->curType = $type;
69                 }
70                 return $type;
71         }
72
73         function getParsers($file){
74                 if(substr_count($file, 'DetailView.html') > 0 || substr_count($file, 'EditView.html' ) > 0) return array('default'=>'StudioParser', array('StudioParser', 'StudioRowParser'));
75                 if(substr_count($file, 'ListView.html' ) > 0) return array('default'=>'XTPLListViewParser', array('XTPLListViewParser'));
76                 return array('default'=>'StudioParser', array('StudioParser'));
77         }
78
79
80         function parseRows($str){
81                 preg_match_all("'(<tr[^>]*)>(.*?)(</tr[^>]*>)'si", $str, $this->rows,PREG_SET_ORDER);
82
83         }
84
85         function parseNames($str){
86                 $results = array();
87                 preg_match_all("'name[ ]*=[ ]*[\'\"]+([a-zA-Z0-9\_]+)[\'\"]+'si", $str, $results,PREG_SET_ORDER);
88                 return $results;
89         }
90
91         function parseLabels($str){
92                 $mod = array();
93                 $app = array();
94                 preg_match_all("'\{MOD\.([a-zA-Z0-9\_]+)\}'si", $str, $mod,PREG_SET_ORDER);
95                 preg_match_all("'\{APP\.([a-zA-Z0-9\_]+)\}'si", $str, $app,PREG_SET_ORDER);
96                 return array_merge($app, $mod);
97         }
98
99         function getMaxPosition(){
100                 $max = 0;
101                 for($i = 0; $i < count($this->positions) ; $i++){
102                         if($this->positions[$i][2] >= $max){
103                                 $max = $this->positions[$i][2] + 1;
104                         }
105                 }
106                 return $max;
107         }
108         function parsePositions($str, $output= false) {
109                 $results = array();
110                 preg_match_all("'<span[^>]*sugar=[\'\"]+([a-zA-Z\_]*)([0-9]+)([b]*)[\'\"]+[^>]*>(.*?)</span[ ]*sugar=[\'\"]+[a-zA-Z0-9\_]*[\'\"]+>'si", $str, $results, PREG_SET_ORDER);
111                 if($output){
112                         return $results;
113                 }
114                 $this->positions = $results;
115         }
116         function parseCols($str){
117                 preg_match_all("'(<td[^>]*?)>(.*?)(</td[^>]*?>)'si", $str, $this->cols,PREG_SET_ORDER);
118
119         }
120         function parse($str){
121                 $this->parsePositions($str);
122         }
123         function positionCount($str) {
124                 $result = array ();
125                 return preg_match_all("'<span[^>]*sugar=[\'\"]+([a-zA-Z\_]*)([0-9]+)([b]*)[\'\"]+[^>]*>(.*?)</span[ ]*sugar=[\'\"]+[a-zA-Z0-9\_]*[\'\"]+>'si", $str, $result, PREG_SET_ORDER)/2;
126         }
127         function rowCount($str) {
128                 $result = array ();
129                 return preg_match_all("'(<tr[^>]*>)(.*?)(</tr[^>]*>)'si", $str, $result);
130         }
131
132         function loadFile($file) {
133                 $this->curFile = $file;
134                 $this->curText = file_get_contents($file);
135                 $this->form = <<<EOQ
136                 </form>
137                 <form name='studio'  method='POST'>
138                         <input type='hidden' name='action' value='save'>
139                         <input type='hidden' name='module' value='Studio'>
140
141 EOQ;
142
143         }
144         function buildImageButtons($buttons,$horizontal=true){
145                 $text = '<table cellspacing=2><tr>';
146                 foreach($buttons as $button){
147                         if(!$horizontal){
148                                 $text .= '</tr><tr>';
149                         }
150                         if(!empty($button['plain'])){
151                                 $text .= <<<EOQ
152                                 <td valign='center' {$button['actionScript']}>
153 EOQ;
154
155                         }else{
156
157
158                                 $text .= <<<EOQ
159                                 <td valign='center' class='button' style='cursor:default' onmousedown='this.className="buttonOn";return false;' onmouseup='this.className="button"' onmouseout='this.className="button"' {$button['actionScript']} >
160 EOQ;
161                         }
162             if ( !isset($button['image']) )
163                 $text .= "{$button['text']}</td>";
164             else
165                             $text .= "{$button['image']}&nbsp;{$button['text']}</td>";
166                 }
167                 $text .= '</tr></table>';
168                 return $text;
169         }
170
171         function generateButtons(){
172
173         global $mod_strings;
174                 $imageSave = SugarThemeRegistry::current()->getImage( 'studio_save', '',null,null,'.gif',$mod_strings['LBL_SAVE']);
175                 $imagePublish = SugarThemeRegistry::current()->getImage( 'studio_publish', '',null,null,'.gif',$mod_strings['LBL_PUBLISH']);
176                 $imageHistory = SugarThemeRegistry::current()->getImage( 'studio_history', '',null,null,'.gif',$mod_strings['LBL_HISTORY']);
177                 $imageAddRows = SugarThemeRegistry::current()->getImage('studio_addRows', '',null,null,'.gif',$mod_strings['LBL_ADDROWS']);
178                 $imageUndo = SugarThemeRegistry::current()->getImage('studio_undo', '',null,null,'.gif',$mod_strings['LBL_UNDO']);
179                 $imageRedo = SugarThemeRegistry::current()->getImage('studio_redo', '',null,null,'.gif',$mod_strings['LBL_REDO']);
180                 $imageAddField = SugarThemeRegistry::current()->getImage( 'studio_addField', '',null,null,'.gif',$mod_strings['LBL_ADDFIELD']);
181                 $buttons = array();
182
183                 $buttons[] = array('image'=>$imageUndo,'text'=>$GLOBALS['mod_strings']['LBL_BTN_UNDO'],'actionScript'=>"onclick='jstransaction.undo()'" );
184                 $buttons[] = array('image'=>$imageRedo,'text'=>$GLOBALS['mod_strings']['LBL_BTN_REDO'],'actionScript'=>"onclick='jstransaction.redo()'" );
185                 $buttons[] = array('image'=>$imageAddField,'text'=>$GLOBALS['mod_strings']['LBL_BTN_ADDCUSTOMFIELD'],'actionScript'=>"onclick='studiopopup.display();return false;'" );
186                 $buttons[] = array('image'=>$imageAddRows,'text'=>$GLOBALS['mod_strings']['LBL_BTN_ADDROWS'],'actionScript'=>"onclick='if(!confirmNoSave())return false;document.location.href=\"index.php?module=Studio&action=EditLayout&parser=StudioRowParser\"'" ,);
187                 $buttons[] = array('image'=>$imageAddRows,'text'=>$GLOBALS['mod_strings']['LBL_BTN_TABINDEX'],'actionScript'=>"onclick='if(!confirmNoSave())return false;document.location.href=\"index.php?module=Studio&action=EditLayout&parser=TabIndexParser\"'" ,);
188                 $buttons[] = array('image'=>'', 'text'=>'-', 'actionScript'=>'', 'plain'=>true);
189
190                 $buttons[] = array('image'=>$imageSave,'text'=>$GLOBALS['mod_strings']['LBL_BTN_SAVE'],'actionScript'=>"onclick='studiojs.save(\"studio\", false);'");
191                 $buttons[] = array('image'=>$imagePublish,'text'=>$GLOBALS['mod_strings']['LBL_BTN_SAVEPUBLISH'],'actionScript'=>"onclick='studiojs.save(\"studio\", true);'");
192                 $buttons[] = array('image'=>$imageHistory,'text'=>$GLOBALS['mod_strings']['LBL_BTN_HISTORY'],'actionScript'=>"onclick='if(!confirmNoSave())return false;document.location.href=\"index.php?module=Studio&action=wizard&wizard=ManageBackups&setFile={$_SESSION['studio']['selectedFileId']}\"'");
193                 return $buttons;
194         }
195         function getFormButtons(){
196                 $buttons = $this->generateButtons();
197                 return $this->buildImageButtons($buttons);
198         }
199         function getForm(){
200                 return $this->form  . <<<EOQ
201                 </form>
202
203
204 EOQ;
205
206         }
207
208
209
210         function getFiles($module, $fileId=false){
211                 if(empty($GLOBALS['studioDefs'][$module])){
212                         require_once('modules/'. $module . '/metadata/studio.php');
213                 }
214                 if($fileId){
215                         return  $GLOBALS['studioDefs'][$module][$fileId];
216                 }
217                 return $GLOBALS['studioDefs'][$module];
218         }
219
220
221         function getWorkingFile($file, $refresh = false){
222                 $workingFile = 'working/' . $file;
223                 $customFile = create_custom_directory($workingFile);
224                 if($refresh || !file_exists($customFile)){
225                         copy($file, $customFile);
226                 }
227                 return $customFile;
228         }
229
230         function getSwapWith($value){
231                 return $value * 2 - 1;
232         }
233         /**
234          * takes the submited form and parses the file moving the fields around accordingly
235          * it also checks if the original file has a matching field and uses that field instead of attempting to generate a new one
236          */
237         function handleSave() {
238                 $this->parseOldestFile($this->curFile);
239                 $fileDef = $this->getFiles($_SESSION['studio']['module'], $_SESSION['studio']['selectedFileId']);
240                 $type = $this->getFileType($fileDef['type']);
241                 $view = $this->curText;
242                 $counter = 0;
243                 $return_view = '';
244                 $slotCount = 0;
245                 $slotLookup = array();
246                 for ($i = 0; $i < sizeof($this->positions); $i ++) {
247                         //used for reverse lookups to figure out where the associated slot is
248                         $slotLookup[$this->positions[$i][2]][$this->positions[$i][3]] = array('position'=>$i, 'value'=>$this->positions[$i][4]);
249                 }
250
251                 $customFields = $this->focus->custom_fields->getAllBeanFieldsView($type, 'html');
252
253                 //now we set it to the new values
254
255                 for ($i = 0; $i < sizeof($this->positions); $i ++) {
256                         $slot = $this->positions[$i];
257
258                         if (empty($slot[3])) {
259                                 $slotCount ++;
260
261                                 //if the value in the request doesn't equal our current slot then something should be done
262                                 if(isset($_REQUEST['slot_'.$slotCount]) && $_REQUEST['slot_'.$slotCount] != $slotCount){
263
264                                         $swapValue = $_REQUEST['slot_'.$slotCount] ;
265                                         //if its an int then its a simple swap
266                                         if(is_numeric($swapValue)){
267
268                                                 $swapWith = $this->positions[$this->getSwapWith($swapValue)];
269
270                                                 //label
271                                                 $slotLookup[$slot[2]]['']['value'] = $this->positions[ $slotLookup[$swapWith[2]]['']['position']][4];
272                                                 //html
273                                                 $slotLookup[$slot[2]]['b']['value'] = $this->positions[ $slotLookup[$swapWith[2]]['b']['position']][4];
274                                         }
275                                         //now check if its a delete action
276                                         if(strcmp('add:delete', $swapValue) == 0){
277                                                 //label
278                                                 $slotLookup[$slot[2]][$slot[3]]['value'] = '&nbsp;';
279                                                 //html
280                                                 $slotLookup[$slot[2]]['b']['value'] = '&nbsp;';
281                                         }else{
282
283                                                 //now handle the adding of custom fields
284                                                 if(substr_count($swapValue, 'add:')){
285                                                         $addfield = explode('add:', $_REQUEST['slot_'.$slotCount], 2);
286
287                                                         //label
288                                                         $slotLookup[$slot[2]][$slot[3]]['value'] = $customFields[$addfield[1]]['label'] ;
289                                                         //html
290                                                         if(!empty($this->oldMatches[$addfield[1]])){
291                                                                 //we have an exact match from the original file use that
292                                                                 $slotLookup[$slot[2]]['b']['value'] = $this->oldMatches[$addfield[1]];
293                                                         }else{
294                                                                 if(!empty($this->oldLabels[$customFields[$addfield[1]]['label']])){
295                                                                         //we have matched the label from the original file use that
296                                                                         $slotLookup[$slot[2]]['b']['value'] = $this->oldLabels[$customFields[$addfield[1]]['label']];
297                                                                 }else{
298                                                                         //no matches so use what we are generating
299                                                                         $slotLookup[$slot[2]]['b']['value'] = $customFields[$addfield[1]]['html'];
300                                                                 }
301
302                                                         }
303
304                                                 }
305                                         }
306                                 }
307                         }
308                 }
309
310                 for ($i = 0; $i < sizeof($this->positions); $i ++) {
311                         $slot = $this->positions[$i];
312                         $explode = explode($slot[0], $view, 2);
313                         $explode[0] .= "<span sugar='". $slot[1] . $slot[2]. $slot[3]. "'>";
314                         $explode[1] = "</span sugar='" .$slot[1] ."'>".$explode[1];
315
316                         $return_view .= $explode[0].$slotLookup[$slot[2]][$slot[3]]['value'];
317                         $view = $explode[1];
318                         $counter ++;
319                 }
320                 $return_view .= $view;
321
322                 $this->saveFile('', $return_view);
323                 return $return_view;
324         }
325
326         function saveFile($file = '', $contents = false) {
327                 if (empty ($file)) {
328                         $file = $this->curFile;
329                 }
330
331                 $fp = sugar_fopen($file, 'w');
332                 $output = $contents ? $contents : $this->curText;
333                 if(strpos($file, 'SearchForm.html') > 0) {
334                         $fileparts = preg_split("'<!--\s*(BEGIN|END)\s*:\s*main\s*-->'", $output);
335                         if(!empty($fileparts) && count($fileparts) > 1) {
336                                 //preg_replace_callback doesn't seem to work w/o anonymous method
337                                 $output = preg_replace_callback("/name\s*=\s*[\"']([^\"']*)[\"']/Us",
338                                 create_function(
339                                                              '$matches',
340                                                              '$name = str_replace(array("[", "]"), "", $matches[1]);
341                                                                                                                   if((strpos($name, "LBL_") === 0) && (strpos($name, "_basic") === 0)) {
342                                                                   return str_replace($name, $name . "_basic", $matches[0]);
343                                                               }
344                                                               return  $matches[0];'
345                                                               ),
346                                                               $fileparts[1]);
347
348
349
350                                                               $output = $fileparts[0] . '<!-- BEGIN:main -->' . $output . '<!-- END:main -->' . $fileparts[2];
351                         }
352                 }
353
354                 fwrite($fp, $output);
355                 fclose($fp);
356         }
357
358         function handleSaveLabels($module_name, $language){
359                 require_once('modules/Studio/LabelEditor/LabelEditor.php');
360                 LabelEditor::saveLabels($_REQUEST, $module_name, $language);
361         }
362
363         /**
364          * UTIL FUNCTIONS
365          */
366         /**
367          * STATIC FUNCTION DISABLE INPUTS IN AN HTML STRING
368          *
369          */
370         function disableInputs($str) {
371                 $match = array ("'(<input)([^>]*>)'si" => "\$1 disabled readonly $2",
372     "'(<input)([^>]*?type[ ]*=[ ]*[\'\"]submit[\'\"])([^>]*>)'si" => "\$1 disabled readonly style=\"display:none\" $2",
373      "'(<select)([^>]*)'si" => "\$1 disabled readonly $2",
374                 // "'<a .*>(.*)</a[^>]*>'siU"=>"\$1",
375 "'(href[\ ]*=[\ ]*)([\'])([^\']*)([\'])'si" => "href=\$2javascript:void(0);\$2 alt=\$2\$3\$2", "'(href[\ ]*=[\ ]*)([\"])([^\"]*)([\"])'si" => "href=\$2javascript:void(0)\$2 title=\$2\$3\$2");
376                 return preg_replace(array_keys($match), array_values($match), $str);
377         }
378
379         function enableLabelEditor($str) {
380         global $mod_strings;
381                 $image = SugarThemeRegistry::current()->getImage( 'edit_inline', "onclick='studiojs.handleLabelClick(\"$2\", 1);' onmouseover='this.style.cursor=\"default\"'",null,null,'.gif',$mod_strings['LBL_EDIT']);
382                 $match = array ("'>[^<]*\{(MOD.)([^\}]*)\}'si" => "$image<span id='label$2' onclick='studiojs.handleLabelClick(\"$2\", 2);' >{".'$1$2' . "}</span><span id='span$2' style='display:none'><input type='text' id='$2' name='$2' msi='label' value='{".'$1$2' . "}' onblur='studiojs.endLabelEdit(\"$2\")'></span>");
383                 $keys = array_keys($match);
384                 $matches = array();
385                 preg_match_all($keys[0], $str, $matches, PREG_SET_ORDER);
386                 foreach($matches as $labelmatch){
387                         $label_name = 'label_' . $labelmatch[2];
388                         $this->form .= "\n<input type='hidden' name='$label_name'  id='$label_name' value='no_change'>";
389
390                 }
391                 return preg_replace(array_keys($match), array_values($match), $str);
392         }
393
394
395
396         function writeToCache($file, $view, $preview_file=false) {
397                 if (!is_writable($file)) {
398                         echo "<br><span style='color:red'>Warning: $file is not writeable. Please make sure it is writeable before continuing</span><br><br>";
399                 }
400
401                 if(!$preview_file){
402                         $file_cache = create_cache_directory('studio/'.$file);
403                 }else{
404                         $file_cache = create_cache_directory('studio/'.$preview_file);
405                 }
406                 $fp = sugar_fopen($file_cache, 'w');
407                 $view = $this->disableInputs($view);
408                 if(!$preview_file){
409                         $view = $this->enableLabelEditor($view);
410                 }
411                 fwrite($fp, $view);
412                 fclose($fp);
413                 return $this->cacheXTPL($file, $file_cache, $preview_file);
414         }
415
416         function populateRequestFromBuffer($file) {
417                 $results = array ();
418                 $temp = sugar_file_get_contents($file);
419                 preg_match_all("'name[\ ]*=[\ ]*[\']([^\']*)\''si", $buffer, $results);
420                 $res = $results[1];
421                 foreach ($res as $r) {
422                         $_REQUEST[$r] = $r;
423                 }
424                 preg_match_all("'name[\ ]*=[\ ]*[\"]([^\"]*)\"'si", $buffer, $results);
425                 $res = $results[1];
426                 foreach ($res as $r) {
427                         $_REQUEST[$r] = $r;
428                 }
429
430                 $_REQUEST['query'] = true;
431                 $_REQUEST['advanced'] = true;
432
433         }
434         function cacheXTPL($file, $cache_file, $preview_file = false) {
435                 global $beanList;
436                 //now if we have a backup_file lets use that instead of the original
437                 if($preview_file){
438                         $file  = $preview_file;
439                 }
440
441
442                 if(!isset($the_module))$the_module = $_SESSION['studio']['module'];
443                 $files = StudioParser::getFiles($the_module);
444                 $xtpl = $files[$_SESSION['studio']['selectedFileId']]['php_file'];
445                 $originalFile = $files[$_SESSION['studio']['selectedFileId']]['template_file'];
446                 $type = StudioParser::getFileType($files[$_SESSION['studio']['selectedFileId']]['type']);
447                 $buffer = sugar_file_get_contents($xtpl);
448                 $cache_file = create_cache_directory('studio/'.$file);
449                 $xtpl_cache = create_cache_directory('studio/'.$xtpl);
450                 $module = $this->workingModule;
451
452                 $form_string = "require_once('modules/".$module."/Forms.php');";
453
454                 if ($type == 'edit' || $type == 'detail') {
455                         if (empty ($_REQUEST['record'])) {
456                                 $buffer = preg_replace('(\$xtpl[\ ]*=)', "\$focus->assign_display_fields('$module'); \$0", $buffer);
457                         } else {
458                                 $buffer = preg_replace('(\$xtpl[\ ]*=)', "\$focus->retrieve('".$_REQUEST['record']."');\n\$focus->assign_display_fields('$module');\n \$0", $buffer);
459                         }
460                 }
461                 $_REQUEST['query'] = true;
462                 if (substr_count($file, 'SearchForm') > 0) {
463                         $temp_xtpl = new XTemplate($file);
464                         if ($temp_xtpl->exists('advanced')) {
465
466                                 global $current_language, $beanFiles, $beanList;
467                                 $mods = return_module_language($current_language, 'DynamicLayout');
468                                 $class_name = $beanList[$module];
469                                 require_once ($beanFiles[$class_name]);
470                                 $mod = new $class_name ();
471
472                                 $this->populateRequestFromBuffer($file);
473                                 $mod->assign_display_fields($module);
474                                 $buffer = str_replace(array ('echo $lv->display();','$search_form->parse("advanced");', '$search_form->out("advanced");', '$search_form->parse("main");', '$search_form->out("main");'), '', $buffer);
475                                 $buffer = str_replace('echo get_form_footer();', '$search_form->parse("main");'."\n".'$search_form->out("main");'."\necho '<br><b>".translate('LBL_ADVANCED', 'DynamicLayout')."</b><br>';".'$search_form->parse("advanced");'."\n".'$search_form->out("advanced");'."\n \$sugar_config['list_max_entries_per_page'] = 1;", $buffer);
476                         }
477                 }else{
478
479                         if ($type == 'detail') {
480                                 $buffer = str_replace('header(', 'if(false) header(', $buffer);
481                         }
482                 }
483
484                 $buffer = str_replace($originalFile, $cache_file, $buffer);
485                 $buffer = "<?php\n\$sugar_config['list_max_entries_per_page'] = 1;\n ?>".$buffer;
486
487                 $buffer = str_replace($form_string, '', $buffer);
488                 $buffer = $this->disableInputs($buffer);
489                 $xtpl_fp_cache = sugar_fopen($xtpl_cache, 'w');
490                 fwrite($xtpl_fp_cache, $buffer);
491                 fclose($xtpl_fp_cache);
492                 return $xtpl_cache;
493         }
494
495         /**
496          * Yahoo Drag & Drop Support
497          */
498         ////<script type="text/javascript" src="modules/Studio/studio.js" ></script>
499         function yahooJS() {
500                 $custom_module = $_SESSION['studio']['module'];
501                 $custom_type = $this->curType;
502                 $v = getVersionedPath('');
503                 return<<<EOQ
504                 <style type='text/css'>
505                 .slot {
506                 border-width:1px;border-color:#999999;border-style:solid;padding:0px 1px 0px 1px;margin:2px;cursor:move;
507
508         }
509
510         .slotB {
511         border-width:0;cursor:move;
512
513         }
514         </style>
515
516         <!-- Namespace source file -->
517
518         <script type="text/javascript" src="modules/Studio/JSTransaction.js?v=$v" ></script>
519         <script>
520         var jstransaction = new JSTransaction();
521         </script>
522
523         <!-- Drag and Drop source file -->
524         <script type="text/javascript" src="include/javascript/yui/build/dragdrop/dragdrop.js?v=$v" ></script>
525         <script type="text/javascript" src="modules/Studio/studiodd.js?v=$v" ></script>
526         <script type="text/javascript" src="modules/Studio/studio.js?v=$v" ></script>
527         <script>
528
529         var yahooSlots = [];
530
531         function dragDropInit(){
532
533         YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
534
535         for(mj = 0; mj <= $this->yahooSlotCount; mj++){
536         yahooSlots["slot" + mj] = new ygDDSlot("slot" + mj, "studio");
537         }
538         for(mj = 0; mj < dyn_field_count; mj++){
539         yahooSlots["dyn_field_" + mj] = new ygDDSlot("dyn_field_" + mj, "studio");
540         }
541         // initPointMode();
542         yahooSlots['s_field_delete'] =  new YAHOO.util.DDTarget("s_field_delete", 'studio');
543         }
544
545         YAHOO.util.Event.addListener(window, "load", dragDropInit);
546         var custom_module = '$custom_module';
547         var custom_view = '$custom_type';
548
549                         </script>
550
551 EOQ;
552
553         }
554
555         /**
556          * delete:-1
557          * add:2000
558          * swap: 0 - 1999
559          *
560          */
561         function addSlotToForm($slot_count, $display_count){
562                 $this->form .= "\n<input type='hidden' name='slot_$slot_count'  id='slot_$display_count' value='$slot_count'>";
563         }
564         function prepSlots() {
565                 $view = $this->curText;
566                 $counter = 0;
567                 $return_view = '';
568                 $slotCount = 0;
569                 for ($i = 0; $i < sizeof($this->positions); $i ++) {
570                         $slot = $this->positions[$i];
571                         $class = '';
572
573                         if (empty($this->positions[$i][3])) {
574
575                                 $slotCount ++;
576                                 $class = " class='slot' ";
577                                 $displayCount = $slotCount. $this->positions[$i][3];
578                                 $this->addSlotToForm($slotCount, $displayCount);
579                         }else{
580                                 $displayCount = $slotCount. $this->positions[$i][3];
581                         }
582
583
584                         $explode = explode($slot[0], $view, 2);
585                         $style = '';
586                         $explode[0] .= "<div id = 'slot$displayCount'  $class style='cursor: move$style'>";
587                         $explode[1] = "</div>".$explode[1];
588                         $return_view .= $explode[0].$slot[4];
589                         $view = $explode[1];
590                         $counter ++;
591                 }
592                 $this->yahooSlotCount = $slotCount;
593                 $newView = $return_view.$view;
594                 $newView = str_replace(array ('<slot>', '</slot>'), array ('', ''), $newView);
595
596                 return $newView;
597         }
598
599         function parseOldestFile($file){
600          ob_clean();
601                 require_once('modules/Studio/SugarBackup.php');
602                 $file = str_replace('custom/working/', '' ,$file);
603
604                 $filebk = SugarBackup::oldestBackup($file);
605                 $oldMatches = array();
606                 $oldLabels = array();
607                 // echo $filebk;
608                 if($filebk){
609                         $content = file_get_contents($filebk);
610                         $positions = $this->parsePositions($content, true);
611                         for ($i = 0; $i < sizeof($positions); $i ++) {
612                                 $position = $positions[$i];
613                                 //used for reverse lookups to figure out where the associated slot is
614                                 $slotLookup[$position[2]][$position[3]] = array('position'=>$i, 'value'=>$position[4]);
615                                 $names = $this->parseNames($position[4]);
616                                 $labels = $this->parseLabels($position[4]);
617
618                                 foreach($names as $name){
619                                         $oldMatches[$name[1]] = $position[0];
620                                 }
621                                 foreach($labels as $label){
622                                         $oldLabels[$label[0]] = $position[2];
623                                 }
624                         }
625
626
627
628                 }
629                 foreach($oldLabels as $key=>$value){
630                         $oldLabels[$key] = $slotLookup[$value]['b']['value'];
631                 }
632
633                 $this->oldLabels = $oldLabels;
634                 $this->oldMatches = $oldMatches;
635
636         }
637
638
639         function clearWorkingDirectory(){
640
641                 $file = 'custom/working/';
642                 if(file_exists($file)){
643
644                         rmdir_recursive($file);
645                 }
646
647                 return true;
648
649         }
650
651         /**
652          * UPGRADE TO SMARTY
653          */
654         function upgradeToSmarty() {
655                 return str_replace('{', '{$', $this->curText);
656         }
657 }
658 ?>