SpiffyJr's Blogaroo

Happenings of the man known as SpiffyJr

  • Home
  • About
    • Inspiration
    • Resume
  • Projects
    • Blitzaroo
    • phpRaider
    • Zend Calendar
  • Docs
    • Blitzaroo API
    • SpiffyCalendar Docs
    • SpiffyDb Docs
  • Downloads
  • Technologies
    • Doctrine ORM
    • Dojo
    • PHP
    • Zend Framework
  • Other
    • License
Twitter RSS

Zend_Dojo_View_Helper_Dialog

Posted on March 12, 2010 by SpiffyJr
3 commentsLeave a comment

Quickie

I’m going to make this one short & sweet. I’ve created a view helper for Dialogs (and an extended Dijit view helper).

Zend_Dojo_View_Helper_Dijit_Extended (wtb: namespaces)

<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Dojo_View_Helper_Dijit_Extended
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 * Zend_Calendar
 *
 * @category   Zend
 * @package    Zend_Dojo_View_Helper_Dijit_Extended
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Dojo_View_Helper_Dijit_Extended extends Zend_Dojo_View_Helper_Dijit
{
	/**
	 * Holds the base path for scripts.
	 */
	protected static $_scriptBase = null;

	/**
	 * Dojo for static methods.
	 */
	protected static $_dojo = null;

	/**
	 * View for static methods.
	 */
	protected static $_view = null;

	/**
	 * Static setting for script base.
	 * @param string $scriptBase Path to the base script directory.
	 */
	public static function setScriptBase($scriptBase)
	{
		self::$_scriptBase = $scriptBase;
	}

	/**
	 * Static getter for script base.
	 * @return string
	 */
	public static function getScriptBase()
	{
		return self::$_scriptBase;
	}

	/**
	 * Adds a stylesheet based on the dojo path (local or cdn).
	 * Method is static for situations where you need the stylesheets
	 * to render even if even the dijits haven't been called but
	 * will during execution (AJAX calls).
	 *
	 * @param string $stylesheet Path to stylesheet from base path.
	 */
	public static function addStylesheet($path)
	{
		// Set static view if necessary
		if (null === self::$_view) {
			$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
			self::$_view = $viewRenderer->view;
			self::$_dojo = self::$_view->dojo();
		}

		// Determine path to use
		if (null === self::getScriptBase()) {
			if (self::$_dojo->useLocalPath()) {
				self::setScriptBase(self::$_dojo->getLocalPath());
			} else {
				self::setScriptBase(self::$_dojo->getCdnBase() . self::$_dojo->getCdnVersion());
			}
		}

		self::$_dojo->addStylesheet(self::getScriptBase() . $path);
	}
}

Zend_Dojo_View_Helper_Dialog

Pretty simple view helper that renders a dialog using the regular (dijit.Dialog) method or the dojox (dojox.widget.Dialog) method.

Usage

// Regular dialog
<?=$this->dialog('myDialog', array('title' => 'w00t!', 'content' => 'I am a w00tastic dialog'));?>

