Ads edition and creation with internationalization
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 15 May 2012 23:31:47 +0000 (01:31 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 15 May 2012 23:31:47 +0000 (01:31 +0200)
Nested forms in order to create or edit ads
(relation 1 to N between data base tables)
I hate internationalization...

apps/companyfront/config/app.yml
apps/companyfront/modules/ad/templates/_form.php
apps/companyfront/modules/ad/templates/_list.php
apps/companyfront/modules/ad/templates/editSuccess.php
apps/companyfront/modules/ad/templates/indexSuccess.php
apps/companyfront/modules/ad/templates/newSuccess.php
apps/companyfront/templates/layout.php
lib/form/doctrine/AdDescriptionForm.class.php
lib/form/doctrine/AdForm.class.php

index 7cad5cf..0e41bc0 100644 (file)
@@ -4,7 +4,7 @@
 # default values
 all:
   max_offices_on_pager: 3
-  max_ads_on_pager: 3
+  max_ads_on_pager: 2
 
   sf_guard_plugin:
      remember_key_expiration_age: 2592000             # 30 days in seconds
index 055a634..66bba95 100644 (file)
         <td colspan="2">
           <?php echo $form->renderHiddenFields(false) ?>
           &nbsp;<a href="<?php echo url_for('ad/index') ?>">Back to list</a>
-          <?php if (!$form->getObject()->isNew()): ?>
-            &nbsp;<?php echo link_to('Delete', 'ad/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
-          <?php endif; ?>
-          <input type="submit" value="Save" />
+          <input type="submit" value="Update" />
         </td>
       </tr>
     </tfoot>
     <tbody>
       <?php echo $form->renderGlobalErrors() ?>
-      <tr>
-        <th><?php echo $form['company_id']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['company_id']->renderError() ?>
-          <?php echo $form['company_id'] ?>
-        </td>
-      </tr>
-      <tr>
-        <th><?php echo $form['company_categ_id']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['company_categ_id']->renderError() ?>
-          <?php echo $form['company_categ_id'] ?>
-        </td>
-      </tr>
-      <tr>
-        <th><?php echo $form['ad_mobile_image_link']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['ad_mobile_image_link']->renderError() ?>
-          <?php echo $form['ad_mobile_image_link'] ?>
-        </td>
-      </tr>
-      <tr>
-        <th><?php echo $form['created_at']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['created_at']->renderError() ?>
-          <?php echo $form['created_at'] ?>
-        </td>
-      </tr>
-      <tr>
-        <th><?php echo $form['updated_at']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['updated_at']->renderError() ?>
-          <?php echo $form['updated_at'] ?>
-        </td>
-      </tr>
+      <?php echo $form ?>
     </tbody>
   </table>
 </form>
index 1ddfa3d..09e66bd 100644 (file)
@@ -1,7 +1,7 @@
 <table id="rounded-corner">
   <thead>
     <tr>
-         <th scope="col" class="rounded-company"><?php echo __('Phone Image Link') ?></th>
+         <th scope="col" class="rounded-company"><?php echo __('Phone Image') ?></th>
          <th scope="col" class="rounded"><?php echo __('Company Category') ?></th>
       <th scope="col" class="rounded"><?php echo __('Ad Name') ?></th>
       <th scope="col" class="rounded"><?php echo __('Edit') ?></th>
   <tbody>
     <?php foreach ($ads as $ad): ?>
     <tr>
-      <td><?php echo $ad->getAd()->getAdMobileImageLink() ?></td>
+      <td><img src="<?php echo $ad->getAd()->getAdMobileImageLink() ?>" alt="<?php echo $ad->getAdName() ?>"/></td>
       <td><?php echo $ad->getAd()->getCompanyCategId() ?></td>
       <td><?php echo $ad->getAdName() ?></td>
-         <td><a href="<?php echo url_for('ad/edit?id='.$ad->getId()) ?>"><img src="/images/pencil_add.png" alt="" title="" border="0" /></a></td>
-      <td><?php echo link_to('<img src="/images/inadminpanel/images/trash.png" alt="" title="" border="0" />', 'ad/delete?id='.$ad->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?></td>
+         <td><a href="<?php echo url_for('ad/edit?id='.$ad->getAd()->getId()) ?>"><img src="/images/pencil_add.png" alt="" title="" border="0" /></a></td>
+      <td><?php echo link_to('<img src="/images/inadminpanel/images/trash.png" alt="" title="" border="0" />', 'ad/delete?id='.$ad->getAd()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?></td>
 
     </tr>
     <?php endforeach; ?>
index b6d77be..0cdbaf0 100644 (file)
@@ -1,3 +1,4 @@
-<h1>Edit Ad</h1>
+<h2><?php echo __('Edit Ad') ?></h2>
+
 
 <?php include_partial('form', array('form' => $form)) ?>
index 48948e2..5c2dfd1 100644 (file)
@@ -34,5 +34,5 @@
   </div>
 <?php endif; ?>
 
-  <a href="<?php echo url_for('ad/new') ?>" class="bt_green"><span class="bt_green_lft"></span><strong><?php echo __('Ad Office') ?></strong><span class="bt_green_r"></span></a>
+  <a href="<?php echo url_for('ad/new') ?>" class="bt_green"><span class="bt_green_lft"></span><strong><?php echo __('New Ad') ?></strong><span class="bt_green_r"></span></a>
 
index 3d12b7f..2c9e131 100644 (file)
@@ -1,3 +1,3 @@
-<h1>New Ad</h1>
+<h2><?php echo __('New Ad') ?></h2>
 
 <?php include_partial('form', array('form' => $form)) ?>
index 9302c0e..19ee11e 100644 (file)
@@ -61,7 +61,7 @@
         <li><a>Manage Ads<!--[if IE 7]><!--></a><!--<![endif]-->
         <!--[if lte IE 6]><table><tr><td><![endif]-->
                <ul>
-                <li><a href="<?php echo url_for('anuncio/new') ?>" title="">Create New Add</a></li>
+                <li><a href="<?php echo url_for('ad/new') ?>" title="">Create New Add</a></li>
                 <li><a href="" title="">Associate Ad with Company Category</a></li>
              </ul>
          <!--[if lte IE 6]></td></tr></table></a><![endif]-->
             </div>
             <div class="sidebarmenu">
             
-                <a class="menuitem submenuheader" href="">Ads</a>
+                <a class="menuitem submenuheader" href="<?php echo url_for('ad/index') ?>">Ads</a>
                 <div class="submenu">
                     <ul>
-                    <li><a href="<?php echo url_for('anuncio/index') ?>">Ads Index</a></li>
+                    <li><a href="<?php echo url_for('ad/index') ?>">Ads Index</a></li>
                     </ul>
                 </div>
-                <a class="menuitem submenuheader" href="" >Offices</a>
+                <a class="menuitem submenuheader" href="<?php echo url_for('office/index') ?>" >Offices</a>
                 <div class="submenu">
                     <ul>
                     <li><a href="<?php echo url_for('office/index') ?>">Offices Index</a></li>
index b6e0c1c..f2c17d0 100644 (file)
@@ -12,5 +12,13 @@ class AdDescriptionForm extends BaseAdDescriptionForm
 {
   public function configure()
   {
+    //$this->useFields(array('language_id', 'ad_id', 'ad_name', 'ad_description', 'ad_mobile_text', 'ad_link'));
+    unset($this['ad_id']);
+
+    if ($this->object->exists())
+    {
+      $this->widgetSchema['delete'] = new sfWidgetFormInputCheckbox();
+      $this->validatorSchema['delete'] = new sfValidatorPass();
+    }
   }
 }
index c5e32ef..53d3c48 100644 (file)
  *
  * @package    mobiads
  * @subpackage form
- * @author     Your name here
- * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
+ * @author     Gustavo Martin Morcuende
+ * @version
  */
 class AdForm extends BaseAdForm
 {
+  protected $scheduledForDeletion = array();
+
   public function configure()
   {
+    $this->useFields(array('company_categ_id', 'ad_mobile_image_link'));
+
+    // Ad creation form
+    $adDescription = new AdDescription();
+    $adDescription->Ad = $this->getObject();
+    $newAdDescriptionForm = new AdDescriptionForm($adDescription);
+
+    $this->embedForm('new', $newAdDescriptionForm);
+
+    $this->embedRelation('AdDescription');
+
+  }
+
+  protected function doBind(array $values)
+  {
+    if ('' === trim($values['new']['ad_description']) && '' === trim($values['new']['ad_mobile_text'])
+        && '' === trim($values['new']['ad_name']) && '' === trim($values['new']['ad_link']))
+    {
+      unset($values['new'], $this['new']);
+    }
+
+    if (isset($values['AdDescription']))
+    {
+      foreach ($values['AdDescription'] as $i => $adDescriptionValues)
+      {
+        if (isset($adDescriptionValues['delete']) && $adDescriptionValues['id'])
+        {
+          $this->scheduledForDeletion[$i] = $adDescriptionValues['id'];
+        }
+      }
+    }
+
+
+    parent::doBind($values);
+  }
+
+  /**
+   * Updates object with provided values, dealing with evantual relation deletion
+   *
+   * @see sfFormDoctrine::doUpdateObject()
+   */
+  protected function doUpdateObject($values)
+  {
+    if (count($this->scheduledForDeletion))
+    {
+      foreach ($this->scheduledForDeletion as $index => $id)
+      {
+        unset($values['AdDescription'][$index]);
+        unset($this->object['AdDescription'][$index]);
+        Doctrine::getTable('AdDescription')->findOneById($id)->delete();
+      }
+    }
+
+    $this->getObject()->fromArray($values);
+  }
+
+  /**
+   * Saves embedded form objects.
+   *
+   * @param mixed $con   An optional connection object
+   * @param array $forms An array of forms
+   */
+  public function saveEmbeddedForms($con = null, $forms = null)
+  {
+    if (null === $con)
+    {
+      $con = $this->getConnection();
+    }
+
+    if (null === $forms)
+    {
+      $forms = $this->embeddedForms;
+    }
+
+    foreach ($forms as $form)
+    {
+      if ($form instanceof sfFormObject)
+      {
+        if (!in_array($form->getObject()->getId(), $this->scheduledForDeletion))
+        {
+          $form->saveEmbeddedForms($con);
+          $form->getObject()->save($con);
+        }
+      }
+      else
+      {
+        $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
+      }
+    }
   }
 }