Company Category edit, new, list, remove
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 May 2012 19:46:02 +0000 (21:46 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 May 2012 19:46:02 +0000 (21:46 +0200)
Everything about company categories.

apps/companyfront/config/app.yml
apps/companyfront/modules/ad/templates/_list.php
apps/companyfront/modules/category/actions/actions.class.php
apps/companyfront/modules/category/templates/_form.php
apps/companyfront/modules/category/templates/_list.php
apps/companyfront/modules/category/templates/editSuccess.php
apps/companyfront/modules/category/templates/newSuccess.php
lib/form/doctrine/CompanyCategoryDescriptionForm.class.php
lib/form/doctrine/CompanyCategoryForm.class.php
lib/model/doctrine/CompanyCategory.class.php
lib/model/doctrine/CompanyCategoryTable.class.php

index adb5077..40a5a99 100644 (file)
@@ -5,7 +5,7 @@
 all:
   max_offices_on_pager: 3
   max_ads_on_pager: 2
-  max_categories_on_pager: 2
+  max_categories_on_pager: 3
   default_language: eng                               # Everything must exist at least with this language
 
   sf_guard_plugin:
index 5160b2f..72be869 100644 (file)
     <?php foreach ($ads as $ad): ?>
     <tr>
       <td><img src="<?php echo $ad->getAd()->getAdMobileImageLink() ?>" width="80" height="80" alt="<?php echo $ad->getAdName() ?>"/></td>
-      <td><?php echo $ad->getAd()->getCompanyCategory() ?></td>
+      <td>
+        <?php if ($ad->getAd()->getCompanyCategId() != null): ?>
+            <?php echo $ad->getAd()->getCompanyCategory() ?></td>
+        <?php endif; ?>
+      </td>
       <td><?php echo $ad->getAdName() ?></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>
index b27e802..57727a5 100644 (file)
@@ -32,14 +32,25 @@ class categoryActions extends sfActions
 
   public function executeNew(sfWebRequest $request)
   {
-    $this->form = new CompanyCategoryForm();
+    //Get user Id
+    $userId = $this->getUser()->getGuardUser()->getId();
+
+    $this->form = new CompanyCategoryForm(null, array('company_user_id' => CompanyTable::getInstance()->findOneByUserId($userId)->getId()));
   }
 
   public function executeCreate(sfWebRequest $request)
   {
     $this->forward404Unless($request->isMethod(sfRequest::POST));
 
-    $this->form = new CompanyCategoryForm();
+    $companyCategoryInit = new CompanyCategory();
+
+    //Get user Id
+    $userId = $this->getUser()->getGuardUser()->getId();
+
+    //Get company owned by that user and insert value in form
+    $companyCategoryInit->company_id = CompanyTable::getInstance()->findOneByUserId($userId)->getId();
+
+    $this->form = new CompanyCategoryForm($companyCategoryInit, array('company_user_id' => CompanyTable::getInstance()->findOneByUserId($userId)->getId()));
 
     $this->processForm($request, $this->form);
 
@@ -49,13 +60,41 @@ class categoryActions extends sfActions
   public function executeEdit(sfWebRequest $request)
   {
     $this->forward404Unless($company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id'))), sprintf('Object company_category does not exist (%s).', $request->getParameter('id')));
-    $this->form = new CompanyCategoryForm($company_category);
+
+    //Get user Id
+    $userId = $this->getUser()->getGuardUser()->getId();
+
+    //Get company owned by that user and insert value in form
+    $companyUserId = CompanyTable::getInstance()->findOneByUserId($userId)->getId();
+
+    //Get id number sent by the user (never trust the users)
+    $companyCategoryId = $request->getParameter('id');
+
+    $companyOfficeId = CompanyCategoryTable::getInstance()->findOneById($companyCategoryId)->getCompanyId();
+
+    $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Category does not exist (%s).', $request->getParameter('id')));
+
+    $this->form = new CompanyCategoryForm($company_category, array('company_user_id' => $companyUserId));
   }
 
   public function executeUpdate(sfWebRequest $request)
   {
     $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
     $this->forward404Unless($company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id'))), sprintf('Object company_category does not exist (%s).', $request->getParameter('id')));
+
+    //Get user Id
+    $userId = $this->getUser()->getGuardUser()->getId();
+
+    //Get company owned by that user and insert value in form
+    $companyUserId = CompanyTable::getInstance()->findOneByUserId($userId)->getId();
+
+    //Get id number sent by the user (never trust the users)
+    $companyCategoryId = $request->getParameter('id');
+
+    $companyOfficeId = CompanyCategoryTable::getInstance()->findOneById($companyCategoryId)->getCompanyId();
+
+    $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Category does not exist (%s).', $request->getParameter('id')));
+
     $this->form = new CompanyCategoryForm($company_category);
 
     $this->processForm($request, $this->form);
index 493967b..ada3fd8 100644 (file)
       <tr>
         <td colspan="2">
           <?php echo $form->renderHiddenFields(false) ?>
-          &nbsp;<a href="<?php echo url_for('category/index') ?>">Back to list</a>
-          <?php if (!$form->getObject()->isNew()): ?>
-            &nbsp;<?php echo link_to('Delete', 'category/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
-          <?php endif; ?>
-          <input type="submit" value="Save" />
+          &nbsp;<a href="<?php echo url_for('category/index') ?>"><?php echo __('Back to list') ?></a>
+          <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['general_categ_id']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['general_categ_id']->renderError() ?>
-          <?php echo $form['general_categ_id'] ?>
-        </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>
-      <tr>
-        <th><?php echo $form['root_id']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['root_id']->renderError() ?>
-          <?php echo $form['root_id'] ?>
-        </td>
-      </tr>
-      <tr>
-        <th><?php echo $form['lft']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['lft']->renderError() ?>
-          <?php echo $form['lft'] ?>
-        </td>
-      </tr>
-      <tr>
-        <th><?php echo $form['rgt']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['rgt']->renderError() ?>
-          <?php echo $form['rgt'] ?>
-        </td>
-      </tr>
-      <tr>
-        <th><?php echo $form['level']->renderLabel() ?></th>
-        <td>
-          <?php echo $form['level']->renderError() ?>
-          <?php echo $form['level'] ?>
-        </td>
-      </tr>
+      <?php echo $form->renderGlobalErrors(false) ?>
+      <?php echo $form ?>
     </tbody>
   </table>
 </form>
index 8d9ae35..8fea7d6 100644 (file)
@@ -18,18 +18,17 @@ jQuery(document).ready(function(){
 <table id="rounded-corner">
   <thead>
     <tr>
-      <th scope="col" class="rounded-company">Name</th>
-         <th scope="col" class="rounded">Current General Category</th>
-      <th scope="col" class="rounded">Associate/Delete</th>
-      <th scope="col" class="rounded">Edit</th>
-      <th scope="col" class="rounded-q4">Delete</th>
+      <th scope="col" class="rounded-company"><?php echo __('Company Category') ?></th>
+         <th scope="col" class="rounded"><?php echo __('Current General Category') ?></th>
+      <th scope="col" class="rounded"><?php echo __('Edit') ?></th>
+      <th scope="col" class="rounded-q4"></th>
     </tr>
   </thead>
   <tbody>
-    <?php foreach ($company_categories as $categ_empresa): ?>
-    <tr id="node-<?php echo $categ_empresa->getId()?>" <?php
+    <?php foreach ($company_categories as $company_category): ?>
+    <tr id="node-<?php echo $company_category->getId()?>" <?php
          // insert hierarchical info
-         $node = $categ_empresa->getNode();
+         $node = $company_category->getNode();
          if ($node->isValidNode() && $node->hasParent() && ($node->getParent()->getId() != '1'))
       {
        echo 'class="child-of-node-'.$node->getParent()->getId().'"';
@@ -37,35 +36,25 @@ jQuery(document).ready(function(){
       ?>>
       <td>
         <a>
-           <?php echo $categ_empresa ?>
+           <?php echo $company_category ?>
         </a>
       </td >
          <td>
-          <?php echo $categ_empresa->getGeneralCategory() ?>
-      </td>
-      <td>
-        <?php if ($categ_empresa->getId() != 1): ?>
-         <?php if ($categ_empresa->getGeneralCategId() != null): ?>
-          <a href="<?php echo url_for('categgeneral/deleteassociation?idcompanycateg='.$categ_empresa->getId()) ?>">
-          <img src="/images/link_break.png" alt="" title="" border="0" /></a>
-         <?php else: ?>
-          <a><img src="/images/link.png" alt="" title="" border="0" /></a>
-         <?php endif; ?>
-        <?php else: ?>
-                <a><img src="/images/cross.png" alt="" title="" border="0" /></a>
+        <?php if ($company_category->getGeneralCategId() != null): ?>
+            <?php echo $company_category->getGeneralCategory() ?>
         <?php endif; ?>
       </td>
       <td>
-        <?php if ($categ_empresa->getId() != 1): ?>
-         <a href="<?php echo url_for('categempresa/edit?id='.$categ_empresa->getId()) ?>">
+        <?php if ($company_category->getId() != 1): ?>
+         <a href="<?php echo url_for('category/edit?id='.$company_category->getId()) ?>">
          <img src="/images/pencil_add.png" alt="" title="" border="0" /></a>
         <?php else: ?>
             <a><img src="/images/cross.png" alt="" title="" border="0" /></a>
         <?php endif; ?>
       </td>
       <td>
-        <?php if ($categ_empresa->getId() != 1): ?>      
-         <?php echo link_to('<img src="/images/inadminpanel/images/trash.png" alt="" title="" border="0" />', 'categempresa/delete?id='.$categ_empresa->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
+        <?php if ($company_category->getId() != 1): ?>   
+         <?php echo link_to('<img src="/images/inadminpanel/images/trash.png" alt="" title="" border="0" />', 'category/delete?id='.$company_category->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
         <?php else: ?>
          <a><img src="/images/cross.png" alt="" title="" border="0" /></a>
         <?php endif; ?>
index e7dfb14..bfa90a8 100644 (file)
@@ -1,3 +1,4 @@
-<h1>Edit Company category</h1>
+<h2><?php echo __('Edit Company Category') ?></h2>
+
 
 <?php include_partial('form', array('form' => $form)) ?>
index 735bafa..bc57398 100644 (file)
@@ -1,3 +1,3 @@
-<h1>New Company category</h1>
+<h2><?php echo __('New Company Category') ?></h2>
 
 <?php include_partial('form', array('form' => $form)) ?>
index 52d2af2..6b0c289 100644 (file)
@@ -5,12 +5,19 @@
  *
  * @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 CompanyCategoryDescriptionForm extends BaseCompanyCategoryDescriptionForm
 {
   public function configure()
   {
+    unset($this['company_categ_id']);
+
+    if ($this->object->exists())
+    {
+      $this->widgetSchema['delete'] = new sfWidgetFormInputCheckbox();
+      $this->validatorSchema['delete'] = new sfValidatorPass();
+    }
   }
 }
index 52d4c27..2a5ade5 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 CompanyCategoryForm extends BaseCompanyCategoryForm
 {
   public function configure()
   {
+    $this->useFields(array('general_categ_id'));
+
+    //Narrow down the valid options for some field validators
+    $companyCategs = CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($this->getOption('company_user_id'));
+
+    $this->widgetSchema['parent_category'] = new sfWidgetFormDoctrineChoice(array('model'     => $this->getModelName(),
+                                                                                  'add_empty' => false,
+                                                                                  'query'     => $companyCategs));
+
+    $this->validatorSchema['parent_category'] = new sfValidatorDoctrineChoice(array('model'    => $this->getModelName(),
+                                                                                    'required' => true,
+                                                                                    'query'    => $companyCategs));
+
+    if (!$this->getObject()->isNew())
+    {
+        $this->setDefault('parent_category', $this->getObject()->getNode()->getParent()->getId());
+    }
+
+    $this->widgetSchema->setLabels(array('parent_category'  => 'Parent Company Category'));
+    $this->widgetSchema->setLabels(array('general_categ_id' => 'General Category'));
+
+
+
+    //Company categ creation form
+    $companyCategoryDescription = new CompanyCategoryDescription();
+    $companyCategoryDescription->CompanyCategory = $this->getObject();
+    $newCompanyCategDescriptionForm = new CompanyCategoryDescriptionForm($companyCategoryDescription);
+
+    $this->embedForm('new', $newCompanyCategDescriptionForm);
+
+    $this->embedRelation('CompanyCategoryDescription');
+
+
+    //i18n (Internationalization)
+    $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('company_categor_form');
+  }
+
+  protected function doBind(array $values)
+  {
+    if ('' === trim($values['new']['company_categ_description']) && '' === trim($values['new']['company_categ_name']))
+    {
+      unset($values['new'], $this['new']);
+    }
+
+    if (isset($values['CompanyCategoryDescription']))
+    {
+      foreach ($values['CompanyCategoryDescription'] as $i => $companyCategoryDescriptionValues)
+      {
+        if (isset($companyCategoryDescriptionValues['delete']) && $companyCategoryDescriptionValues['id'])
+        {
+          $this->scheduledForDeletion[$i] = $companyCategoryDescriptionValues['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['CompanyCategoryDescription'][$index]);
+        unset($this->object['CompanyCategoryDescription'][$index]);
+        Doctrine::getTable('CompanyCategoryDescription')->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());
+      }
+    }
+  }
+
+
+  /**
+   * Overriding doSave method from lib/vendor/symfony/lib/form/addon/sfFormObject.class.php
+   *
+   * We need to save new objects in a Nested Tree where the object must have always a parent node
+   * We retrieve the parent node id and insert the new object as a child of that node.
+   *
+   * @param mixed $con An optional connection object
+   */
+  protected function doSave($con = null)
+  {
+    //In this way we are writing on the database twice. Right now and the second one inserting the node as a child of its parent.
+    parent::doSave($con);
+
+    $companyCateg = CompanyCategoryTable::getInstance()->findOneById($this->values['parent_category']);
+    //Second one, right here
+    $this->getObject()->getNode()->insertAsFirstChildOf($companyCateg);
   }
 }
index e64ac0d..aacdf06 100644 (file)
@@ -34,7 +34,7 @@ class CompanyCategory extends BaseCompanyCategory
 
     //Otherwise return with the default language
     $languageCode = sfConfig::get('app_default_language');
-    $languageId = LanguageTable::getInstance()->findByCode($languageCode);
+    $languageId = LanguageTable::getInstance()->findOneByCode($languageCode)->getId();
     foreach ($categoryDescriptions as $categoryDescription)
     {
         if ($categoryDescription->getLanguageId() == $languageId)
index 1947289..3a2a9a4 100644 (file)
@@ -25,6 +25,8 @@ class CompanyCategoryTable extends Doctrine_Table
     */
     public function getCompanyCategoriesByCompanyIdQuery($companyId)
     {
-        return $this->createQuery('cg')->where('cg.company_id = ?', $companyId);
+        return $this->createQuery('cg')->where('cg.company_id = ?', $companyId)
+                                       ->orWhere('cg.id = ?', '1')
+                                       ->orderBy('cg.id');
     }
 }