<?php

/*
 * phpMyEdit - instant MySQL table editor and code generator
 *
 * extensions/phpMyEdit-calpopup.class.php - phpMyEdit calendar popup extension
 * ____________________________________________________________
 *
 * Contribution of Adam Hammond <php@pixolet.co.uk>, London, UK
 *
 * Copyright (c) 2003 Platon SDG, http://platon.sk/
 * All rights reserved.
 *
 * See README file for more information about this software.
 * See COPYING file for license information.
 *
 * Download the latest version from
 * http://platon.sk/projects/phpMyEdit/
 */
 
/*
    OVERVIEW
    --------

	calPopup extends the standard phpMyEdit class to allow
	a calendar popup helper to be put on any text field. This
	extension uses the free jsCalendar code from:
	http://dynarch.com/mishoo/calendar.epl
	
    REQUIREMENTS
    ------------

    The extension requires a properly installed jsCalendar script. All browsers
    supported by jsCalendar are supported by this extension.

    USAGE
    -----

    This extension enables the display of a popup calendar selection against selected fields.
	
    In order to use it, you should:

    1. Load the jsCalendar scripts in the <head>...</head> section of your
       phpMyEdit calling program, substituting the correct paths:

		<script type="text/javascript" src="js/calendar.js"></script>
		<script type="text/javascript" src="js/lang/calendar-en.js"></script>
		<script type="text/javascript" src="js/calendar-setup.js"></script>

	2.  Choose your preferred jsCalendar css file (see jsCalendar documentation)
	    and add the following in the <head>...</head> section of your phpMyEdit
	    calling program, substituting the correct path:
	    
         <LINK REL=StyleSheet HREF="css/calendar-system.css" TYPE="text/css" MEDIA=screen>

       NOTE: To avoid an unwanted side effect in the css style produced
       by phpMyEditSetup.php, add a 'width:auto' property into the
       '.calendar table' entry in your selected jsCalendar style sheet.

    2. Call to phpMyEdit-calPopup.class.php instead
       of phpMyEdit.class.php.

       Example:

        require_once 'extensions/phpMyEdit-calpopup.class.php';
        new phpMyEdit-calpopup($opts);

    3. Add 'calendar' parameter to the field definitions where you want a calendar
        popup in your phpMyEdit calling program.

       Example:

        $opts['fdd']['body'] = array(
          'name'     => 'Content',
          'select'   => 'T',
          'options'  => 'ACPVD',
          'maxlen'   => 65535,
          'required' => true,
          'calendar' => true
        );

		This is will display a button next to the field which pops up a calendar when
		clicked. If that field has a 'strftimemask' parameter set, it will use this
		for the date format.
		
		For more advanced usage, you can set the 'calendar' parameter to an array of
		valid jsCalendar Calendar.setup options (see jSCalendar document for details).
		Note that not all of these options make sense to use in phpMyEdit, and some of
		them will actively break the function.
		
		Example:
		
        $opts['fdd']['body'] = array(
          'name'     => 'Content',
          'select'   => 'T',
          'options'  => 'ACPVD',
          'maxlen'   => 65535,
          'required' => true,
          'calendar' => array(
            'ifFormat' => '%Y/%m/%d',   // defaults to the value in 'strftimemask'
            'singleClick' => true,      // single or double click to close
            'mondayFirst' => true,      // Sunday or Monday first
            'weekNumbers' => true,      // Show week numbers
			'showsTime'   => false,     // Show time as well as date
			'timeFormat'  => '24',      // 12 or 24 hour clock
			'date'        => '2003-12-19 10:00'
			                            // Initial date/time for popup (see note below)
			)
        );

		NOTES:
		1. The popup will normally set the initial value to the current field value
		or to current date/time. 'date' will always override this, even if there is a
		current date/time value in the field. If you want a default value only if the field
		is currently empty, use the phpMyEdit 'default' option.
		
		2. There are a number of Calendar.setup options that most not be overriden as these
		are set by the class to link to fields. Espcially avoid using 'inputField' and 'button'.
		It is best to stick with the options listed in the example.

*/


require_once dirname(__FILE__).'/../phpMyEdit.class.php';

class phpMyEdit_calpopup extends phpMyEdit
{
	/* CalPopup mod start */
	var $calendars;     /* Array for collecting list of fields with calendar popups */
	/* CalPopup mod end */
	
	
	function display_add_record() /* {{{ */
	{
		for ($tab = 0, $k = 0; $k < $this->num_fds; $k++) {
			if (isset($this->fdd[$k]['tab']) && $this->tabs_enabled() && $k > 0) {
				$tab++;
				echo '</table>',"\n";
				echo '</div>',"\n";
				echo '<div id="phpMyEdit_tab',$tab,'">',"\n";
				echo '<table class="',$this->getCSSclass('main'),'" summary="',$this->tb,'">',"\n";
			}
			if (! $this->displayed[$k]) {
				continue;
			}
			if ($this->hidden($k)) {
				echo $this->htmlHidden($this->fds[$k], $row["qf$k"]);
				continue;
			}
			$css_postfix    = @$this->fdd[$k]['css']['postfix'];
			$css_class_name = $this->getCSSclass('input', null, 'next', $css_postfix);
			echo '<tr class="',$this->getCSSclass('row', null, true, $css_postfix),'">',"\n";
			echo '<td class="',$this->getCSSclass('key', null, true, $css_postfix),'">',$this->fdd[$k]['name'],'</td>',"\n";
			echo '<td class="',$this->getCSSclass('value', null, true, $css_postfix),'"';
			echo $this->getColAttributes($k),">\n";
			if ($this->col_has_values($k)) {
				$vals       = $this->set_values($k);
				$selected   = @$this->fdd[$k]['default'];
				$multiple   = $this->col_has_multiple_select($k);
				$readonly   = $this->readonly($k);
				$strip_tags = true;
				$escape     = true;
				echo $this->htmlSelect($this->fds[$k], $css_class_name, $vals, $selected,
						$multiple, $readonly, $strip_tags, $escape);
			} elseif (isset ($this->fdd[$k]['textarea'])) {
				echo '<textarea class="',$css_class_name,'" name="',$this->fds[$k],'"';
				echo ($this->readonly($k) ? ' disabled' : '');
				if (intval($this->fdd[$k]['textarea']['rows']) > 0) {
					echo ' rows="',$this->fdd[$k]['textarea']['rows'],'"';
				}
				if (intval($this->fdd[$k]['textarea']['cols']) > 0) {
					echo ' cols="',$this->fdd[$k]['textarea']['cols'],'"';
				}
				if (isset($this->fdd[$k]['textarea']['wrap'])) {
					echo ' wrap="',$this->fdd[$k]['textarea']['wrap'],'"';
				} else {
					echo ' wrap="virtual"';
				}
				echo '>',htmlspecialchars($this->fdd[$k]['default']),'</textarea>',"\n";
			} else {
				// Simple edit box required
				$size_ml_props = '';
				$maxlen = intval($this->fdd[$k]['maxlen']);
				//$maxlen > 0 || $maxlen = 300;
				$size   = min($maxlen, 60);
				$size   && $size_ml_props .= ' size="'.$size.'"';
				$maxlen && $size_ml_props .= ' maxlength="'.$maxlen.'"';
				echo '<input class="',$css_class_name,'" type="text" ';
				echo ($this->readonly($k) ? 'disabled ' : ''),' id="',$this->fds[$k],'" name="',$this->fds[$k],'"';
				echo $size_ml_props,' value="';
				echo htmlspecialchars($this->fdd[$k]['default']),'">';

                /* CalPopup mod start */
				$this->cal_helper($k, null);          /* Call CalPopup helper function */
				/* CalPopup mod end */
				
    		}
			echo '</td>',"\n";
			if ($this->guidance) {
				$css_class_name = $this->getCSSclass('help', null, true, $css_postfix);
				$cell_value     = $this->fdd[$k]['help'] ? $this->fdd[$k]['help'] : '&nbsp;';
				echo '<td class="',$css_class_name,'">',$cell_value,'</td>',"\n";
			}
			echo '</tr>',"\n";
		}
	} /* }}} */

	function display_change_field($row, $k) /* {{{ */
	{
		$css_postfix    = @$this->fdd[$k]['css']['postfix'];
		$css_class_name = $this->getCSSclass('input', null, true, $css_postfix);
		echo '<td class="',$this->getCSSclass('value', null, true, $css_postfix),'"';
		echo $this->getColAttributes($k),">\n";
		if ($this->col_has_values($k)) {
			$vals       = $this->set_values($k);
			$multiple   = $this->col_has_multiple_select($k);
			$readonly   = $this->readonly($k);
			$strip_tags = true;
			$escape     = true;
			echo $this->htmlSelect($this->fds[$k], $css_class_name, $vals, $row["qf$k"],
					$multiple, $readonly, $strip_tags, $escape);
		} elseif (isset($this->fdd[$k]['textarea'])) {
			echo '<textarea class="',$css_class_name,'" name="',$this->fds[$k],'"';
			echo ($this->readonly($k) ? ' disabled' : '');
			if (intval($this->fdd[$k]['textarea']['rows']) > 0) {
				echo ' rows="',$this->fdd[$k]['textarea']['rows'],'"';
			}
			if (intval($this->fdd[$k]['textarea']['cols']) > 0) {
				echo ' cols="',$this->fdd[$k]['textarea']['cols'],'"';
			}
			if (isset($this->fdd[$k]['textarea']['wrap'])) {
				echo ' wrap="',$this->fdd[$k]['textarea']['wrap'],'"';
			} else {
				echo ' wrap="virtual"';
			}
			echo '>',htmlspecialchars($row["qf$k"]),'</textarea>',"\n";
		} else {
			$size_ml_props = '';
			$maxlen = intval($this->fdd[$k]['maxlen']);
			//$maxlen > 0 || $maxlen = 300;
			$size   = min($maxlen, 60);
			$size   && $size_ml_props .= ' size="'.$size.'"';
			$maxlen && $size_ml_props .= ' maxlength="'.$maxlen.'"';
			echo '<input class="',$css_class_name,'" type="text" ';
			echo ($this->readonly($k) ? 'disabled ' : ''),'name="',$this->fds[$k],'" value="';
			echo htmlspecialchars($row["qf$k"]),'" ',$size_ml_props,'>',"\n";

            /* CalPopup mod start */
			$this->cal_helper($k, htmlspecialchars($row["qf$k"]));           /* Call CalPopup helper function */
			/* CalPopup mod end */
		}
		echo '</td>',"\n";
	} /* }}} */


