My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) Jonathan H. Wage <jonwage@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* sfWidgetFormDoctrineJQueryChoiceAutocompleter represents a choice widget for a model that is optimized for large choice lists.
*
* @package symfony
* @subpackage doctrine
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class sfWidgetFormDoctrineJQueryChoiceAutocompleter extends sfWidgetFormDoctrineChoice
{
protected $value = array();

protected function configure($options = array(), $attributes = array())
{
$this->addRequiredOption('url');

parent::configure($options, $attributes);
}

public function getChoices()
{
if (empty($this->value)) {
return array();
}

$choices = array();
if (false !== $this->getOption('add_empty'))
{
$choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
}

if (null === $this->getOption('table_method'))
{
$query = null === $this->getOption('query') ? Doctrine_Core::getTable($this->getOption('model'))->createQuery() : $this->getOption('query');
if ($order = $this->getOption('order_by'))
{
$query->addOrderBy($order[0] . ' ' . $order[1]);
}
$query->whereIn($this->getOption('model').'.id', $this->value);
$objects = $query->execute();
}
else
{
$tableMethod = $this->getOption('table_method');
$results = Doctrine_Core::getTable($this->getOption('model'))->$tableMethod();
$results->whereIn($this->getOption('model').'.id', $this->value);

if ($results instanceof Doctrine_Query)
{
$objects = $results->execute();
}
else if ($results instanceof Doctrine_Collection)
{
$objects = $results;
}
else if ($results instanceof Doctrine_Record)
{
$objects = new Doctrine_Collection($this->getOption('model'));
$objects[] = $results;
}
else
{
$objects = array();
}
}

$method = $this->getOption('method');
$keyMethod = $this->getOption('key_method');
foreach ($objects as $object)
{
$choices[$object->$keyMethod()] = $object->$method();
}

return $choices;
}

public function render($name, $value = null, $attributes = array(), $errors = array())
{
$this->value = $value;

$html = '';
if ($this->getOption('multiple'))
{
$attributes['multiple'] = 'multiple';

if ('[]' != substr($name, -2))
{
$name .= '[]';
}
$html = '<br />'.parent::render($name, $value, $attributes, $errors);
$value = null;
}

$autocompleter = new sfWidgetFormDoctrineJQueryAutocompleter(
array(
'model' => $this->getOption('model'),
'url' => $this->getOption('url'),
)
);
$autocompleter = $autocompleter->render($name, $value);

if ($this->getOption('multiple'))
{
// fix trailing _ due to value being an empty string out of toString()
$autocompleter = str_replace(
'autocomplete_'.$this->generateId($name).'_',
'autocomplete_'.$this->generateId($name),
$autocompleter
);

// remove hidden
$autocompleter = preg_replace(
'/^[^>]+>(.*)/',
'$1',
$autocompleter
);

// adjust action on result selection
$autocompleter = str_replace(
'jQuery("#'.$this->generateId($name).'").val(data[1]);',
'if (data[1] > 0) { var dest = document.getElementById("'.$this->generateId($name).'"); dest.options[dest.length] = new Option(data[0], data[1]); }',
$autocompleter
);
}

return '<strong>Search</strong>: '.$autocompleter.$html;
}

/**
* Gets the stylesheet paths associated with the widget.
*
* @return array An array of stylesheet paths
*/
public function getStylesheets()
{
$stylesheets = parent::getStylesheets();
$stylesheets['/sfFormExtraPlugin/css/jquery.autocompleter.css'] = 'all';
return $stylesheets;
}

/**
* Gets the JavaScript paths associated with the widget.
*
* @return array An array of JavaScript paths
*/
public function getJavascripts()
{
$javascripts = parent::getJavascripts();
$javascripts[] = '/sfFormExtraPlugin/js/jquery.autocompleter.js';
return $javascripts;
}
}

Change log

r516 by lukas.smith on May 25, 2010   Diff
tweaked phpdoc
Go to: 
Project members, sign in to write a code review

Older revisions

r511 by lukas.smith on May 25, 2010   Diff
minor tweak
r507 by lukas.smith on May 25, 2010   Diff
added a new (multi) select doctrine
widget that loads only the current
selected items + autocompleter for new
stuff
All revisions of this file

File info

Size: 4843 bytes, 164 lines
Powered by Google Project Hosting