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                 }
00226         }
00227 
00233         function printContent() {
00234                 $this->content.= $this->doc->endPage();
00235                 $this->content = $this->doc->insertStylesAndJS($this->content);
00236                 echo $this->content;
00237         }
00238 
00246         function frameSet() {
00247                 global $LANG;
00248 
00249                         // Set doktype:
00250                 $GLOBALS['TBE_TEMPLATE']->docType = 'xhtml_frames';
00251                 $GLOBALS['TBE_TEMPLATE']->JScode = $GLOBALS['TBE_TEMPLATE']->wrapScriptTags('
00252                                 if (!window.opener)     {
00253                                         alert("ERROR: Sorry, no link to main window... Closing");
00254                                         close();
00255                                 }
00256                 ');
00257 
00258                 $this->content = $GLOBALS['TBE_TEMPLATE']->startPage($LANG->getLL('colorpicker_title'));
00259 
00260                         // URL for the inner main frame:
00261                 $url = 'wizard_colorpicker.php?showPicker=1'.
00262                                 '&colorValue='.rawurlencode($this->P['currentValue']).
00263                                 '&fieldName='.rawurlencode($this->P['itemName']).
00264                                 '&formName='.rawurlencode($this->P['formName']).
00265                                 '&exampleImg='.rawurlencode($this->P['exampleImg']).
00266                                 '&md5ID='.rawurlencode($this->P['md5ID']).
00267                                 '&fieldChangeFunc='.rawurlencode(serialize($this->P['fieldChangeFunc']));
00268 
00269                 $this->content.='
00270                         <frameset rows="*,1" framespacing="0" frameborder="0" border="0">
00271                                 <frame name="content" src="'.htmlspecialchars($url).'" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" noresize="noresize" />
00272                                 <frame name="menu" src="dummy.php" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" noresize="noresize" />
00273                         </frameset>
00274                 ';
00275 
00276                 $this->content.='
00277 </html>';
00278         }
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294         /************************************
00295          *
00296          * Rendering of various color selectors
00297          *
00298          ************************************/
00299 
00305         function colorMatrix()  {
00306                 global $LANG;
00307 
00308                 $steps = 51;
00309 
00310                         // Get colors:
00311                 $color = array();
00312 
00313                 for($rr=0;$rr<256;$rr+=$steps)  {
00314                         for($gg=0;$gg<256;$gg+=$steps)  {
00315                                 for($bb=0;$bb<256;$bb+=$steps)  {
00316                                         $color[] = '#'.
00317                                                 substr('0'.dechex($rr),-2).
00318                                                 substr('0'.dechex($gg),-2).
00319                                                 substr('0'.dechex($bb),-2);
00320                                 }
00321                         }
00322                 }
00323 
00324                         // Traverse colors:
00325                 $columns = 24;
00326 
00327                 $rows = 0;
00328                 $tRows = array();
00329                 while(isset($color[$columns*$rows]))    {
00330                         $tCells = array();
00331                         for($i=0;$i<$columns;$i++)      {
00332                                 $tCells[] = '
00333                                         <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>';
00334                         }
00335                         $tRows[] = '
00336                                 <tr>'.implode('',$tCells).'
00337                                 </tr>';
00338                         $rows++;
00339                 }
00340 
00341                 $table = '
00342                         <p class="c-head">'.$LANG->getLL('colorpicker_fromMatrix',1).'</p>
00343                         <table border="0" cellpadding="1" cellspacing="1" style="width:100%; border: 1px solid black; cursor:crosshair;">'.implode('',$tRows).'
00344                         </table>';
00345 
00346                 return $table;
00347         }
00348 
00354         function colorList()    {
00355                 global $LANG;
00356 
00357                         // Initialize variables:
00358                 $colors = explode(',',$this->HTMLcolorList);
00359                 $currentValue = strtolower($this->colorValue);
00360                 $opt = array();
00361                 $opt[] = '<option value=""></option>';
00362 
00363                         // Traverse colors, making option tags for selector box.
00364                 foreach($colors as $colorName)  {
00365                         $opt[] = '<option style="background-color: '.$colorName.';" value="'.htmlspecialchars($colorName).'"'.($currentValue==$colorName ? ' selected="selected"' : '').'>'.htmlspecialchars($colorName).'</option>';
00366                 }
00367 
00368                         // Compile selector box and return result:
00369                 $output = '
00370                         <p class="c-head">'.$LANG->getLL('colorpicker_fromList',1).'</p>
00371                         <select onchange="document.colorform.colorValue.value = this.options[this.selectedIndex].value; document.colorform.submit(); return false;">
00372                                 '.implode('
00373                                 ',$opt).'
00374                         </select><br/>';
00375 
00376                 return $output;
00377         }
00378 
00384         function colorImage()   {
00385                 global $LANG;
00386 
00387                         // Handling color-picker image if any:
00388                 if (!$this->imageError) {
00389                         if ($this->pickerImage) {
00390                                 if(t3lib_div::_POST('coords_x')) {
00391                                         $this->colorValue = '#'.$this->getIndex(t3lib_stdgraphic::imageCreateFromFile($this->pickerImage),t3lib_div::_POST('coords_x'),t3lib_div::_POST('coords_y'));
00392                                 }
00393                                 $pickerFormImage = '
00394                                 <p class="c-head">'.$LANG->getLL('colorpicker_fromImage',1).'</p>
00395                                 <input type="image" src="../'.substr($this->pickerImage,strlen(PATH_site)).'" name="coords" style="cursor:crosshair;" /><br />';
00396                         } else {
00397                                 $pickerFormImage = '';
00398                         }
00399                 } else {
00400                         $pickerFormImage = '
00401                         <p class="c-head">'.htmlspecialchars($this->imageError).'</p>';
00402                 }
00403 
00404                 return $pickerFormImage;
00405         }
00406 
00417         function getIndex($im,$x,$y) {
00418                 $rgb = ImageColorAt($im, $x, $y);
00419                 $colorrgb = imagecolorsforindex($im,$rgb);
00420                 $index['r'] = dechex($colorrgb['red']);
00421                 $index['g'] = dechex($colorrgb['green']);
00422                 $index['b'] = dechex($colorrgb['blue']);
00423                 foreach ($index as $value) {
00424                         if(strlen($value) == 1) {
00425                                 $hexvalue[] = strtoupper('0'.$value);
00426                         } else {
00427                                 $hexvalue[] = strtoupper($value);
00428                         }
00429                 }
00430                 $hex = implode('',$hexvalue);
00431                 return $hex;
00432         }
00433 }
00434 
00435 // Include extension?
00436 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php'])    {
00437         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php']);
00438 }
00439 
00440 
00441 
00442 
00443 
00444 
00445 
00446 
00447 
00448 
00449 
00450 
00451 // Make instance:
00452 $SOBE = t3lib_div::makeInstance('SC_wizard_colorpicker');
00453 $SOBE->init();
00454 $SOBE->main();
00455 $SOBE->printContent();
00456 ?>


Généré par Les experts TYPO3 avec  doxygen 1.4.6