	/* CalPopup mod start */
	
	/*
	$k : current field name
	$curval : current value of field (set to null for default)
	                    
	cal_helper checks to see if the calendar parameter is set on the field. If so it displays the button.
	It then pushes the Calendar.setup parameters into an array, including the user specified ones in the calling
	program is they exist. This array is then added to the $calendars array indexed by the field name. This allows
	for multiple fields with calendar popups.
	*/
	
	function cal_helper($k, $curval) /* {{{ */
	{
 		if (isset($this->fdd[$k]['calendar'])) {
 			echo '<button id="tr_',$this->fds[$k],'">...</button>';
			$tmp_arr['button'] = 'tr_'.$this->fds[$k];
			$tmp_arr['ifFormat'] = '%Y-%m-%d %H:%M';
			$tmp_arr['showsTime'] = true;
			$tmp_arr['singleClick'] = false;
			if (isset($curval))
				if (substr($curval, 0, 4) != '0000')
					$tmp_arr['date'] = $curval;
   			if (isset($this->fdd[$k]['strftimemask']))
			    $tmp_arr['ifFormat'] = $this->fdd[$k]['strftimemask'];
			if (is_array($this->fdd[$k]['calendar'])) {
				foreach($this->fdd[$k]['calendar'] as $ck => $cv) {
					$tmp_arr[$ck] = $cv;
				}
			}
			$this->calendars[$this->fds[$k]] = $tmp_arr;
		}
	} /* }}} */

	/* CalPopup mod end */

	function display_record() /* {{{ */
	{
		$this->create_javascripts();
		if ($this->cgi['persist'] != '') {
			echo $this->get_origvars_html($this->cgi['persist']);
		}
		echo $this->get_origvars_html($this->get_sfn_cgi_vars());
		echo $this->get_origvars_html($this->qfn);
		echo '<input type="hidden" name="qfn" value="',htmlspecialchars($this->qfn),'">',"\n";
		echo '<input type="hidden" name="rec" value="',($this->copy_operation()?'':$this->rec),'">',"\n";
		echo '<input type="hidden" name="fm" value="',$this->fm,'">',"\n";
		echo '<input type="hidden" name="fl" value="',$this->fl,'">',"\n";
		if ($this->nav_up()) {
			$this->display_record_buttons();
			echo '<hr class="',$this->getCSSclass('hr', 'up'),'">',"\n";
			if ($this->tabs_enabled()) {
				$this->display_tab_labels('up');
			}
		}
		if ($this->tabs_enabled()) {
			echo '<div id="phpMyEdit_tab0">',"\n";
		}
		echo '<table class="',$this->getCSSclass('main'),'" summary="',$this->tb,'">',"\n";
		if ($this->add_operation()) {
			$this->display_add_record();
		} else {
			$this->display_copy_change_delete_record();
		}
		echo '</table>',"\n";
		echo '</div>',"\n";
		if ($this->nav_down()) {
			if ($this->tabs_enabled()) {
				$this->display_tab_labels('down');
			}
			echo '<hr class="',$this->getCSSclass('hr', 'down'),'">',"\n";
			$this->display_record_buttons();
		}
		if ($this->display['form']) {
			echo '</form>',"\n";
			
			/* CalPopup mod start */
			
			/* Add script calls to the end of the form for all fields with calendar
			popups. */
			if (isset($this->calendars)) {
				foreach($this->calendars as $ck => $cv) {
					echo '<script type="text/javascript">',"\n";
  					echo 'Calendar.setup(',"\n";
    				echo '{',"\n";
      				foreach ($cv as $ck1 => $cv1)
      			    	echo $ck1, ' : "',$cv1,'",',"\n";
                	echo 'inputField  : "',$ck,'"',"\n";
    				echo '}',"\n";
  					echo ');',"\n";
					echo '</script>',"\n";
				};
			};
			/* CalPopup mod end */
			
		};
	} /* }}} */
}
?>


