<?php

/**
 * RokGallery_Model_Gallery
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    RokGallery
 * @subpackage models
 * @author     RocketTheme LLC <support@rockettheme.com>
 * @version    SVN: $Id: Gallery.php 39322 2011-07-02 05:20:22Z btowles $
 */
class RokGallery_Model_Gallery extends RokGallery_Model_Base_Gallery
{

    public function postDelete(Doctrine_Event $event)
    {
        //Make sure all linked tables are clean
        $q = Doctrine_Query::create()
            ->update('RokGallery_Model_Slice')
            ->set('gallery_id', 'NULL')
            ->andWhere('gallery_id NOT IN (SELECT g.id from RokGallery_Model_Gallery g)');
        $q->execute();
        $q->free();
    }

    public function preUpdate(Doctrine_Event $event)
    {
        // set background color to transparent
        if (empty($this->thumb_background)) {
            $this->thumb_background = 'transparent';
        }
    }

    public function preInsert(Doctrine_Event $event)
    {
        // set background color to transparent
        if (empty($this->thumb_background)) {
            $this->thumb_background = 'transparent';
        }
    }


    /**
     * Check to see if a gallery is for a particular filetag
     * @param $tag
     * @return bool
     */
    public function hasTag($tag)
    {
        foreach($this->filetags as $filetag)
        {
            if ($tag == $filetag) {
                return true;
            }
        }
        return false;
    }

    public function setSliceOrder(array $slice_order)
    {
        $this->Slices->setKeyColumn('id');
        foreach ($slice_order as $position => $slice_id)
        {
            foreach($this->Slices as &$slice) {
            {
                    if ($slice->id == $slice_id) {
                    $slice->ordering = $position;
                    $slice->save();
                    break;
                }
            }
        }
        }
    }
}