From 265b7e85766ba403ca0a8b58648dd091e483cf38 Mon Sep 17 00:00:00 2001 From: wei <> Date: Mon, 30 Jan 2006 22:31:58 +0000 Subject: Adding THtmlArea. --- framework/Web/UI/TAssetManager.php | 19 +- framework/Web/UI/TTarAssetManager.php | 74 +++++ framework/Web/UI/WebControls/TCompareValidator.php | 35 +-- framework/Web/UI/WebControls/THtmlArea.php | 307 +++++++++++++++++++++ .../Web/UI/WebControls/TValueTypeValidator.php | 90 ++++++ 5 files changed, 492 insertions(+), 33 deletions(-) create mode 100644 framework/Web/UI/TTarAssetManager.php create mode 100644 framework/Web/UI/WebControls/THtmlArea.php create mode 100644 framework/Web/UI/WebControls/TValueTypeValidator.php (limited to 'framework/Web/UI') diff --git a/framework/Web/UI/TAssetManager.php b/framework/Web/UI/TAssetManager.php index 94b1bc55..cd25a74c 100644 --- a/framework/Web/UI/TAssetManager.php +++ b/framework/Web/UI/TAssetManager.php @@ -131,6 +131,23 @@ class TAssetManager extends TModule $this->_baseUrl=$value; } + public function getPublishedUrl($path) + { + if(($fullpath=realpath($path))!==false) + { + $dir=$this->hash(dirname($fullpath)); + $file=$this->_basePath.'/'.$dir.'/'.basename($fullpath); + if(is_file($file) || is_dir($file)) + return $this->_baseUrl.'/'.$dir.'/'.basename($fullpath); + } + return null; + } + + public function isPublished($path) + { + return $this->getPublishedUrl($path) !== null; + } + /** * Publishes a file or a directory (recursively). * This method will copy the content in a directory (recursively) to @@ -181,7 +198,7 @@ class TAssetManager extends TModule * @param string string to be hashed. * @return string hashed string. */ - private function hash($dir) + protected function hash($dir) { return sprintf('%x',crc32($dir)); } diff --git a/framework/Web/UI/TTarAssetManager.php b/framework/Web/UI/TTarAssetManager.php new file mode 100644 index 00000000..4a05891e --- /dev/null +++ b/framework/Web/UI/TTarAssetManager.php @@ -0,0 +1,74 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2006 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.Web.UI + */ + +/** + * TTarAssetDeployer class. + * Publish a tar file by extracting its contents to the assets directory. + * Each tar file must be accomplished with its own MD5 check sum file. + * The MD5 file is published when the tar contents are successfully + * extracted to the assets directory. The presence of the MD5 file + * as published asset assumes that the tar file has already been extracted. + * + * Compressed tar files are not supported. + * + * @author Wei Zhuo + * @version $Revision: $ $Date: $ + * @package System.Web.UI + * @since 3.0 + */ +class TTarAssetManager extends TAssetManager +{ + /** + * Publish a tar file by extracting its contents to the assets directory. + * Each tar file must be accomplished with its own MD5 check sum file. + * The MD5 file is published when the tar contents are successfully + * extracted to the assets directory. The presence of the MD5 file + * as published asset assumes that the tar file has already been extracted. + * @param string tar filename + * @param string MD5 checksum for the corresponding tar file. + * @return string URL path to the directory where the tar file was extracted. + */ + public function publishTarFile($tarfile, $md5sum) + { + //if md5 file is published assume tar asset deployed + if(($md5Path = $this->getPublishedUrl($md5sum)) != null) + return dirname($md5Path); + + if(($fullpath=realpath($tarfile))===false) + throw new TIOException('unable_to_locate_tar_file', $tarfile); + else if(is_file($fullpath)) + { + $dir=$this->hash(dirname($fullpath)); + $destination = $this->getBasePath().'/'.$dir.'/'; + if($this->deployTarFile($fullpath, $destination)) + return dirname($this->publishFilePath($md5sum)); + } + + throw new TIOException('unable_to_publish_tar_file', $tarfile); + } + + /** + * Extracts the tar file to the destination directory. + * N.B Tar file must not be compressed. + * @param string tar file + * @param string path where the contents of tar file are to be extracted + * @return boolean true if extract successful, false otherwise. + */ + protected function deployTarFile($fullpath,$destination) + { + Prado::using('System.Data.TTarFileExtractor'); + $tar = new TTarFileExtractor($fullpath); + return $tar->extract($destination); + } +} + +?> \ No newline at end of file diff --git a/framework/Web/UI/WebControls/TCompareValidator.php b/framework/Web/UI/WebControls/TCompareValidator.php index 02eddfd4..eeebb946 100644 --- a/framework/Web/UI/WebControls/TCompareValidator.php +++ b/framework/Web/UI/WebControls/TCompareValidator.php @@ -104,7 +104,7 @@ class TCompareValidator extends TBaseValidator } /** - * @return string the comparison operation to perform (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck). Defaults to Equal. + * @return string the comparison operation to perform (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual). Defaults to Equal. */ public function getOperator() { @@ -112,12 +112,12 @@ class TCompareValidator extends TBaseValidator } /** - * Sets the comparison operation to perform (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck) + * Sets the comparison operation to perform (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual) * @param string the comparison operation */ public function setOperator($value) { - $this->setViewState('Operator',TPropertyValue::ensureEnum($value,'Equal','NotEqual','GreaterThan','GreaterThanEqual','LessThan','LessThanEqual','DataTypeCheck'),'Equal'); + $this->setViewState('Operator',TPropertyValue::ensureEnum($value,'Equal','NotEqual','GreaterThan','GreaterThanEqual','LessThan','LessThanEqual'),'Equal'); } /** @@ -149,9 +149,6 @@ class TCompareValidator extends TBaseValidator if(($value=$this->getValidationValue($this->getValidationTarget()))==='') return true; - if($this->getOperator()==='DataTypeCheck') - return $this->evaluateDataTypeCheck($value); - if(($controlToCompare=$this->getControlToCompare())!=='') { if(($control2=$this->findControl($controlToCompare))===null) @@ -182,32 +179,6 @@ class TCompareValidator extends TBaseValidator return false; } - /** - * Determine if the given value is of a particular type using RegExp. - * @param string value to check - * @return boolean true if value fits the type expression. - */ - protected function evaluateDataTypeCheck($value) - { - switch($this->getValueType()) - { - case 'Integer': - return preg_match('/^[-+]?[0-9]+$/',trim($value)); - case 'Float': - case 'Double': - return preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value)); - case 'Currency': - return preg_match('/[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value)); - case 'Date': - $dateFormat = $this->getDateFormat(); - if(strlen($dateFormat)) - return pradoParseDate($value, $dateFormat) !== null; - else - return strtotime($value) > 0; - } - return true; - } - /** * Parse the pair of values into the appropriate value type. * @param string value one diff --git a/framework/Web/UI/WebControls/THtmlArea.php b/framework/Web/UI/WebControls/THtmlArea.php new file mode 100644 index 00000000..4196de3a --- /dev/null +++ b/framework/Web/UI/WebControls/THtmlArea.php @@ -0,0 +1,307 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2006 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.Web.UI + */ + +/** + * THtmlArea class + * + * THtmlArea wraps the visual editting functionalities provided by the + * TinyMCE project {@link http://tinymce.moxiecode.com/}. + * + * THtmlArea displays a WYSIWYG text area on the Web page for user input + * in the HTML format. The text displayed in the THtmlArea component is + * specified or determined by using the Text property. + * + * To enable the visual editting on the client side, set the property + * EnableVisualEdit to true (which is default value). + * To set the size of the editor when the visual editting is enabled, + * set the Width and Height properties instead of + * Columns and Rows because the latter has no meaning + * under the situation. + * + * The default editor gives only the basic tool bar. To change or add + * additional tool bars, use the Options property add to additional + * editor options with each options on a new line. + * See http://tinymce.moxiecode.com/tinymce/docs/index.html + * for a list of options. The options can be change/added as shown in the + * following example. + * + * + * + * plugins : "contextmenu,paste" + * language : "zh_CN" + * + * + * + * + * Compatibility + * The client-side visual editting capability is supported by + * Internet Explorer 5.0+ for Windows and Gecko-based browser. + * If the browser does not support the visual editting, + * a traditional textarea will be displayed. + * + * Browser support + * + * + * Windows XP MacOS X 10.4 + * ---------------------------------------------------- + * MSIE 6 OK + * MSIE 5.5 SP2 OK + * MSIE 5.0 OK + * Mozilla 1.7.x OK OK + * Firefox 1.0.x OK OK + * Firefox 1.5b2 OK OK + * Safari 2.0 (412) OK(1) + * Opera 9 Preview 1 OK(1) OK(1) + * ---------------------------------------------------- + * * (1) - Partialy working + * ---------------------------------------------------- + * + * + * @author Wei Zhuo + * @version $Revision: $ $Date: $ + * @package System.Web.UI.WebControls + * @since 3.0 + */ +class THtmlArea extends TTextBox +{ + /** + * Overrides the parent implementation. + * TextMode for THtmlArea control is always 'MultiLine' + * @return string the behavior mode of the THtmlArea component. + */ + public function getTextMode() + { + return 'MultiLine'; + } + + /** + * Overrides the parent implementation. + * TextMode for THtmlArea is always 'MultiLine' and cannot be changed to others. + * @param string the text mode + */ + public function setTextMode($value) + { + throw new TInvalidOperationException("You cannot set 'TextMode' property."); + } + + /** + * Overrides the parent implementation. Return always false if EnableVisualEdit + * is false, otherwise normal settings are returned. + * @return boolean false + */ + public function getAutoPostBack() + { + if(!$this->getEnableVisualEdit()) + return false; + return parent::getAutoPostBack(); + } + + /** + * @return boolean whether to show WYSIWYG text editor + */ + public function getEnableVisualEdit() + { + return $this->getViewState('VisualEditEnabled',true); + } + + /** + * Sets whether to show WYSIWYG text editor. + * @param boolean whether to show WYSIWYG text editor + */ + public function setEnableVisualEdit($value) + { + $this->setViewState('VisualEditEnabled',TPropertyValue::ensureBoolean($value),true); + } + + /** + * Gets the current culture. + * @return string current culture, e.g. en_AU. + */ + public function getCulture() + { + return $this->getViewState('Culture', ''); + } + + /** + * Sets the culture/language for the date picker. + * @param string a culture string, e.g. en_AU. + */ + public function setCulture($value) + { + $this->setViewState('Culture', $value, ''); + } + + /** + * Gets the list of options for the WYSIWYG (TinyMCE) editor + * @see http://tinymce.moxiecode.com/tinymce/docs/index.html + * @return string options + */ + public function getOptions() + { + return $this->getViewState('Options', ''); + } + + /** + * Sets the list of options for the WYSIWYG (TinyMCE) editor + * @see http://tinymce.moxiecode.com/tinymce/docs/index.html + * @param string options + */ + public function setOptions($value) + { + $this->setViewState('Options', $value, ''); + } + + /** + * Overrides parent implement, provide default width of '450px'. + * @param string editor width (CSS) + */ + public function setWidth($value) + { + $this->setViewState('Width', $value, '450px'); + } + + /** + * @param string editor width, default '450px'. + */ + public function getWidth() + { + return $this->getViewState('Width', '450px'); + } + + /** + * @param string editor height, default '200px'. + */ + public function getHeight() + { + return $this->getViewState('Height', '200px'); + } + + /** + * Overrides parent implement, provide default height of '200px'. + * @param string editor height (CSS) + */ + public function setHeight($value) + { + $this->setViewState('Height', $value, '200px'); + } + + /** + * Adds attribute name-value pairs to renderer. + * This method overrides the parent implementation by registering + * additional javacript code. + * @param THtmlWriter the writer used for the rendering purpose + */ + protected function addAttributesToRender($writer) + { + if($this->getEnableVisualEdit()) + { + $writer->addAttribute('id',$this->getClientID()); + if(($width=$this->getWidth()) != '') + $this->getStyle()->setWidth($width); + if(($height=$this->getHeight()) != '') + $this->getStyle()->setHeight($height); + + $this->registerEditorClientScript($writer); + } + parent::addAttributesToRender($writer); + } + + /** + * Registers the editor javascript file and code to initialize the editor. + */ + protected function registerEditorClientScript($writer) + { + $scripts = $this->getPage()->getClientScript(); + if(!$scripts->isScriptFileRegistered('prado:THtmlArea')) + $scripts->registerScriptFile('prado:THtmlArea', $this->getScriptUrl()); + $serializer = new TJavascriptSerializer($this->getEditorOptions()); + $options = $serializer->toJavascript(true); + $script = "if(tinyMCE){ tinyMCE.init($options); }"; + $scripts->registerEndScript('THtmlArea'.$this->ClientID,$script); + } + + /** + * @return string editor script URL. + */ + protected function getScriptUrl() + { + return $this->getScriptDeploymentPath().'/tiny_mce/tiny_mce_gzip.php'; + } + + /** + * Gets the editor script base URL by publishing the tarred source via TTarAssetManager. + * @return string URL base path to the published editor script + */ + protected function getScriptDeploymentPath() + { + $manager = Prado::createComponent('System.Web.UI.TTarAssetManager'); + $manager->init(null); + $tarfile = Prado::getPathOfNamespace('System.3rdParty.TinyMCE.tiny_mce', '.tar'); + $md5sum = Prado::getPathOfNamespace('System.3rdParty.TinyMCE.tiny_mce', '.md5'); + return $manager->publishTarFile($tarfile, $md5sum); + } + + /** + * Default editor options gives basic tool bar only. + * @return array editor initialization options. + */ + protected function getEditorOptions() + { + $options['mode'] = 'exact'; + $options['elements'] = $this->getClientID(); + $options['language'] = $this->getLanguageSuffix($this->getCulture()); + $options['theme'] = 'advanced'; + $options['theme_advanced_buttons1'] = 'bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright, justifyfull,separator,bullist,numlist,separator,undo,redo,separator,link,unlink,separator,code,help'; + $options['theme_advanced_buttons2'] = ''; + $options['theme_advanced_buttons3'] = ''; + $options['theme_advanced_toolbar_location'] = 'top'; + $options['theme_advanced_toolbar_align'] = 'left'; + $options['theme_advanced_path_location'] = 'bottom'; + $options['extended_valid_elements'] = 'a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]'; + + $options = array_merge($options, $this->parseEditorOptions($this->getOptions())); + return $options; + } + + /** + * Parse additional options set in the Options property. + * @return array additional custom options + */ + protected function parseEditorOptions($string) + { + $options = array(); + $substrings = preg_split('/\n|,\n/', trim($string)); + foreach($substrings as $bits) + { + $option = explode(":",$bits); + if(count($option) == 2) + $options[trim($option[0])] = trim(preg_replace('/\'|"/','', $option[1])); + } + return $options; + } + + /** + * @return string localized editor interface language extension. + */ + protected function getLanguageSuffix($culture) + { + if(empty($culture)) + { + $app = $this->getApplication()->getGlobalization(); + if(!is_null($app)) + $culture = $app->Culture; + } + return empty($culture) ? 'en' : strtolower($culture); + } +} + +?> \ No newline at end of file diff --git a/framework/Web/UI/WebControls/TValueTypeValidator.php b/framework/Web/UI/WebControls/TValueTypeValidator.php new file mode 100644 index 00000000..d24fda4f --- /dev/null +++ b/framework/Web/UI/WebControls/TValueTypeValidator.php @@ -0,0 +1,90 @@ + + * @version $Revision: $ $Date: $ + * @package System.Web.UI.WebControls + * @since 3.0 + */ +class TValueTypeValidator +{ + /** + * @return string the data type that the values being compared are converted to before the comparison is made. Defaults to String. + */ + public function getValueType() + { + return $this->getViewState('ValueType','String'); + } + + /** + * Sets the data type (Integer, Double, Currency, Date, String) that the values being compared are converted to before the comparison is made. + * @param string the data type + */ + public function setValueType($value) + { + $this->setViewState('ValueType',TPropertyValue::ensureEnum($value,'Integer','Double','Date','Currency','String'),'String'); + } + + /** + * Sets the date format for a date validation + * @param string the date format value + */ + public function setDateFormat($value) + { + $this->setViewState('DateFormat', $value, ''); + } + + /** + * @return string the date validation date format if any + */ + public function getDateFormat() + { + return $this->getViewState('DateFormat', ''); + } + + + /** + * Determine if the given value is of a particular type using RegExp. + * @param string value to check + * @return boolean true if value fits the type expression. + */ + protected function evaluateDataTypeCheck($value) + { + switch($this->getValueType()) + { + case 'Integer': + return preg_match('/^[-+]?[0-9]+$/',trim($value)); + case 'Float': + case 'Double': + return preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value)); + case 'Currency': + return preg_match('/[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value)); + case 'Date': + $dateFormat = $this->getDateFormat(); + if(strlen($dateFormat)) + return pradoParseDate($value, $dateFormat) !== null; + else + return strtotime($value) > 0; + } + return true; + } + + /** + * This method overrides the parent's implementation. + * The validation succeeds if the input data is of valid type. + * The validation always succeeds if ControlToValidate is not specified + * or the input data is empty. + * @return boolean whether the validation succeeds + */ + public function evaluateIsValid() + { + if(($value=$this->getValidationValue($this->getValidationTarget()))==='') + return true; + + return $this->evaluateDataTypeCheck($value); + } +} + +?> \ No newline at end of file -- cgit v1.2.3