Edit/create new ads. Duplicate language.
authorGusa <gu.martinm@gmail.com>
Sun, 16 Dec 2012 07:42:00 +0000 (08:42 +0100)
committerGusa <gu.martinm@gmail.com>
Sun, 16 Dec 2012 07:42:00 +0000 (08:42 +0100)
Show just once a language.

lib/form/doctrine/AdDescriptionForm.class.php
lib/model/doctrine/LanguageTable.class.php

index f8de80f..350834c 100644 (file)
@@ -15,12 +15,88 @@ class AdDescriptionForm extends BaseAdDescriptionForm
     unset($this['ad_id']);
     unset($this['ad_description']);
 
+
     $this->widgetSchema['ad_link'] = new sfWidgetFormInputText();
 
+    if ($this->isNew())
+    {
+        $query = LanguageTable::getInstance()->getLanguagesQuery($this->availableLanguages());
+
+        $this->widgetSchema['language_id'] = new sfWidgetFormDoctrineChoice(array('model'     => $this->getRelatedModelName('Language'),
+                                                                                  'add_empty' => false,
+                                                                                  'query'     => $query));
+
+
+        $this->validatorSchema['language_id'] = new sfValidatorDoctrineChoice(array('model'    => $this->getRelatedModelName('Language'),
+                                                                                    'required' => true,
+                                                                                    'query'    => $query));
+    }
+    else {
+        $query = LanguageTable::getInstance()->getLanguagesQuery($this->getObject()->getLanguageId());
+
+        $this->widgetSchema['language_id'] = new sfWidgetFormDoctrineChoice(array('model'     => $this->getRelatedModelName('Language'),
+                                                                                  'add_empty' => false,
+                                                                                   'query'    => $query));
+
+
+        $this->validatorSchema['language_id'] = new sfValidatorDoctrineChoice(array('model'    => $this->getRelatedModelName('Language'),
+                                                                                    'required' => true,
+                                                                                    'query'    => $query));
+    }
+
     if ($this->object->exists())
     {
       $this->widgetSchema['delete'] = new sfWidgetFormInputCheckbox();
       $this->validatorSchema['delete'] = new sfValidatorPass();
     }
   }
+
+  /**
+   * Retrieve the available language's ids.
+   *
+   * @return array an array with the available languages' ids.
+   */
+  private function availableLanguages()
+  {
+    //Doctrine_Collection with all our languages
+    $languages = LanguageTable::getInstance()->findAll();
+
+    //Using Doctrine_Collection_Iterator
+    $iterator = $languages->getIterator();
+
+    //Doctrine_Collection with the current descriptions for our ad
+    //When creating the first time a new ad there is not Doctrine_Row for this ad in the Ad Table.
+    //We check that edge condition.
+    if ($this->getObject()->getAd()->exists())
+    {
+        $adDescriptions = AdDescriptionTable::getInstance()->findByAdId($this->getObject()->getAdId());
+    }
+    else
+    {
+        $adDescriptions = array();
+    }
+
+    $availableLanguages = array();
+
+    while ($language = $iterator->current())
+    {
+        $match = false;
+        foreach ($adDescriptions as $adDescription)
+        {
+            if ($adDescription->getLanguageId() == $language->getId())
+            {
+                //There is a match
+                $match = true;
+                break;
+            }
+        }
+        if (!$match)
+        {
+            $availableLanguages[] = $language->getId();
+        }
+        $iterator->next();
+    }
+
+    return $availableLanguages;
+  }
 }
index 0674478..e2055f8 100644 (file)
@@ -16,4 +16,22 @@ class LanguageTable extends Doctrine_Table
     {
         return Doctrine_Core::getTable('Language');
     }
-}
\ No newline at end of file
+
+   /**
+    * Return languages by their ids
+    *
+    * @param array $availableLanguages    an array with the language ids to search for.
+    * @return Doctrine_Query              one ore more languages by its ids as a Doctrine Query
+    */
+    public function getLanguagesQuery($availableLanguages)
+    {
+        if (empty($availableLanguages)) {
+            return $this->createQuery('la');
+        } elseif (is_array($availableLanguages)) {
+            return $this->createQuery('la')->whereIn('la.id', $availableLanguages);
+        }
+        else {
+            return $this->createQuery('la')->where('la.id = ?', $availableLanguages);
+        }
+    }
+}