Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Adapter/Doctrine/Element/Suggest.php
<?php
/**
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the new BSD license.
 *
 * @package     Ace
 * @license     http://www.opensource.org/licenses/BSD-3-Clause New BSD license
 * @since       1.0
 */
class Ace_Form_Adapter_Doctrine_Element_Suggest extends Ace_Form_Element_Input_Autocomplete
{
    /**
     * Configure the element.
     *
     * Available options:
     *
     *  * model:            The model class (required)
     *  * url:              The url of the ajax action (required)
     *  * method:           The method to use to display object values
     *
     * @see Ace_Element
     */
    protected function configure($options = array(), $attributes = array())
    {
        $this->addRequiredOption('url');
        $this->addRequiredOption('model');

        $this->addOption('id_field', null);
        $this->addOption('method', '__toString');

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

    /**
     * Render the element
     *
     * @see Ace_Form_Element
     */
    public function render($name, $value = null, $attributes = array(), $errors = array())
    {
        if(is_null($this->getOption('id_field')))
        {
            $element_hidden = new Ace_Form_Element_Input_Hidden();

            return $element_hidden->render($name, $value, $attributes, array()) .
                parent::render(preg_replace('/\]$/', '_search]', $name), $this->getValue($value), $attributes, $errors);
        }
        else
        {
            return parent::render($name, $this->getValue($value), $attributes, $errors);
        }
    }

    /**
     * Gets a value from a Doctrine_Record
     *
     * @param null|string $value
     * @return string
     */
    public function getValue($value = null)
    {
        if(!empty($value))
        {
            $obj = Doctrine::getTable($this->getOption('model'))
                ->createQuery()
                ->addWhere(Doctrine::getTable($this->getOption('model'))->getIdentifier() . ' = ?', $value)
                ->execute()
                ->getFirst();

            if($obj)
            {
                $method = $this->getOption('method');

                if(method_exists($obj, $method))
                {
                    return $obj->$method();
                }
                else
                {
                    throw new Ace_Form_Exception(sprintf('The method "%s" doesn\'t exists for the current object', $method));
                }
            }
            else
            {
                return $value;
            }
        }
        else
        {
            return $value;
        }
    }

    /**
     * Returns the autocomplete javascript
     *
     * @param  string $name
     * @return string
     */
    protected function getAutocompleteJavascript($name)
    {
        if(is_null($this->getOption('id_field')))
        {
            $id_field = preg_replace('/_search\]$/', ']', $name);
        }
        else
        {
            $id_field = preg_replace('/\[([a-zA-Z0-9_-]*)]$/', '[' . $this->getOption('id_field') . ']', $name);
        }

        $options = array();

        $options['minLength'] = $this->getOption('min_chars');
        $options['delay'] = $this->getOption('delay');
        $options['source'] = url_for($this->getOption('url'));

        $events = ', select:function(event,ui){jQuery("#' . $this->generateId($id_field) . '").val(ui.item.id).trigger("change")}';

        return javascript_tag('jQuery(function(){jQuery("#' . $this->generateId($name) . '").autocomplete(' . substr(options_for_javascript($options), 0 , -1) . $events . '});
jQuery("#' . $this->generateId($name) . '").keyup(function(){if(this.value == ""){jQuery("#' . $this->generateId($id_field) . '").val("").trigger("change")}})});');
    }
}
Return current item: Ace Framework