Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Adapter/Doctrine/Element/Choice.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_Choice extends Ace_Form_Element_Choice_Select
{
    /**
     * Constructor.
     * 
     * @see Ace_Element
     */
    public function __construct($options = array(), $attributes = array())
    {
        $options['choices'] = array();

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

    /**
     * Configure the element.
     *
     * Available options:
     *
     *  * model:        The model class (required)
     *  * add_empty:    Whether to add a first empty value or not (false by default)
     *                  If the option is not a Boolean, the value will be used as the text value
     *  * method:       The method to use to display object values (__toString by default)
     *  * order_by:     An array composed of two fields:
     *                    * The column to order by the results (must be in the PhpName format)
     *                    * asc or desc
     *  * query:        A query to use when retrieving objects
     *  * multiple:     true if the select tag must allow multiple selections
     *  * table_method: A method to return either a query, collection or single object
     *
     * @see Ace_Element
     */
    protected function configure($options = array(), $attributes = array())
    {
        $this->addRequiredOption('model');
        $this->addOption('add_empty', false);
        $this->addOption('method', '__toString');
        $this->addOption('order_by', null);
        $this->addOption('query', null);
        $this->addOption('multiple', false);
        $this->addOption('table_method', null);

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

    /**
     * Returns the choices associated to the model.
     *
     * @return array An array of choices
     */
    public function getChoices()
    {
        $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]);
            }
            
            $objects = $query->execute();
        }
        else
        {
            $tableMethod = $this->getOption('table_method');
            $results = Doctrine_Core::getTable($this->getOption('model'))->$tableMethod();

            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');

        foreach($objects as $object)
        {
            $choices[$this->getPrimaryKey($object)] = $object->$method();
        }

        return $choices;
    }

    /**
     * Returns the current record's primary key.
     *
     * @param  Doctrine_Record $object
     * @return mixed The value of the current model's last identifier column
     */
    public function getPrimaryKey($object)
    {
        $identifier = (array) $object->identifier();
        return end($identifier);
    }
}
Return current item: Ace Framework