Documentation TYPO3 par Ameos

wizard_colorpicker.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2005 Kasper Skårhøj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00060 $BACK_PATH = '';
00061 require('init.php');
00062 require('template.php');
00063 require_once(PATH_t3lib.'class.t3lib_stdgraphic.php');
00064 $LANG->includeLLFile('EXT:lang/locallang_wizards.xml');
00065 
00075 class SC_wizard_colorpicker {
00076 
00077                 // GET vars:
00078         var $P;                         // Wizard parameters, coming from TCEforms linking to the wizard.
00079         var $colorValue;        // Value of the current color picked.
00080         var $fieldChangeFunc;   // Serialized functions for changing the field... Necessary to call when the value is transferred to the TCEform since the form might need to do internal processing. Otherwise the value is simply not be saved.
00081         var $fieldName;         // Form name (from opener script)
00082         var $formName;          // Field name (from opener script)
00083         var $md5ID;                     // ID of element in opener script for which to set color.
00084         var $showPicker;        // Internal: If false, a frameset is rendered, if true the content of the picker script.
00085 
00086                 // Static:
00087         var $HTMLcolorList = "aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow,white";
00088 
00089                 // Internal:
00090         var $pickerImage = '';
00091         var $imageError = '';           // Error message if image not found.
00092         var $doc;                                       // Template Object
00093         var $content;                           // Accumulated content.
00094 
00095 
00096 
00097 
00103         function init() {
00104                 global $BACK_PATH, $LANG;
00105 
00106                         // Setting GET vars (used in frameset script):
00107                 $this->P = t3lib_div::_GP('P',1);
00108 
00109                         // Setting GET vars (used in colorpicker script):
00110                 $this->colorValue = t3lib_div::_GP('colorValue');
00111                 $this->fieldChangeFunc = t3lib_div::_GP('fieldChangeFunc');
00112                 $this->fieldName = t3lib_div::_GP('fieldName');
00113                 $this->formName = t3lib_div::_GP('formName');
00114                 $this->md5ID = t3lib_div::_GP('md5ID');
00115                 $this->exampleImg = t3lib_div::_GP('exampleImg');
00116 
00117 
00118                         // Resolving image (checking existence etc.)
00119                 $this->imageError = '';
00120                 if ($this->exampleImg)  {
00121                         $this->pickerImage = t3lib_div::getFileAbsFileName($this->exampleImg,1,1);
00122                         if (!$this->pickerImage || !@is_file($this->pickerImage))       {
00123                                 $this->imageError = 'ERROR: The image, "'.$this->exampleImg.'", could not be found!';
00124                         }
00125                 }
00126 
00127                         // Setting field-change functions:
00128                 $fieldChangeFuncArr = unserialize($this->fieldChangeFunc);
00129                 $update = '';
00130                 if (is_array($fieldChangeFuncArr))      {
00131                         unset($fieldChangeFuncArr['alert']);
00132                         foreach($fieldChangeFuncArr as $v)      {
00133                                 $update.= '
00134                                 parent.opener.'.$v;
00135                         }
00136                 }
00137 
00138                         // Initialize document object:
00139                 $this->doc = t3lib_div::makeInstance('smallDoc');
00140                 $this->doc->backPath = $BACK_PATH;
00141                 $this->doc->docType = 'xhtml_trans';
00142                 $this->doc->JScode = $this->doc->wrapScriptTags('
00143                         function checkReference()       {       //
00144                                 if (parent.opener && parent.opener.document && parent.opener.document.'.$this->formName.' && parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"])        {
00145                                         return parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"];
00146                                 } else {
00147                                         close();
00148                                 }
00149                         }
00150                         function changeBGcolor(color) { // Changes the color in the table sample back in the TCEform.
00151                             if (parent.opener.document.layers)  {
00152                                 parent.opener.document.layers["'.$this->md5ID.'"].bgColor = color;
00153                             } else if (parent.opener.document.all)      {
00154                                 parent.opener.document.all["'.$this->md5ID.'"].style.background = color;
00155                                 } else if (parent.opener.document.getElementById && parent.opener.document.getElementById("'.$this->md5ID.'"))  {
00156                                         parent.opener.document.getElementById("'.$this->md5ID.'").bgColor = color;
00157                                 }
00158                         }
00159                         function setValue(input)        {       //
00160                                 var field = checkReference();
00161                                 if (field)      {
00162                                         field.value = input;
00163                                         '.$update.'
00164                                         changeBGcolor(input);
00165                                 }
00166                         }
00167                         function getValue()     {       //
00168                                 var field = checkReference();
00169                                 return field.value;
00170                         }
00171                 ');
00172 
00173                         // Start page:
00174                 $this->content.=$this->doc->startPage($LANG->getLL('colorpicker_title'));
00175         }
00176 
00182         function main() {
00183                 global $LANG;
00184 
00185                 if(!t3lib_div::_GP('showPicker')) {     // Show frameset by default:
00186                         $this->frameSet();
00187                 } else {
00188 
00189                                 // Putting together the items into a form:
00190                         $content = '
00191                                 <form name="colorform" method="post" action="wizard_colorpicker.php">
00192                                         '.$this->colorMatrix().'
00193                                         '.$this->colorList().'
00194                                         '.$this->colorImage().'
00195 
00196                                                 <!-- Value box: -->
00197                                         <p class="c-head">'.$LANG->getLL('colorpicker_colorValue',1).'</p>
00198                                         <table border="0" cellpadding="0" cellspacing="3">
00199                                                 <tr>
00200                                                         <td><input type="text" '.$this->doc->formWidth(7).' maxlength="10" name="colorValue" value="'.htmlspecialchars($this->colorValue).'" /></td>
00201                                                         <td style="background-color:'.htmlspecialchars($this->colorValue).'; border: 1px solid black;">&nbsp;<span style="color: black;">'.$LANG->getLL('colorpicker_black',1).'</span>&nbsp;<span style="color: white;">'.$LANG->getLL('colorpicker_white',1).'</span>&nbsp;</td>
00202                                                         <td><input type="submit" name="save_close" value="'.$LANG->getLL('colorpicker_setClose',1).'" /></td>
00203                                                 </tr>
00204                                         </table>
00205 
00206                                                 <!-- Hidden fields with values that has to be kept constant -->
00207                                         <input type="hidden" name="showPicker" value="1" />
00208                                         <input type="hidden" name="fieldChangeFunc" value="'.htmlspecialchars($this->fieldChangeFunc).'" />
00209                                         <input type="hidden" name="fieldName" value="'.htmlspecialchars($this->fieldName).'" />
00210                                         <input type="hidden" name="formName" value="'.htmlspecialchars($this->formName).'" />
00211                                         <input type="hidden" name="md5ID" value="'.htmlspecialchars($this->md5ID).'" />
00212                                         <input type="hidden" name="exampleImg" value="'.htmlspecialchars($this->exampleImg).'" />
00213                                 </form>';
00214 
00215                                 // If the save/close button is clicked, then close:
00216                         if(t3lib_div::_GP('save_close')) {
00217                                 $content.=$this->doc->wrapScriptTags('
00218                                         setValue(\''.$this->colorValue.'\');
00219                                         parent.close();
00220                                 ');
00221                         }
00222 
00223                                 // Output:
00224                         $this->content.=$this->doc->section($LANG->getLL('colorpicker_title'), $content, 0,1);
00225                         $this->content.=$this->doc->endPage();
00226                 }
00227         }
00228 
00234         function printContent() {
00235                 echo $this->content;
00236         }
00237 
00245         function frameSet() {
00246                 global $LANG;
00247 
00248                         // Set doktype:
00249                 $GLOBALS['TBE_TEMPLATE']->docType = 'xhtml_frames';
00250                 $GLOBALS['TBE_TEMPLATE']->JScode = $GLOBALS['TBE_TEMPLATE']->wrapScriptTags('
00251                                 if (!window.opener)     {
00252                                         alert("ERROR: Sorry, no link to main window... Closing");
00253                                         close();
00254                                 }
00255                 ');
00256 
00257                 $this->content = $GLOBALS['TBE_TEMPLATE']->startPage($LANG->getLL('colorpicker_title'));
00258 
00259                         // URL for the inner main frame:
00260                 $url = 'wizard_colorpicker.php?showPicker=1'.
00261                                 '&colorValue='.rawurlencode($this->P['currentValue']).
00262                                 '&fieldName='.rawurlencode($this->P['itemName']).
00263                                 '&formName='.rawurlencode($this->P['formName']).
00264                                 '&exampleImg='.rawurlencode($this->P['exampleImg']).
00265                                 '&md5ID='.rawurlencode($this->P['md5ID']).
00266                                 '&fieldChangeFunc='.rawurlencode(serialize($this->P['fieldChangeFunc']));
00267 
00268                 $this->content.='
00269                         <frameset rows="*,1" framespacing="0" frameborder="0" border="0">
00270                                 <frame name="content" src="'.htmlspecialchars($url).'" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" noresize="noresize" />
00271                                 <frame name="menu" src="dummy.php" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" noresize="noresize" />
00272                         </frameset>
00273                 ';
00274 
00275                 $this->content.='
00276 </html>';
00277         }
00278 
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293         /************************************
00294          *
00295          * Rendering of various color selectors
00296          *
00297          ************************************/
00298 
00304         function colorMatrix()  {
00305                 global $LANG;
00306 
00307                 $steps = 51;
00308 
00309                         // Get colors:
00310                 $color = array();
00311 
00312                 for($rr=0;$rr<256;$rr+=$steps)  {
00313                         for($gg=0;$gg<256;$gg+=$steps)  {
00314                                 for($bb=0;$bb<256;$bb+=$steps)  {
00315                                         $color[] = '#'.
00316                                                 substr('0'.dechex($rr),-2).
00317                                                 substr('0'.dechex($gg),-2).
00318                                                 substr('0'.dechex($bb),-2);
00319                                 }
00320                         }
00321                 }
00322 
00323                         // Traverse colors:
00324                 $columns = 24;
00325 
00326                 $rows = 0;
00327                 $tRows = array();
00328                 while(isset($color[$columns*$rows]))    {
00329                         $tCells = array();
00330                         for($i=0;$i<$columns;$i++)      {
00331                                 $tCells[] = '
00332                                         <td bgcolor="'.$color[$columns*$rows+$i].'" onclick="document.colorform.colorValue.value = \''.$color[$columns*$rows+$i].'\'; document.colorform.submit();" title="'.$color[$columns*$rows+$i].'">&nbsp;&nbsp;</td>';
00333                         }
00334                         $tRows[] = '
00335                                 <tr>'.implode('',$tCells).'
00336                                 </tr>';
00337                         $rows++;
00338                 }
00339 
00340                 $table = '
00341                         <p class="c-head">'.$LANG->getLL('colorpicker_fromMatrix',1).'</p>
00342                         <table border="0" cellpadding="1" cellspacing="1" style="width:100%; border: 1px solid black; cursor:crosshair;">'.implode('',$tRows).'
00343                         </table>';
00344 
00345                 return $table;
00346         }
00347 
00353         function colorList()    {
00354                 global $LANG;
00355 
00356                         // Initialize variables:
00357                 $colors = explode(',',$this->HTMLcolorList);
00358                 $currentValue = strtolower($this->colorValue);
00359                 $opt = array();
00360                 $opt[] = '<option value=""></option>';
00361 
00362                         // Traverse colors, making option tags for selector box.
00363                 foreach($colors as $colorName)  {
00364                         $opt[] = '<option style="background-color: '.$colorName.';" value="'.htmlspecialchars($colorName).'"'.($currentValue==$colorName ? ' selected="selected"' : '').'>'.htmlspecialchars($colorName).'</option>';
00365                 }
00366 
00367                         // Compile selector box and return result:
00368                 $output = '
00369                         <p class="c-head">'.$LANG->getLL('colorpicker_fromList',1).'</p>
00370                         <select onchange="document.colorform.colorValue.value = this.options[this.selectedIndex].value; document.colorform.submit(); return false;">
00371                                 '.implode('
00372                                 ',$opt).'
00373                         </select><br/>';
00374 
00375                 return $output;
00376         }
00377 
00383         function colorImage()   {
00384                 global $LANG;
00385 
00386                         // Handling color-picker image if any:
00387                 if (!$this->imageError) {
00388                         if ($this->pickerImage) {
00389                                 if(t3lib_div::_POST('coords_x')) {
00390                                         $this->colorValue = '#'.$this->getIndex(t3lib_stdgraphic::imageCreateFromFile($this->pickerImage),t3lib_div::_POST('coords_x'),t3lib_div::_POST('coords_y'));
00391                                 }
00392                                 $pickerFormImage = '
00393                                 <p class="c-head">'.$LANG->getLL('colorpicker_fromImage',1).'</p>
00394                                 <input type="image" src="../'.substr($this->pickerImage,strlen(PATH_site)).'" name="coords" style="cursor:crosshair;" /><br />';
00395                         } else {
00396                                 $pickerFormImage = '';
00397                         }
00398                 } else {
00399                         $pickerFormImage = '
00400                         <p class="c-head">'.htmlspecialchars($this->imageError).'</p>';
00401                 }
00402 
00403                 return $pickerFormImage;
00404         }
00405 
00416         function getIndex($im,$x,$y) {
00417                 $rgb = ImageColorAt($im, $x, $y);
00418                 $colorrgb = imagecolorsforindex($im,$rgb);
00419                 $index['r'] = dechex($colorrgb['red']);
00420                 $index['g'] = dechex($colorrgb['green']);
00421                 $index['b'] = dechex($colorrgb['blue']);
00422                 foreach ($index as $value) {
00423                         if(strlen($value) == 1) {
00424                                 $hexvalue[] = strtoupper('0'.$value);
00425                         } else {
00426                                 $hexvalue[] = strtoupper($value);
00427                         }
00428                 }
00429                 $hex = implode('',$hexvalue);
00430                 return $hex;
00431         }
00432 }
00433 
00434 // Include extension?
00435 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php'])    {
00436         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php']);
00437 }
00438 
00439 
00440 
00441 
00442 
00443 
00444 
00445 
00446 
00447 
00448 
00449 
00450 // Make instance:
00451 $SOBE = t3lib_div::makeInstance('SC_wizard_colorpicker');
00452 $SOBE->init();
00453 $SOBE->main();
00454 $SOBE->printContent();
00455 ?>


Généré par L'expert TYPO3 avec  doxygen 1.4.6