Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Adapter/Doctrine/Validator/Unique.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_Validator_Unique extends Ace_Form_Validator
{
    /**
     * Configures the current validator.
     *
     * Available options:
     *
     *  * model:              The model class (required)
     *  * column:             The unique column name in Doctrine field name format (required)
     *                        If the uniquess is for several columns, you can pass an array of field names
     *  * primary_key:        The primary key column name in Doctrine field name format (optional, will be introspected if not provided)
     *                        You can also pass an array if the table has several primary keys
     *  * connection:         The Doctrine connection to use (null by default)
     *  * throw_global_error: Whether to throw a global error (false by default) or an error tied to the first field related to the column option array
     *
     * @see Ace_Form_Validator
     */
    protected function configure($options = array(), $messages = array())
    {
        $this->addRequiredOption('model');
        $this->addRequiredOption('column');
        $this->addOption('primary_key', null);
        $this->addOption('connection', null);
        $this->addOption('throw_global_error', false);

        $this->setMessage('invalid', 'An object with the same "%column%" already exist.');
    }

    /**
     * @see Ace_Form_Validator
     */
    protected function doClean($values)
    {
        $originalValues = $values;
        $table = Doctrine_Core::getTable($this->getOption('model'));

        if(!is_array($this->getOption('column')))
        {
            $this->setOption('column', array($this->getOption('column')));
        }

        //if $values isn't an array, make it one
        if(!is_array($values))
        {
            //use first column for key
            $columns = $this->getOption('column');
            $values = array($columns[0] => $values);
        }

        $q = Doctrine_Core::getTable($this->getOption('model'))->createQuery('a');

        foreach($this->getOption('column') as $column)
        {
            $colName = $table->getColumnName($column);
            if(!array_key_exists($column, $values))
            {
                // one of the column has be removed from the form
                return $originalValues;
            }

            $q->addWhere('a.' . $colName . ' = ?', $values[$column]);
        }

        $object = $q->fetchOne();

        // if no object or if we're updating the object, it's ok
        if(!$object || $this->isUpdate($object, $values))
        {
            return $originalValues;
        }

        throw new Ace_Form_Validator_Exception($this, 'invalid', array('column' => implode(', ', $this->getOption('column')), 'error_in_field' => $column));
    }

    /**
     * Returns whether the object is being updated.
     *
     * @param Doctrine_Record $object
     * @param array $values
     *
     * @return boolean
     */
    protected function isUpdate(Doctrine_Record $object, $values)
    {
        // check each primary key column
        foreach($this->getPrimaryKeys() as $column)
        {
            if(!isset($values[$column]) || $object->$column != $values[$column])
            {
                return false;
            }
        }

        return true;
    }

    /**
     * Returns the primary keys for the model.
     *
     * @return array An array of primary keys
     */
    protected function getPrimaryKeys()
    {
        if(null === $this->getOption('primary_key'))
        {
            $primaryKeys = Doctrine_Core::getTable($this->getOption('model'))->getIdentifier();
            $this->setOption('primary_key', $primaryKeys);
        }

        if(!is_array($this->getOption('primary_key')))
        {
            $this->setOption('primary_key', array($this->getOption('primary_key')));
        }

        return $this->getOption('primary_key');
    }

}
Return current item: Ace Framework