Skip to content

Instantly share code, notes, and snippets.

@77web
Created December 16, 2011 05:34
Show Gist options
  • Save 77web/1484644 to your computer and use it in GitHub Desktop.
Save 77web/1484644 to your computer and use it in GitHub Desktop.
sfWidgetFormDoctrineHierSelect(jquery)
<?php
/**
* sfWidgetFormDoctrineHierSelect represents a hierarchical selects with jquery help.
*
* @auther Hiromi Hishida<[email protected]>
*/
class sfWidgetFormDoctrineHierSelect extends sfWidgetForm
{
/**
* Constructor.
*
* Available options:
*
* * parentModel: The parent model class(required)
* * childModel: The child model class(required)
* * filter_url The module/action path to filtering action(required)
* * relation_column: The name of column in child table(parent_model_id by default)
* * jquery_path: Path to jquery(jquery.js by default)
*/
public function configure($options = array(), $attributes = array())
{
$this->addRequiredOption('parentModel');
$this->addRequiredOption('childModel');
$this->addRequiredOption('filter_url');
$this->addOption('relation_column');
$this->addOption('jquery_path', 'jquery.js');
}
/**
* Renders the widget.
*
* @param string $name The element name
* @param string $value The value selected in this widget
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
* @param array $errors An array of errors for the field
*
* @return string An HTML tag string
*
* @see sfWidgetForm
*/
public function render($name, $value = null, $attributes = array(), $errors = array())
{
if(!$this->getOption('realtion_column'))
{
$this->setOption('relation_column', sfInflector::underscore($this->getOption('parentModel')).'_id');
}
if(null !== $value)
{
$value = array('child_id'=>is_array($value) ? (isset($value['child_id']) ? $value['child_id'] : null) : $value);
if(null !== $value['child_id'])
{
$object = Doctrine::getTable($this->getOption('childModel'))->find($value['child_id']);
if($object)
{
$value['parent_id'] = $object->get($this->getOption('relation_column'));
}
}
}
else
{
$value = array();
}
$value = array_merge(array('parent_id'=>null, 'child_id'=>null), $value);
$parentWidget = new sfWidgetFormDoctrineChoice(array('model'=>$this->getOption('parentModel'), 'add_empty'=>true));
$parentSelect = $parentWidget->render($name.'[parent_id]', $value['parent_id'], $attributes, $errors);
$childWidget = new sfWidgetFormDoctrineChoice(array('model'=>$this->getOption('childModel'), 'add_empty'=>true));
if(null != $value['parent_id'])
{
$query = Doctrine::getTable($this->getOption('childModel'))->createQuery('r')->addWhere(sprintf('r.%s = ?', $this->getOption('relation_column')), $value['parent_id']);
$childWidget->setOption('query', $query);
}
$childSelect = $childWidget->render($name.'[child_id]', $value['child_id'], $attributes, $errors);
sfContext::getInstance()->getResponse()->addJavascript($this->getOption('jquery_path'));
$script = '<script type="text/javascript">'
.'window.'.$this->generateId($name).'FilterUrl = "'.url_for($this->getOption('filter_url')).'";'
.'$(document).ready(function(){ $("#'.$this->generateId($name.'[parent_id]').'").change(function(){ $.get(window.'.$this->generateId($name).'FilterUrl, { id: $(this).val() }, function(data){ $("#'.$this->generateId($name.'[child_id]').'").empty().html(data); }); }); });'
.'</script>';
return $script.$parentSelect.' '.$childSelect;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment