First steps in company category administration.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sat, 19 May 2012 19:29:46 +0000 (21:29 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sat, 19 May 2012 19:29:46 +0000 (21:29 +0200)
14 files changed:
apps/companyfront/config/routing.yml
apps/companyfront/modules/category/actions/actions.class.php [new file with mode: 0644]
apps/companyfront/modules/category/templates/_form.php [new file with mode: 0644]
apps/companyfront/modules/category/templates/_list.php [new file with mode: 0644]
apps/companyfront/modules/category/templates/editSuccess.php [new file with mode: 0644]
apps/companyfront/modules/category/templates/indexSuccess.php [new file with mode: 0644]
apps/companyfront/modules/category/templates/newSuccess.php [new file with mode: 0644]
apps/companyfront/modules/category/templates/showSuccess.php [new file with mode: 0644]
apps/companyfront/templates/layout.php
lib/model/doctrine/AdDescriptionTable.class.php
lib/model/doctrine/CompanyCategoryDescriptionTable.class.php
lib/model/doctrine/CompanyCategoryTable.class.php
lib/model/doctrine/GeneralCategory.class.php
test/functional/companyfront/categoryActionsTest.php [new file with mode: 0644]

index a5d1a2c..927ab60 100644 (file)
@@ -20,6 +20,12 @@ ads_index:
   param:   { module: ad, action: index }
   options: { model: Ad, type: object }
 
+companycategories_index:
+  url:     /category/index
+  class:   sfDoctrineRoute
+  param:   { module: category, action: index }
+  options: { model: CompanyCategory, type: object }
+
 # generic rules
 # please, remove them by adding more specific rules
 default_index:
diff --git a/apps/companyfront/modules/category/actions/actions.class.php b/apps/companyfront/modules/category/actions/actions.class.php
new file mode 100644 (file)
index 0000000..e144889
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * category actions.
+ *
+ * @package    mobiads
+ * @subpackage category
+ * @author     Gustavo Martin Morcuende
+ * @version
+ */
+class categoryActions extends sfActions
+{
+  public function executeIndex(sfWebRequest $request)
+  {
+    $userId = $this->getUser()->getGuardUser()->getId();
+
+    //Get company owned by that user
+    //Just 1 user owns a company. Should this be improved?
+    $companyId = CompanyTable::getInstance()->findOneByUserId($userId)->getId();
+
+    //Doctrine Query used to show a pager with the Company Categories.
+    $query=CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($companyId);
+
+    $this->pager = new sfDoctrinePager('CompanyCategoryDescription', sfConfig::get('app_max_categories_on_pager'));
+    $this->pager->setQuery($query);
+    $this->pager->setPage($request->getParameter('page', 1));
+    $this->pager->init();
+  }
+
+  public function executeShow(sfWebRequest $request)
+  {
+    $this->company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id')));
+    $this->forward404Unless($this->company_category);
+  }
+
+  public function executeNew(sfWebRequest $request)
+  {
+    $this->form = new CompanyCategoryForm();
+  }
+
+  public function executeCreate(sfWebRequest $request)
+  {
+    $this->forward404Unless($request->isMethod(sfRequest::POST));
+
+    $this->form = new CompanyCategoryForm();
+
+    $this->processForm($request, $this->form);
+
+    $this->setTemplate('new');
+  }
+
+  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);
+  }
+
+  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')));
+    $this->form = new CompanyCategoryForm($company_category);
+
+    $this->processForm($request, $this->form);
+
+    $this->setTemplate('edit');
+  }
+
+  public function executeDelete(sfWebRequest $request)
+  {
+    $request->checkCSRFProtection();
+
+    $this->forward404Unless($company_category = Doctrine_Core::getTable('CompanyCategory')->find(array($request->getParameter('id'))), sprintf('Object company_category does not exist (%s).', $request->getParameter('id')));
+    $company_category->delete();
+
+    $this->redirect('category/index');
+  }
+
+  protected function processForm(sfWebRequest $request, sfForm $form)
+  {
+    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
+    if ($form->isValid())
+    {
+      $company_category = $form->save();
+
+      $this->redirect('category/edit?id='.$company_category->getId());
+    }
+  }
+}
diff --git a/apps/companyfront/modules/category/templates/_form.php b/apps/companyfront/modules/category/templates/_form.php
new file mode 100644 (file)
index 0000000..493967b
--- /dev/null
@@ -0,0 +1,81 @@
+<?php use_stylesheets_for_form($form) ?>
+<?php use_javascripts_for_form($form) ?>
+
+<form action="<?php echo url_for('category/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
+<?php if (!$form->getObject()->isNew()): ?>
+<input type="hidden" name="sf_method" value="put" />
+<?php endif; ?>
+  <table>
+    <tfoot>
+      <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" />
+        </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>
+    </tbody>
+  </table>
+</form>
diff --git a/apps/companyfront/modules/category/templates/_list.php b/apps/companyfront/modules/category/templates/_list.php
new file mode 100644 (file)
index 0000000..8d9ae35
--- /dev/null
@@ -0,0 +1,76 @@
+<script type="text/javascript">
+        $(document).ready(function()  {
+            $("#rounded-corner").treeTable();
+        });
+</script>
+
+<script type="text/javascript">
+jQuery(document).ready(function(){
+  $("#associate").click(function(event){
+      event.preventDefault();
+      event.stopPropagation();
+      window.open('<?php echo url_for('categgeneral/index') ?>', '', 'scrollbars,resizable,status,width=888,height=888');
+  });
+});
+</script>
+
+
+<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>
+    </tr>
+  </thead>
+  <tbody>
+    <?php foreach ($company_categories as $categ_empresa): ?>
+    <tr id="node-<?php echo $categ_empresa->getId()?>" <?php
+         // insert hierarchical info
+         $node = $categ_empresa->getNode();
+         if ($node->isValidNode() && $node->hasParent() && ($node->getParent()->getId() != '1'))
+      {
+       echo 'class="child-of-node-'.$node->getParent()->getId().'"';
+      }
+      ?>>
+      <td>
+        <a>
+           <?php echo $categ_empresa ?>
+        </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 endif; ?>
+      </td>
+      <td>
+        <?php if ($categ_empresa->getId() != 1): ?>
+         <a href="<?php echo url_for('categempresa/edit?id='.$categ_empresa->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 else: ?>
+         <a><img src="/images/cross.png" alt="" title="" border="0" /></a>
+        <?php endif; ?>
+      </td>
+    </tr>
+    <?php endforeach; ?>
+  </tbody> 
+</table>
diff --git a/apps/companyfront/modules/category/templates/editSuccess.php b/apps/companyfront/modules/category/templates/editSuccess.php
new file mode 100644 (file)
index 0000000..e7dfb14
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>Edit Company category</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
diff --git a/apps/companyfront/modules/category/templates/indexSuccess.php b/apps/companyfront/modules/category/templates/indexSuccess.php
new file mode 100644 (file)
index 0000000..e9c8e90
--- /dev/null
@@ -0,0 +1,25 @@
+<h2><?php echo __('Company Category Index') ?></h2>
+
+<?php include_partial('category/list', array('company_categories' => $pager->getResults())) ?>
+
+<?php if ($pager->haveToPaginate()): ?>
+  <div class="pagination">
+    <a href="<?php echo url_for('companycategories_index') ?>?page=1"><?php echo __('first page') ?></a>
+
+    <a href="<?php echo url_for('companycategories_index') ?>?page=<?php echo $pager->getPreviousPage() ?>"><?php echo __('<< prev') ?></a>
+
+    <?php foreach ($pager->getLinks() as $page): ?>
+      <?php if ($page == $pager->getPage()): ?>
+        <?php echo $page ?>
+      <?php else: ?>
+        <a href="<?php echo url_for('companycategories_index') ?>?page=<?php echo $page ?>"><?php echo $page ?></a>
+      <?php endif; ?>
+    <?php endforeach; ?>
+
+    <a href="<?php echo url_for('companycategories_index') ?>?page=<?php echo $pager->getNextPage() ?>"><?php echo __('next >>') ?></a>
+
+    <a href="<?php echo url_for('companycategories_index') ?>?page=<?php echo $pager->getLastPage() ?>"><?php echo __('last page') ?></a>
+  </div>
+<?php endif; ?>
+
+<a href="<?php echo url_for('category/new') ?>" class="bt_green"><span class="bt_green_lft"></span><strong>Create new Category</strong><span class="bt_green_r"></span></a>
diff --git a/apps/companyfront/modules/category/templates/newSuccess.php b/apps/companyfront/modules/category/templates/newSuccess.php
new file mode 100644 (file)
index 0000000..735bafa
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>New Company category</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
diff --git a/apps/companyfront/modules/category/templates/showSuccess.php b/apps/companyfront/modules/category/templates/showSuccess.php
new file mode 100644 (file)
index 0000000..5cee794
--- /dev/null
@@ -0,0 +1,46 @@
+<table>
+  <tbody>
+    <tr>
+      <th>Id:</th>
+      <td><?php echo $company_category->getId() ?></td>
+    </tr>
+    <tr>
+      <th>Company:</th>
+      <td><?php echo $company_category->getCompanyId() ?></td>
+    </tr>
+    <tr>
+      <th>General categ:</th>
+      <td><?php echo $company_category->getGeneralCategId() ?></td>
+    </tr>
+    <tr>
+      <th>Created at:</th>
+      <td><?php echo $company_category->getCreatedAt() ?></td>
+    </tr>
+    <tr>
+      <th>Updated at:</th>
+      <td><?php echo $company_category->getUpdatedAt() ?></td>
+    </tr>
+    <tr>
+      <th>Root:</th>
+      <td><?php echo $company_category->getRootId() ?></td>
+    </tr>
+    <tr>
+      <th>Lft:</th>
+      <td><?php echo $company_category->getLft() ?></td>
+    </tr>
+    <tr>
+      <th>Rgt:</th>
+      <td><?php echo $company_category->getRgt() ?></td>
+    </tr>
+    <tr>
+      <th>Level:</th>
+      <td><?php echo $company_category->getLevel() ?></td>
+    </tr>
+  </tbody>
+</table>
+
+<hr />
+
+<a href="<?php echo url_for('category/edit?id='.$company_category->getId()) ?>">Edit</a>
+&nbsp;
+<a href="<?php echo url_for('category/index') ?>">List</a>
index 65de699..f7f0af6 100644 (file)
@@ -94,7 +94,7 @@
                 <a class="menuitem submenuheader" href="">Company Categories</a>
                 <div class="submenu">
                     <ul>
