studioparser.php
来自「SugarCRM5.1 开源PHP客户关系管理系统」· PHP 代码 · 共 668 行 · 第 1/2 页
PHP
668 行
<?phpif(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');/** * SugarCRM is a customer relationship management program developed by * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation with the addition of the following permission added * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, see http://www.gnu.org/licenses or write to the Free * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA. * * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road, * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * SugarCRM" logo. If the display of the logo is not reasonably feasible for * technical reasons, the Appropriate Legal Notices must display the words * "Powered by SugarCRM". */require_once('include/utils/file_utils.php');/** * interface for studio parsers */class StudioParser { var $positions = array (); var $rows = array (); var $cols = array (); var $curFile = ''; var $curText = ''; var $form; var $labelEditor = true; var $curType = 'detail'; var $fieldEditor = true; var $oldMatches = array(); function getFileType($type, $setType=true){ switch($type){ case 'EditView':$type = 'edit'; break; case 'SearchForm': $type= 'search';break; case 'ListView': $type= 'list';break; default: $type= 'detail'; } if($setType){ $this->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("'(<tr[^>]*)>(.*?)(</tr[^>]*>)'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("'<span[^>]*sugar=[\'\"]+([a-zA-Z\_]*)([0-9]+)([b]*)[\'\"]+[^>]*>(.*?)</span[ ]*sugar=[\'\"]+[a-zA-Z0-9\_]*[\'\"]+>'si", $str, $results, PREG_SET_ORDER); if($output){ return $results; } $this->positions = $results; } function parseCols($str){ preg_match_all("'(<td[^>]*?)>(.*?)(</td[^>]*?>)'si", $str, $this->cols,PREG_SET_ORDER); } function parse($str){ $this->parsePositions($str); } function positionCount($str) { $result = array (); 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; } function rowCount($str) { $result = array (); return preg_match_all("'(<tr[^>]*>)(.*?)(</tr[^>]*>)'si", $str, $result); } function loadFile($file) { $this->curFile = $file; $this->curText = file_get_contents($file); $this->form = <<<EOQ </form> <form name='studio' method='POST'> <input type='hidden' name='action' value='save'> <input type='hidden' name='module' value='Studio'> EOQ; } function buildImageButtons($buttons,$horizontal=true){ $text = '<table cellspacing=2><tr>'; foreach($buttons as $button){ if(!$horizontal){ $text .= '</tr><tr>'; } if(!empty($button['plain'])){ $text .= <<<EOQ <td valign='center' {$button['actionScript']}>EOQ; }else{ $text .= <<<EOQ <td valign='center' class='button' style='cursor:default' onmousedown='this.className="buttonOn";return false;' onmouseup='this.className="button"' onmouseout='this.className="button"' {$button['actionScript']} >EOQ;}$text .= "{$button['image']} {$button['text']}</td>"; } $text .= '</tr></table>'; return $text; } function generateButtons(){ global $image_path; $imageSave = get_image($image_path. 'studio_save', ''); $imagePublish = get_image($image_path. 'studio_publish', ''); $imageHistory = get_image($image_path. 'studio_history', ''); $imageAddRows = get_image($image_path.'studio_addRows', ''); $imageUndo = get_image($image_path.'studio_undo', ''); $imageRedo = get_image($image_path.'studio_redo', ''); $imageAddField = get_image($image_path. '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 </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] .= "<span sugar='". $slot[1] . $slot[2]. $slot[3]. "'>"; $explode[1] = "</span sugar='" .$slot[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("'<!--\s*(BEGIN|END)\s*:\s*main\s*-->'", $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(
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?