curType = $type; } return $type; } function getParsers($file){ if(substr_count($file, 'DetailView.html') > 0 || substr_count($file, 'EditView.html' ) > 0) return array('default'=>'StudioParser', array('StudioParser', 'StudioRowParser')); if(substr_count($file, 'ListView.html' ) > 0) return array('default'=>'XTPLListViewParser', array('XTPLListViewParser')); return array('default'=>'StudioParser', array('StudioParser')); } function parseRows($str){ preg_match_all("'(]*)>(.*?)(]*>)'si", $str, $this->rows,PREG_SET_ORDER); } function parseNames($str){ $results = array(); preg_match_all("'name[ ]*=[ ]*[\'\"]+([a-zA-Z0-9\_]+)[\'\"]+'si", $str, $results,PREG_SET_ORDER); return $results; } function parseLabels($str){ $mod = array(); $app = array(); preg_match_all("'\{MOD\.([a-zA-Z0-9\_]+)\}'si", $str, $mod,PREG_SET_ORDER); preg_match_all("'\{APP\.([a-zA-Z0-9\_]+)\}'si", $str, $app,PREG_SET_ORDER); return array_merge($app, $mod); } function getMaxPosition(){ $max = 0; for($i = 0; $i < count($this->positions) ; $i++){ if($this->positions[$i][2] >= $max){ $max = $this->positions[$i][2] + 1; } } return $max; } function parsePositions($str, $output= false) { $results = array(); preg_match_all("']*sugar=[\'\"]+([a-zA-Z\_]*)([0-9]+)([b]*)[\'\"]+[^>]*>(.*?)'si", $str, $results, PREG_SET_ORDER); if($output){ return $results; } $this->positions = $results; } function parseCols($str){ preg_match_all("'(]*?)>(.*?)(]*?>)'si", $str, $this->cols,PREG_SET_ORDER); } function parse($str){ $this->parsePositions($str); } function positionCount($str) { $result = array (); return preg_match_all("']*sugar=[\'\"]+([a-zA-Z\_]*)([0-9]+)([b]*)[\'\"]+[^>]*>(.*?)'si", $str, $result, PREG_SET_ORDER)/2; } function rowCount($str) { $result = array (); return preg_match_all("'(]*>)(.*?)(]*>)'si", $str, $result); } function loadFile($file) { $this->curFile = $file; $this->curText = file_get_contents($file); $this->form = <<
EOQ; } function buildImageButtons($buttons,$horizontal=true){ $text = ''; foreach($buttons as $button){ if(!$horizontal){ $text .= ''; } if(!empty($button['plain'])){ $text .= << EOQ; }else{ $text .= << EOQ; } if ( !isset($button['image']) ) $text .= "{$button['text']}"; else $text .= "{$button['image']} {$button['text']}"; } $text .= '
'; return $text; } function generateButtons(){ $imageSave = SugarThemeRegistry::current()->getImage( 'studio_save', ''); $imagePublish = SugarThemeRegistry::current()->getImage( 'studio_publish', ''); $imageHistory = SugarThemeRegistry::current()->getImage( 'studio_history', ''); $imageAddRows = SugarThemeRegistry::current()->getImage('studio_addRows', ''); $imageUndo = SugarThemeRegistry::current()->getImage('studio_undo', ''); $imageRedo = SugarThemeRegistry::current()->getImage('studio_redo', ''); $imageAddField = SugarThemeRegistry::current()->getImage( 'studio_addField', ''); $buttons = array(); $buttons[] = array('image'=>$imageUndo,'text'=>$GLOBALS['mod_strings']['LBL_BTN_UNDO'],'actionScript'=>"onclick='jstransaction.undo()'" ); $buttons[] = array('image'=>$imageRedo,'text'=>$GLOBALS['mod_strings']['LBL_BTN_REDO'],'actionScript'=>"onclick='jstransaction.redo()'" ); $buttons[] = array('image'=>$imageAddField,'text'=>$GLOBALS['mod_strings']['LBL_BTN_ADDCUSTOMFIELD'],'actionScript'=>"onclick='studiopopup.display();return false;'" ); $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\"'" ,); $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\"'" ,); $buttons[] = array('image'=>'', 'text'=>'-', 'actionScript'=>'', 'plain'=>true); $buttons[] = array('image'=>$imageSave,'text'=>$GLOBALS['mod_strings']['LBL_BTN_SAVE'],'actionScript'=>"onclick='studiojs.save(\"studio\", false);'"); $buttons[] = array('image'=>$imagePublish,'text'=>$GLOBALS['mod_strings']['LBL_BTN_SAVEPUBLISH'],'actionScript'=>"onclick='studiojs.save(\"studio\", true);'"); $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']}\"'"); return $buttons; } function getFormButtons(){ $buttons = $this->generateButtons(); return $this->buildImageButtons($buttons); } function getForm(){ return $this->form . << EOQ; } function getFiles($module, $fileId=false){ if(empty($GLOBALS['studioDefs'][$module])){ require_once('modules/'. $module . '/metadata/studio.php'); } if($fileId){ return $GLOBALS['studioDefs'][$module][$fileId]; } return $GLOBALS['studioDefs'][$module]; } function getWorkingFile($file, $refresh = false){ $workingFile = 'working/' . $file; $customFile = create_custom_directory($workingFile); if($refresh || !file_exists($customFile)){ copy($file, $customFile); } return $customFile; } function getSwapWith($value){ return $value * 2 - 1; } /** * takes the submited form and parses the file moving the fields around accordingly * it also checks if the original file has a matching field and uses that field instead of attempting to generate a new one */ function handleSave() { $this->parseOldestFile($this->curFile); $fileDef = $this->getFiles($_SESSION['studio']['module'], $_SESSION['studio']['selectedFileId']); $type = $this->getFileType($fileDef['type']); $view = $this->curText; $counter = 0; $return_view = ''; $slotCount = 0; $slotLookup = array(); for ($i = 0; $i < sizeof($this->positions); $i ++) { //used for reverse lookups to figure out where the associated slot is $slotLookup[$this->positions[$i][2]][$this->positions[$i][3]] = array('position'=>$i, 'value'=>$this->positions[$i][4]); } $customFields = $this->focus->custom_fields->getAllBeanFieldsView($type, 'html'); //now we set it to the new values for ($i = 0; $i < sizeof($this->positions); $i ++) { $slot = $this->positions[$i]; if (empty($slot[3])) { $slotCount ++; //if the value in the request doesn't equal our current slot then something should be done if(isset($_REQUEST['slot_'.$slotCount]) && $_REQUEST['slot_'.$slotCount] != $slotCount){ $swapValue = $_REQUEST['slot_'.$slotCount] ; //if its an int then its a simple swap if(is_numeric($swapValue)){ $swapWith = $this->positions[$this->getSwapWith($swapValue)]; //label $slotLookup[$slot[2]]['']['value'] = $this->positions[ $slotLookup[$swapWith[2]]['']['position']][4]; //html $slotLookup[$slot[2]]['b']['value'] = $this->positions[ $slotLookup[$swapWith[2]]['b']['position']][4]; } //now check if its a delete action if(strcmp('add:delete', $swapValue) == 0){ //label $slotLookup[$slot[2]][$slot[3]]['value'] = ' '; //html $slotLookup[$slot[2]]['b']['value'] = ' '; }else{ //now handle the adding of custom fields if(substr_count($swapValue, 'add:')){ $addfield = explode('add:', $_REQUEST['slot_'.$slotCount], 2); //label $slotLookup[$slot[2]][$slot[3]]['value'] = $customFields[$addfield[1]]['label'] ; //html if(!empty($this->oldMatches[$addfield[1]])){ //we have an exact match from the original file use that $slotLookup[$slot[2]]['b']['value'] = $this->oldMatches[$addfield[1]]; }else{ if(!empty($this->oldLabels[$customFields[$addfield[1]]['label']])){ //we have matched the label from the original file use that $slotLookup[$slot[2]]['b']['value'] = $this->oldLabels[$customFields[$addfield[1]]['label']]; }else{ //no matches so use what we are generating $slotLookup[$slot[2]]['b']['value'] = $customFields[$addfield[1]]['html']; } } } } } } } for ($i = 0; $i < sizeof($this->positions); $i ++) { $slot = $this->positions[$i]; $explode = explode($slot[0], $view, 2); $explode[0] .= ""; $explode[1] = "".$explode[1]; $return_view .= $explode[0].$slotLookup[$slot[2]][$slot[3]]['value']; $view = $explode[1]; $counter ++; } $return_view .= $view; $this->saveFile('', $return_view); return $return_view; } function saveFile($file = '', $contents = false) { if (empty ($file)) { $file = $this->curFile; } $fp = sugar_fopen($file, 'w'); $output = $contents ? $contents : $this->curText; if(strpos($file, 'SearchForm.html') > 0) { $fileparts = preg_split("''", $output); if(!empty($fileparts) && count($fileparts) > 1) { //preg_replace_callback doesn't seem to work w/o anonymous method $output = preg_replace_callback("/name\s*=\s*[\"']([^\"']*)[\"']/Us", create_function( '$matches', '$name = str_replace(array("[", "]"), "", $matches[1]); if((strpos($name, "LBL_") == 0) && (strpos($name, "_basic") == 0)) { return str_replace($name, $name . "_basic", $matches[0]); } return $matches[0];' ), $fileparts[1]); $output = $fileparts[0] . '' . $output . '' . $fileparts[2]; } } fwrite($fp, $output); fclose($fp); } function handleSaveLabels($module_name, $language){ require_once('modules/Studio/LabelEditor/LabelEditor.php'); LabelEditor::saveLabels($_REQUEST, $module_name, $language); } /** * UTIL FUNCTIONS */ /** * STATIC FUNCTION DISABLE INPUTS IN AN HTML STRING * */ function disableInputs($str) { $match = array ("'(]*>)'si" => "\$1 disabled readonly $2", "'(]*?type[ ]*=[ ]*[\'\"]submit[\'\"])([^>]*>)'si" => "\$1 disabled readonly style=\"display:none\" $2", "'(]*)'si" => "\$1 disabled readonly $2", // "'(.*)]*>'siU"=>"\$1", "'(href[\ ]*=[\ ]*)([\'])([^\']*)([\'])'si" => "href=\$2javascript:void(0);\$2 alt=\$2\$3\$2", "'(href[\ ]*=[\ ]*)([\"])([^\"]*)([\"])'si" => "href=\$2javascript:void(0)\$2 title=\$2\$3\$2"); return preg_replace(array_keys($match), array_values($match), $str); } function enableLabelEditor($str) { $image = SugarThemeRegistry::current()->getImage( 'edit_inline', "onclick='studiojs.handleLabelClick(\"$2\", 1);' onmouseover='this.style.cursor=\"default\"'"); $match = array ("'>[^<]*\{(MOD.)([^\}]*)\}'si" => "$image{".'$1$2' . "}"); $keys = array_keys($match); $matches = array(); preg_match_all($keys[0], $str, $matches, PREG_SET_ORDER); foreach($matches as $labelmatch){ $label_name = 'label_' . $labelmatch[2]; $this->form .= "\n"; } return preg_replace(array_keys($match), array_values($match), $str); } function writeToCache($file, $view, $preview_file=false) { if (!is_writable($file)) { echo "
Warning: $file is not writeable. Please make sure it is writeable before continuing

"; } if(!$preview_file){ $file_cache = create_cache_directory('studio/'.$file); }else{ $file_cache = create_cache_directory('studio/'.$preview_file); } $fp = sugar_fopen($file_cache, 'w'); $view = $this->disableInputs($view); if(!$preview_file){ $view = $this->enableLabelEditor($view); } fwrite($fp, $view); fclose($fp); return $this->cacheXTPL($file, $file_cache, $preview_file); } function populateRequestFromBuffer($file) { $results = array (); $temp = sugar_fopen($file, 'r'); $buffer = fread($temp, filesize($file)); fclose($temp); preg_match_all("'name[\ ]*=[\ ]*[\']([^\']*)\''si", $buffer, $results); $res = $results[1]; foreach ($res as $r) { $_REQUEST[$r] = $r; } preg_match_all("'name[\ ]*=[\ ]*[\"]([^\"]*)\"'si", $buffer, $results); $res = $results[1]; foreach ($res as $r) { $_REQUEST[$r] = $r; } $_REQUEST['query'] = true; $_REQUEST['advanced'] = true; } function cacheXTPL($file, $cache_file, $preview_file = false) { global $beanList; //now if we have a backup_file lets use that instead of the original if($preview_file){ $file = $preview_file; } if(!isset($the_module))$the_module = $_SESSION['studio']['module']; $files = StudioParser::getFiles($the_module); $xtpl = $files[$_SESSION['studio']['selectedFileId']]['php_file']; $originalFile = $files[$_SESSION['studio']['selectedFileId']]['template_file']; $type = StudioParser::getFileType($files[$_SESSION['studio']['selectedFileId']]['type']); $xtpl_fp = sugar_fopen($xtpl, 'r'); $buffer = fread($xtpl_fp, filesize($xtpl)); fclose($xtpl_fp); $cache_file = create_cache_directory('studio/'.$file); $xtpl_cache = create_cache_directory('studio/'.$xtpl); $module = $this->workingModule; $form_string = "require_once('modules/".$module."/Forms.php');"; if ($type == 'edit' || $type == 'detail') { if (empty ($_REQUEST['record'])) { $buffer = preg_replace('(\$xtpl[\ ]*=)', "\$focus->assign_display_fields('$module'); \$0", $buffer); } else { $buffer = preg_replace('(\$xtpl[\ ]*=)', "\$focus->retrieve('".$_REQUEST['record']."');\n\$focus->assign_display_fields('$module');\n \$0", $buffer); } } $_REQUEST['query'] = true; if (substr_count($file, 'SearchForm') > 0) { $temp_xtpl = new XTemplate($file); if ($temp_xtpl->exists('advanced')) { global $current_language, $beanFiles, $beanList; $mods = return_module_language($current_language, 'DynamicLayout'); $class_name = $beanList[$module]; require_once ($beanFiles[$class_name]); $mod = new $class_name (); $this->populateRequestFromBuffer($file); $mod->assign_display_fields($module); $buffer = str_replace(array ('echo $lv->display();','$search_form->parse("advanced");', '$search_form->out("advanced");', '$search_form->parse("main");', '$search_form->out("main");'), '', $buffer); $buffer = str_replace('echo get_form_footer();', '$search_form->parse("main");'."\n".'$search_form->out("main");'."\necho '
".translate('LBL_ADVANCED', 'DynamicLayout')."
';".'$search_form->parse("advanced");'."\n".'$search_form->out("advanced");'."\n \$sugar_config['list_max_entries_per_page'] = 1;", $buffer); } }else{ if ($type == 'detail') { $buffer = str_replace('header(', 'if(false) header(', $buffer); } } $buffer = str_replace($originalFile, $cache_file, $buffer); $buffer = "".$buffer; $buffer = str_replace($form_string, '', $buffer); $buffer = $this->disableInputs($buffer); $xtpl_fp_cache = sugar_fopen($xtpl_cache, 'w'); fwrite($xtpl_fp_cache, $buffer); fclose($xtpl_fp_cache); return $xtpl_cache; } /** * Yahoo Drag & Drop Support */ //// function yahooJS() { $custom_module = $_SESSION['studio']['module']; $custom_type = $this->curType; return<< .slot { border-width:1px;border-color:#999999;border-style:solid;padding:0px 1px 0px 1px;margin:2px;cursor:move; } .slotB { border-width:0;cursor:move; } EOQ; } /** * delete:-1 * add:2000 * swap: 0 - 1999 * */ function addSlotToForm($slot_count, $display_count){ $this->form .= "\n"; } function prepSlots() { $view = $this->curText; $counter = 0; $return_view = ''; $slotCount = 0; for ($i = 0; $i < sizeof($this->positions); $i ++) { $slot = $this->positions[$i]; $class = ''; if (empty($this->positions[$i][3])) { $slotCount ++; $class = " class='slot' "; $displayCount = $slotCount. $this->positions[$i][3]; $this->addSlotToForm($slotCount, $displayCount); }else{ $displayCount = $slotCount. $this->positions[$i][3]; } $explode = explode($slot[0], $view, 2); $style = ''; $explode[0] .= "
"; $explode[1] = "
".$explode[1]; $return_view .= $explode[0].$slot[4]; $view = $explode[1]; $counter ++; } $this->yahooSlotCount = $slotCount; $newView = $return_view.$view; $newView = str_replace(array ('', ''), array ('', ''), $newView); return $newView; } function parseOldestFile($file){ ob_clean(); require_once('modules/Studio/SugarBackup.php'); $file = str_replace('custom/working/', '' ,$file); $filebk = SugarBackup::oldestBackup($file); $oldMatches = array(); $oldLabels = array(); // echo $filebk; if($filebk){ $content = file_get_contents($filebk); $positions = $this->parsePositions($content, true); for ($i = 0; $i < sizeof($positions); $i ++) { $position = $positions[$i]; //used for reverse lookups to figure out where the associated slot is $slotLookup[$position[2]][$position[3]] = array('position'=>$i, 'value'=>$position[4]); $names = $this->parseNames($position[4]); $labels = $this->parseLabels($position[4]); foreach($names as $name){ $oldMatches[$name[1]] = $position[0]; } foreach($labels as $label){ $oldLabels[$label[0]] = $position[2]; } } } foreach($oldLabels as $key=>$value){ $oldLabels[$key] = $slotLookup[$value]['b']['value']; } $this->oldLabels = $oldLabels; $this->oldMatches = $oldMatches; } function clearWorkingDirectory(){ $file = 'custom/working/'; if(file_exists($file)){ rmdir_recursive($file); } return true; } /** * UPGRADE TO SMARTY */ function upgradeToSmarty() { return str_replace('{', '{$', $this->curText); } } ?>