-                    <li><a href="<?php echo url_for('categempresa/index') ?>">Company Categories Index</a></li>
+                    <li><a href="<?php echo url_for('category/index') ?>">Company Categories Index</a></li>
                     </ul>
                 </div>
                 <a class="menuitem" href="">User Reference</a>
index 4a0355a..ba48ecc 100644 (file)
@@ -30,5 +30,4 @@ class AdDescriptionTable extends Doctrine_Table
                                                   ->innerjoin('addescription.Ad ad')
                                                   ->orderBy('ad.id');
     }
-
 }
index c3b0ece..b7e5096 100644 (file)
@@ -16,22 +16,4 @@ class CompanyCategoryDescriptionTable extends Doctrine_Table
     {
         return Doctrine_Core::getTable('CompanyCategoryDescription');
     }
-
-
-   /**
-    * Return
-    *
-    * @return Doctrine Collection
-    */
-    public function getCategGeneralFromIdDAOImpl($languageId, $companyCategId)
-    {
-         $q = Doctrine_Query::create()
-                        ->from('CompanyCategoryDescription cgd')
-                        ->where('cgd.company_categ_id = ?', $companyCategId)
-                        ->andWhere('cgd.language_id = ?', $languageId);
-
-        $doccollect=$q->execute();
-
-        return $doccollect;
-    }
 }
