public function saveEmbeddedForms($con = null, $forms = null)
 {
     if (null === $forms) {
         $stocks = $this->getValue('newStocks');
         $forms = $this->embeddedForms;
         if (isset($forms['currentStocks'])) {
             foreach ($this->embeddedForms['newStocks'] as $name => $form) {
                 if (!isset($stocks[$name])) {
                     unset($forms['newStocks'][$name]);
                 }
             }
         }
         if (isset($forms['currentStocks'])) {
             $stocks = $this->getValue('currentStocks');
             foreach ($stocks as $name => $form) {
                 if (isset($form['delete']) && $form['delete']) {
                     $stock = Doctrine::getTable('rtShopStock')->find($form['id']);
                     $stock->delete();
                     unset($forms['currentStocks'][$name]);
                 }
             }
         }
     }
     $this->saveRelatedAttributeSelections('currentStocks');
     $this->saveRelatedAttributeSelections('newStocks');
     return parent::saveEmbeddedForms($con, $forms);
 }
 public function setup()
 {
     parent::setup();
     unset($this['comment_status'], $this['rt_shop_product_list'], $this['rt_shop_products_linked_list']);
     $query = Doctrine_Query::create()->from('rtShopAttribute a');
     if (!$this->isNew()) {
         $query->select(sprintf('
     a.id,
     a.title,
     (select position from rt_shop_product_to_attribute where product_id = %s and attribute_id = a.id) as position,
     ((select position from rt_shop_product_to_attribute where product_id = %s and attribute_id = a.id) IS NULL) as has_position
     ', $this->object->id, $this->object->id))->orderBy('has_position ASC, position ASC');
     } else {
         $query->orderBy('a.title');
     }
     $this->setWidget('rt_shop_attributes_list', new sfWidgetFormDoctrineChoice(array('query' => $query, 'expanded' => true, 'multiple' => true, 'model' => 'rtShopAttribute')));
     $this->setWidget('rt_shop_promotions_list', new sfWidgetFormDoctrineChoice(array('expanded' => true, 'multiple' => true, 'model' => 'rtShopPromotionProduct')));
     $query = Doctrine_Query::create()->from('rtShopProduct p')->leftJoin('p.rtShopProductToProduct as ptp')->andWhere('ptp.product_id = ?', $this->object->id);
     if (!$this->isNew()) {
         $query->select(sprintf('
     p.id,
     p.title,
     (select position from rt_shop_product_to_product where product_id = %s and product_id_target = p.id) as position,
     ((select position from rt_shop_product_to_product where product_id = %s and product_id_target = p.id) IS NULL) as has_position
     ', $this->object->id, $this->object->id))->andWhere('p.id != ?', $this->getObject()->getId())->orderBy('has_position ASC, position ASC');
     } else {
         $query->orderBy('p.title');
     }
     $this->setWidget('rt_shop_products_list', new sfWidgetFormDoctrineChoice(array('query' => $query, 'expanded' => true, 'multiple' => true, 'model' => 'rtShopProduct')));
     //$b = new sfWidgetFormSelectCheckbox();
     $query = Doctrine::getTable('rtShopCategory')->getOrderQuery();
     $this->setWidget('rt_shop_categories_list', new rtWidgetFormTreeDoctrineChoice(array('query' => $query, 'expanded' => true, 'multiple' => true, 'model' => 'rtShopCategory')));
     $this->setWidget('rt_shop_related_categories_list', new rtWidgetFormTreeDoctrineChoice(array('query' => $query, 'expanded' => true, 'multiple' => true, 'model' => 'rtShopCategory')));
     $this->widgetSchema['rt_shop_attributes_list']->setLabel('Attributes');
     $this->widgetSchema['rt_shop_categories_list']->setLabel('Categories');
     $this->widgetSchema['rt_shop_related_categories_list']->setLabel('Related Product Categories');
     $this->widgetSchema['rt_shop_promotions_list']->setLabel('Promotions');
     $this->widgetSchema['rt_shop_products_list']->setLabel('Related Products');
     $this->widgetSchema->setHelp('rt_shop_attributes_list', 'Optional features this product is defined by. Dragging up or down changes the display order.');
     $this->widgetSchema->setHelp('rt_shop_categories_list', 'One or more categories can be linked to this product.');
     $this->widgetSchema->setHelp('rt_shop_related_categories_list', 'One or more related product categories can be linked to this product.');
     $this->widgetSchema->setHelp('rt_shop_promotions_list', 'One or more product promotions can be linked to this product.');
     $this->widgetSchema->setHelp('rt_shop_products_list', 'One or more related products can be linked to this product. Dragging up or down changes the display order.');
     $this->widgetSchema->setHelp('is_featured', 'Mark this product as featured.');
     $this->widgetSchema->setHelp('backorder_allowed', 'Should the stock/inventory level management be disabled.');
     $this->widgetSchema->setHelp('is_taxable', 'Should the configured tax rate of ' . sfConfig::get('app_rt_shop_tax_rate', '0') . '% percent be applied to this product.');
 }