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.
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.
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
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
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.
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.
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 ********************************************************************************/
44 * interface for studio parsers
48 var $positions = array ();
54 var $labelEditor = true;
55 var $curType = 'detail';
56 var $fieldEditor = true;
57 var $oldMatches = array();
59 function getFileType($type, $setType=true){
61 case 'EditView':$type = 'edit'; break;
62 case 'SearchForm': $type= 'search';break;
63 case 'ListView': $type= 'list';break;
64 default: $type= 'detail';
68 $this->curType = $type;
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'));
80 function parseRows($str){
81 preg_match_all("'(<tr[^>]*)>(.*?)(</tr[^>]*>)'si", $str, $this->rows,PREG_SET_ORDER);
85 function parseNames($str){
87 preg_match_all("'name[ ]*=[ ]*[\'\"]+([a-zA-Z0-9\_]+)[\'\"]+'si", $str, $results,PREG_SET_ORDER);
91 function parseLabels($str){
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);
99 function getMaxPosition(){
101 for($i = 0; $i < count($this->positions) ; $i++){
102 if($this->positions[$i][2] >= $max){
103 $max = $this->positions[$i][2] + 1;
108 function parsePositions($str, $output= false) {
110 preg_match_all("'<span[^>]*sugar=[\'\"]+([a-zA-Z\_]*)([0-9]+)([b]*)[\'\"]+[^>]*>(.*?)</span[ ]*sugar=[\'\"]+[a-zA-Z0-9\_]*[\'\"]+>'si", $str, $results, PREG_SET_ORDER);
114 $this->positions = $results;
116 function parseCols($str){
117 preg_match_all("'(<td[^>]*?)>(.*?)(</td[^>]*?>)'si", $str, $this->cols,PREG_SET_ORDER);
120 function parse($str){
121 $this->parsePositions($str);
123 function positionCount($str) {
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;
127 function rowCount($str) {
129 return preg_match_all("'(<tr[^>]*>)(.*?)(</tr[^>]*>)'si", $str, $result);
132 function loadFile($file) {
133 $this->curFile = $file;
134 $this->curText = file_get_contents($file);
137 <form name='studio' method='POST'>
138 <input type='hidden' name='action' value='save'>
139 <input type='hidden' name='module' value='Studio'>
144 function buildImageButtons($buttons,$horizontal=true){
145 $text = '<table cellspacing=2><tr>';
146 foreach($buttons as $button){
148 $text .= '</tr><tr>';
150 if(!empty($button['plain'])){
152 <td valign='center' {$button['actionScript']}>
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']} >
162 if ( !isset($button['image']) )
163 $text .= "{$button['text']}</td>";
165 $text .= "{$button['image']} {$button['text']}</td>";
167 $text .= '</tr></table>';
171 function generateButtons(){
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']);
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);
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']}\"'");
195 function getFormButtons(){
196 $buttons = $this->generateButtons();
197 return $this->buildImageButtons($buttons);
200 return $this->form . <<<EOQ
210 function getFiles($module, $fileId=false){
211 if(empty($GLOBALS['studioDefs'][$module])){
212 require_once('modules/'. $module . '/metadata/studio.php');
215 return $GLOBALS['studioDefs'][$module][$fileId];
217 return $GLOBALS['studioDefs'][$module];
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);
230 function getSwapWith($value){
231 return $value * 2 - 1;
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
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;
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]);
251 $customFields = $this->focus->custom_fields->getAllBeanFieldsView($type, 'html');
253 //now we set it to the new values
255 for ($i = 0; $i < sizeof($this->positions); $i ++) {
256 $slot = $this->positions[$i];
258 if (empty($slot[3])) {
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){
264 $swapValue = $_REQUEST['slot_'.$slotCount] ;
265 //if its an int then its a simple swap
266 if(is_numeric($swapValue)){
268 $swapWith = $this->positions[$this->getSwapWith($swapValue)];
271 $slotLookup[$slot[2]]['']['value'] = $this->positions[ $slotLookup[$swapWith[2]]['']['position']][4];
273 $slotLookup[$slot[2]]['b']['value'] = $this->positions[ $slotLookup[$swapWith[2]]['b']['position']][4];
275 //now check if its a delete action
276 if(strcmp('add:delete', $swapValue) == 0){
278 $slotLookup[$slot[2]][$slot[3]]['value'] = ' ';
280 $slotLookup[$slot[2]]['b']['value'] = ' ';
283 //now handle the adding of custom fields
284 if(substr_count($swapValue, 'add:')){
285 $addfield = explode('add:', $_REQUEST['slot_'.$slotCount], 2);
288 $slotLookup[$slot[2]][$slot[3]]['value'] = $customFields[$addfield[1]]['label'] ;
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]];
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']];
298 //no matches so use what we are generating
299 $slotLookup[$slot[2]]['b']['value'] = $customFields[$addfield[1]]['html'];
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];
316 $return_view .= $explode[0].$slotLookup[$slot[2]][$slot[3]]['value'];
320 $return_view .= $view;
322 $this->saveFile('', $return_view);
326 function saveFile($file = '', $contents = false) {
328 $file = $this->curFile;
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",
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]);
350 $output = $fileparts[0] . '<!-- BEGIN:main -->' . $output . '<!-- END:main -->' . $fileparts[2];
354 fwrite($fp, $output);
358 function handleSaveLabels($module_name, $language){
359 require_once('modules/Studio/LabelEditor/LabelEditor.php');
360 LabelEditor::saveLabels($_REQUEST, $module_name, $language);
367 * STATIC FUNCTION DISABLE INPUTS IN AN HTML STRING
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);
379 function enableLabelEditor($str) {
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);
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'>";
391 return preg_replace(array_keys($match), array_values($match), $str);
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>";
402 $file_cache = create_cache_directory('studio/'.$file);
404 $file_cache = create_cache_directory('studio/'.$preview_file);
406 $fp = sugar_fopen($file_cache, 'w');
407 $view = $this->disableInputs($view);
409 $view = $this->enableLabelEditor($view);
413 return $this->cacheXTPL($file, $file_cache, $preview_file);
416 function populateRequestFromBuffer($file) {
418 $temp = sugar_file_get_contents($file);
419 preg_match_all("'name[\ ]*=[\ ]*[\']([^\']*)\''si", $buffer, $results);
421 foreach ($res as $r) {
424 preg_match_all("'name[\ ]*=[\ ]*[\"]([^\"]*)\"'si", $buffer, $results);
426 foreach ($res as $r) {
430 $_REQUEST['query'] = true;
431 $_REQUEST['advanced'] = true;
434 function cacheXTPL($file, $cache_file, $preview_file = false) {
436 //now if we have a backup_file lets use that instead of the original
438 $file = $preview_file;
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;
452 $form_string = "require_once('modules/".$module."/Forms.php');";
454 if ($type == 'edit' || $type == 'detail') {
455 if (empty ($_REQUEST['record'])) {
456 $buffer = preg_replace('(\$xtpl[\ ]*=)', "\$focus->assign_display_fields('$module'); \$0", $buffer);
458 $buffer = preg_replace('(\$xtpl[\ ]*=)', "\$focus->retrieve('".$_REQUEST['record']."');\n\$focus->assign_display_fields('$module');\n \$0", $buffer);
461 $_REQUEST['query'] = true;
462 if (substr_count($file, 'SearchForm') > 0) {
463 $temp_xtpl = new XTemplate($file);
464 if ($temp_xtpl->exists('advanced')) {
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 ();
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);
479 if ($type == 'detail') {
480 $buffer = str_replace('header(', 'if(false) header(', $buffer);
484 $buffer = str_replace($originalFile, $cache_file, $buffer);
485 $buffer = "<?php\n\$sugar_config['list_max_entries_per_page'] = 1;\n ?>".$buffer;
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);
496 * Yahoo Drag & Drop Support
498 ////<script type="text/javascript" src="modules/Studio/studio.js" ></script>
500 $custom_module = $_SESSION['studio']['module'];
501 $custom_type = $this->curType;
502 $v = getVersionedPath('');
504 <style type='text/css'>
506 border-width:1px;border-color:#999999;border-style:solid;padding:0px 1px 0px 1px;margin:2px;cursor:move;
511 border-width:0;cursor:move;
516 <!-- Namespace source file -->
518 <script type="text/javascript" src="modules/Studio/JSTransaction.js?v=$v" ></script>
520 var jstransaction = new JSTransaction();
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>
531 function dragDropInit(){
533 YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
535 for(mj = 0; mj <= $this->yahooSlotCount; mj++){
536 yahooSlots["slot" + mj] = new ygDDSlot("slot" + mj, "studio");
538 for(mj = 0; mj < dyn_field_count; mj++){
539 yahooSlots["dyn_field_" + mj] = new ygDDSlot("dyn_field_" + mj, "studio");
542 yahooSlots['s_field_delete'] = new YAHOO.util.DDTarget("s_field_delete", 'studio');
545 YAHOO.util.Event.addListener(window, "load", dragDropInit);
546 var custom_module = '$custom_module';
547 var custom_view = '$custom_type';
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'>";
564 function prepSlots() {
565 $view = $this->curText;
569 for ($i = 0; $i < sizeof($this->positions); $i ++) {
570 $slot = $this->positions[$i];
573 if (empty($this->positions[$i][3])) {
576 $class = " class='slot' ";
577 $displayCount = $slotCount. $this->positions[$i][3];
578 $this->addSlotToForm($slotCount, $displayCount);
580 $displayCount = $slotCount. $this->positions[$i][3];
584 $explode = explode($slot[0], $view, 2);
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];
592 $this->yahooSlotCount = $slotCount;
593 $newView = $return_view.$view;
594 $newView = str_replace(array ('<slot>', '</slot>'), array ('', ''), $newView);
599 function parseOldestFile($file){
601 require_once('modules/Studio/SugarBackup.php');
602 $file = str_replace('custom/working/', '' ,$file);
604 $filebk = SugarBackup::oldestBackup($file);
605 $oldMatches = array();
606 $oldLabels = array();
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]);
618 foreach($names as $name){
619 $oldMatches[$name[1]] = $position[0];
621 foreach($labels as $label){
622 $oldLabels[$label[0]] = $position[2];
629 foreach($oldLabels as $key=>$value){
630 $oldLabels[$key] = $slotLookup[$value]['b']['value'];
633 $this->oldLabels = $oldLabels;
634 $this->oldMatches = $oldMatches;
639 function clearWorkingDirectory(){
641 $file = 'custom/working/';
642 if(file_exists($file)){
644 rmdir_recursive($file);
654 function upgradeToSmarty() {
655 return str_replace('{', '{$', $this->curText);