index ad9400a..1947289 100644 (file)
@@ -16,4 +16,15 @@ class CompanyCategoryTable extends Doctrine_Table
     {
         return Doctrine_Core::getTable('CompanyCategory');
     }
-}
\ No newline at end of file
+
+
+   /**
+    * Returns company categories by company id.
+    *
+    * @return related company categories to a company as Doctrine Query
+    */
+    public function getCompanyCategoriesByCompanyIdQuery($companyId)
+    {
+        return $this->createQuery('cg')->where('cg.company_id = ?', $companyId);
+    }
+}
index 9c145eb..a96f051 100644 (file)
@@ -7,9 +7,44 @@
  * 
  * @package    mobiads
  * @subpackage model
- * @author     Your name here
- * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
+ * @author     Gustavo Martin Morcuende
+ * @version
  */
 class GeneralCategory extends BaseGeneralCategory
 {
+  /**
+   * Returns the string representation of this object.
+   *
+   * @return string
+   */
+  public function __toString()
+  {
+    $languageId = sfContext::getInstance()->getUser()->getGuardUser()->getLanguage()->getId();
+
+    //Check if there is description with the user's language
+    $generalCategoryDescriptions = GeneralCategoryDescriptionTable::getInstance()->findByGeneralCategId($this->getId());
+    foreach ($generalCategoryDescriptions as $generalCategoryDescription)
+    {
+        if ($generalCategoryDescription->getLanguageId() == $languageId)
+        {
+           //We found it!!!
+           return (string) $generalCategoryDescription->getGeneralCategName();
+        }
+    }
+
+    //Otherwise return with the default language
+    $languageCode = sfConfig::get('app_default_language');
+    $languageId = LanguageTable::getInstance()->findByCode($languageCode);
+    foreach ($generalCategoryDescriptions as $generalCategoryDescription)
+    {
+        if ($generalCategoryDescription->getLanguageId() == $languageId)
+        {
+           //We found the default name description!!!
+           return (string) $generalCategoryDescription->getGeneralCategName();
+        }
+    }
+
+    //Finally, if nothing was found, return nice error message.
+    return (string) "General category without default language";
+  }
 }
diff --git a/test/functional/companyfront/categoryActionsTest.php b/test/functional/companyfront/categoryActionsTest.php
new file mode 100644 (file)
index 0000000..3372571
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+include(dirname(__FILE__).'/../../bootstrap/functional.php');
+
+$browser = new sfTestFunctional(new sfBrowser());
+
+$browser->
+  get('/category/index')->
+
+  with('request')->begin()->
+    isParameter('module', 'category')->
+    isParameter('action', 'index')->
+  end()->
+
+  with('response')->begin()->
+    isStatusCode(200)->
+    checkElement('body', '!/This is a temporary page/')->
+  end()
+;