// Enhanced dojox dialog
<?=$this->dialog('myDialog', array('title' => 'w00t!', 'content' => 'I am a w00tastic dialog'), array('useDojox' => true);?>

Code

<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Dojo_View_Helper_Dialog
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 * Zend_Calendar
 *
 * @category   Zend
 * @package    Zend_Dojo_View_Helper_Dialog
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Dojo_View_Helper_Dialog extends Zend_Dojo_View_Helper_Dijit_Extended
{
	const DIALOG_DOJO = 'dijit.Dialog';
	const DIALOG_DOJOX = 'dojox.widget.Dialog';

	/**
	 * Holds the default dialog type.
	 */
	public static $_dialogType = self::DIALOG_DOJO;

	/**
	 * Sets the default dialog type to use.
	 *
	 * @param string $dojoType Type of dijit to use.
	 */
	public static function setDialogType($dojoType)
	{
		switch ($dojoType) {
			case self::DIALOG_DOJOX:
				self::$_dialogType = $dojoType;
				break;
			default:
				self::$_dialogType = self::DIALOG_DOJO;
				break;
		}
	}

	/**
	 * DataStore view helper.
	 *
	 * @param string $id JavaScript id for the dialog.
	 * @param array $attribs Attributes for the dialog.
	 * @param array $options Options for the dialog.
	 */
	public function dialog($id = '', array $attribs = array(), array $options = array())
	{
		if (!$id) {
			throw new Zend_Exception('Invalid arguments: required jsId.');
		}

		// Determine dialog type
		$dialogType = self::$_dialogType;
		if (array_key_exists('useDojox', $options)) {
			$dialogType = self::DIALOG_DOJOX;
		}

		// Require module
		$this->dojo->requireModule($dialogType);

		// Add styles
		if ($dialogType == self::DIALOG_DOJOX) {
			self::addStylesheet('/dojox/widget/Dialog/Dialog.css');
		}

		// Programmatic
		if ($this->_useProgrammatic()) {
			if (!$this->_useProgrammaticNoScript()) {
				$this->dojo->addJavascript('var ' . $id . ";\n");
				$js = $id . ' = ' . 'new ' . $dialogType . '(' . Zend_Json::encode($attribs) . ");";
				$this->dojo->_addZendLoad("function(){{$js}}");
			}
			return '';
		}

		// Set extra attribs for declarative
		if (!array_key_exists('id', $attribs)) {
			$attribs['id'] = $id;
		}

		if (!array_key_exists('jsId', $attribs)) {
			$attribs['jsId'] = $id;
		}

		if (!array_key_exists('dojoType', $attribs)) {
			$attribs['dojoType'] = $dialogType;
		}

		if (array_key_exists('content', $attribs)) {
			$content = $attribs['content'];
			unset($attribs['content']);
		}

		return '<div' . $this->_htmlAttribs($attribs) . '>' . $content . "</div>\n";
	}
}

Hope it makes life a little easier. Enjoy!

Reblog this post [with Zemanta]
GD Star Rating
loading...
Categories: Random | Tags: Ajax, BSD licenses, JavaScript, Languages, php, Programming, Source code, Zend Framework

About SpiffyJr

View all posts by SpiffyJr→
Notice: This work is licensed under a BY-NC-SA. Permalink: Zend_Dojo_View_Helper_Dialog
Updated Zend_Dojo_View_Helper_Dialog
dataStore and dataGrid view helpers.

3 Responses to “Zend_Dojo_View_Helper_Dialog”

  1. Zend Framework News » Blog Archive » View Helper für Dojo Dialoge says:
    March 15, 2010 at 3:34 pm

    [...] http://www.spiffyjr.me/2010/03/12/zend_dojo_view_helper_dialog/ [...]

  2. Updated Zend_Dojo_View_Helper_Dialog | SpiffyJr's Blogaroo says:
    March 15, 2010 at 4:30 pm

    [...] Zend_Dojo_View_Helper_Dialog Quickie I'm going to make this one short & sweet. I've created a view helper for Dialogs (and an extended Dijit view helper). Zend_Dojo_View_Helper_Dijit_Extended (wtb: namespaces) <?php /** … [...]

  3. Updated Zend_Dojo_View_Helper_Dialog -HackIX says:
    March 21, 2010 at 7:25 am

    [...] For those of you that have no idea what I’m talk­ing about you should read Zend_Dojo_View_Helper_Dialog [...]

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*


question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Recent Posts

    • Keep your Git fork clean
    • Get started with Zend Framework 2 modules!
    • More Doctrine 2 and Zend Framework integration goodies!
    • Super sexy URLs with ZF and the joy of controller plugins!
    • Formatting your API to work with dojox.data.JsonRestStore (#dojo)
  • Categories

    • Other
      • Random
    • Projects
      • Blitzaroo
      • phpRaider
      • SpiffyDb
      • View Helpers
      • Zend Calendar
    • SpiffyJr
    • Technologies
      • Dojo
      • PHP
      • Zend Framework
  • Tag Cloud

      Ajax Algorithm api Blitzaroo Blog BSD licenses calendar Cascading Style Sheets CSS database Data Formats Dojo Dojo Toolkit event Framework game Google HTML HTML element JavaScript JQuery json Languages mapper Marketing mmorpg model php Programming Projects Scripts Source code Style sheet Style Sheets Twitter Website zend Zend Framework
  • Archives

    • December 2011
    • November 2011
    • July 2011
    • April 2011
    • March 2011
    • December 2010
    • November 2010
    • October 2010
    • March 2010
    • February 2010
    • November 2009
    • October 2009
    • September 2009
© SpiffyJr's Blogaroo. Proudly Powered by WordPress | Nest Theme by YChong