Location: PHPKode > projects > Aukyla Platform > aukyla/base/JavaScript.php
<?php
/*
     JavaScript.php, this module provides functions to generate handy
                     JavaScripts
     Copyright (C) 2004 Arend van Beelen, Auton Rijnsburg

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the Free
     Software Foundation; either version 2 of the License, or (at your option)
     any later version.

     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, write to the Free Software Foundation, Inc.,
     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

require_once('Locale.php');
require_once('Output.php');
require_once('Widgets.php');

/**
 * @brief Used to add JavaScript code.
 *
 * This widget shows a JavaScript expression within a proper script block.
 *
 * You can use the static utility functions checkVariables() and
 * checkFormFunction() to generate simple JavaScript checks.
 */
class JavaScript extends Widget
{
	/**
	 * Constructor.
	 *
	 * @param parent     The parent widget to add this JavaScript block to.
	 * @param expression The JavaScript expression put in a proper script block.
	 */
	public function __construct(Container $parent, $expression)
	{
		parent::__construct($parent);
		$this->expression = $expression;
	}

	public function show()
	{
		Output::write("<script type=\"application/x-javascript\"".$this->parentProperties().'>'.
		              "<![CDATA[{$this->expression}]]>".
		              '</script>');
	}

	/**
	 * This function returns the JavaScript code that can be used inside an
	 * @p if-statement to test whether a variable has been filled in.
	 *
	 * @param variables A variabele name, or an array of variable names that will
	 *                  be checked to be empty.
	 * @return The generated JavaScript statement which checks said the given
	 *         variables.
	 */
	public static function checkVariables($variables)
	{
		if(is_array($variables))
		{
			$checkVariables = '';
			foreach($variables as $variable)
			{
				if($checkVariables != '')
				{
					$checkVariables .= ' || ';
				}
				$checkVariables .= "document.getElementById('$variable').value == ''";
			}
			return $checkVariables;
		}
		else
		{
			return "document.getElementById('$variables').value == ''";
		}
	}

	/**
	 * This function returns a JavaScript that checks whether a given expression
	 * evaluates to true. If the expression is true, the given error message is
	 * displayed and the JavaScript function returns @p false. To make use of this
	 * script add @p onclick="return @p checkForm$form()" to the button that
	 * submits the specified form.
	 *
	 * @param expressions  An array consisting of @p expression => @p error pairs
	 *                     where @p expression is the expression that will be
	 *                     checked and @p error is the error message to be shown
	 *                     if the expression is true.
	 * @param form         Only used to append a form name to the end of the
	 *                     function that is created, so multiple scripts can be
	 *                     inserted.
	 * @return The generated JavaScript function which checks the specified form.
	 */
	public static function checkFormFunction($expressions, $form = '')
	{
		$function = "function checkForm$form()".
		            "{";
		foreach($expressions as $expression => $error)
		{
			if($error == '')
			{
				$error = i18n('Please fill in all required fields.');
			}
			$function .= "if($expression)".
			             "{".
			                 "alert('$error');".
			                 "return false;".
			             "}";
		}
		$function .=     "return true;".
		             "}";

		return $function;
	}

	private $expression;
}

/**
 * @brief Links to an external JavaScript block.
 *
 * Use this widget if you want to insert a reference to an external JavaScript
 * file.
 */
class ExternalJavaScript extends Widget
{
	/**
	 * Constructor.
	 *
	 * @param parent Parent to add the JavaScript reference to.
	 * @param url    URL to the external script.
	 */
	public function __construct(Container $parent, $url)
	{
		parent::__construct($parent);
		$this->url = $url;
	}

	public function show()
	{
		Output::write("<script type=\"application/x-javascript\" src=\"{$this->url}\"".$this->parentProperties().' />');
	}

	private $url;
}

?>
Return current item: Aukyla Platform