From 97886e51443efec7cdf99406909efe9f98ee57ed Mon Sep 17 00:00:00 2001 From: Gusa Date: Sun, 16 Dec 2012 08:42:00 +0100 Subject: [PATCH] Edit/create new ads. Duplicate language. Show just once a language. --- lib/form/doctrine/AdDescriptionForm.class.php | 76 +++++++++++++++++++++++++++ lib/model/doctrine/LanguageTable.class.php | 20 ++++++- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/lib/form/doctrine/AdDescriptionForm.class.php b/lib/form/doctrine/AdDescriptionForm.class.php index f8de80f..350834c 100644 --- a/lib/form/doctrine/AdDescriptionForm.class.php +++ b/lib/form/doctrine/AdDescriptionForm.class.php @@ -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; + } } diff --git a/lib/model/doctrine/LanguageTable.class.php b/lib/model/doctrine/LanguageTable.class.php index 0674478..e2055f8 100644 --- a/lib/model/doctrine/LanguageTable.class.php +++ b/lib/model/doctrine/LanguageTable.class.php @@ -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); + } + } +} -- 2.1.4