Web pages for company data management.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 21 May 2012 00:09:39 +0000 (02:09 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 21 May 2012 00:09:39 +0000 (02:09 +0200)
apps/companyfront/config/routing.yml
apps/companyfront/modules/company/actions/actions.class.php [new file with mode: 0644]
apps/companyfront/modules/company/templates/_form.php [new file with mode: 0644]
apps/companyfront/modules/company/templates/editSuccess.php [new file with mode: 0644]
apps/companyfront/modules/company/templates/indexSuccess.php [new file with mode: 0644]
apps/companyfront/modules/company/templates/newSuccess.php [new file with mode: 0644]
apps/companyfront/modules/company/templates/showSuccess.php [new file with mode: 0644]
lib/form/doctrine/CompanyDescriptionForm.class.php
lib/form/doctrine/CompanyForm.class.php
test/functional/companyfront/companyActionsTest.php [new file with mode: 0644]

index 927ab60..41a4507 100644 (file)
@@ -5,8 +5,14 @@
 homepage:
   url:     /
   class:   sfDoctrineRoute
-  param:   { module: office, action: index }
-  options: { model: Office, type: object }
+  param:   { module: company, action: index }
+  options: { model: Company, type: object }
+
+company_index:
+  url:     /company/index
+  class:   sfDoctrineRoute
+  param:   { module: company, action: index }
+  options: { model: Company, type: object }
 
 offices_index:
   url:     /office/index
diff --git a/apps/companyfront/modules/company/actions/actions.class.php b/apps/companyfront/modules/company/actions/actions.class.php
new file mode 100644 (file)
index 0000000..b483044
--- /dev/null
@@ -0,0 +1,134 @@
+<?php
+
+/**
+ * company actions.
+ *
+ * @package    mobiads
+ * @subpackage company
+ * @author     Gustavo Martin Morcuende
+ * @version
+ */
+class companyActions extends sfActions
+{
+  public function executeIndex(sfWebRequest $request)
+  {
+    $this->companys = Doctrine_Core::getTable('Company')
+      ->createQuery('a')
+      ->execute();
+
+
+    //Get user Id
+    $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();
+
+    $languageCode = $request->getParameter('language');
+    if ($languageCode != null)
+    {
+        $language = LanguageTable::getInstance()->findOneByCode($languageCode);
+        if ($language == null)
+        {
+            //By default we use the current user's language.
+            $language = $this->getUser()->getGuardUser()->getLanguage();
+        }
+    }
+    else
+    {
+        //By default we use the current user's language.
+        $language = $this->getUser()->getGuardUser()->getLanguage();
+    }
+
+    //Retrieve Doctrine Record (just one company for user)
+    $this->company = CompanyDescriptionTable::getInstance()->findOneByCompanyIdAndLanguageId($companyId, $language->getId());
+  }
+
+  public function executeShow(sfWebRequest $request)
+  {
+    $this->company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id')));
+    $this->forward404Unless($this->company);
+  }
+
+  public function executeNew(sfWebRequest $request)
+  {
+    $this->form = new CompanyForm();
+  }
+
+  public function executeCreate(sfWebRequest $request)
+  {
+    $this->forward404Unless($request->isMethod(sfRequest::POST));
+
+    $this->form = new CompanyForm();
+
+    $this->processForm($request, $this->form);
+
+    $this->setTemplate('new');
+  }
+
+  public function executeEdit(sfWebRequest $request)
+  {
+    $this->forward404Unless($company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id'))), sprintf('Object company 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)
+    $id = $request->getParameter('id');
+
+    $companyId = CompanyTable::getInstance()->findOneById($id)->getId();
+
+    $this->forward404Unless($companyId == $companyUserId, sprintf('Company does not exist (%s).', $request->getParameter('id')));
+
+    $this->form = new CompanyForm($company);
+  }
+
+  public function executeUpdate(sfWebRequest $request)
+  {
+    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
+    $this->forward404Unless($company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id'))), sprintf('Object company 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)
+    $id = $request->getParameter('id');
+
+    $companyId = CompanyTable::getInstance()->findOneById($id)->getId();
+
+    $this->forward404Unless($companyId == $companyUserId, sprintf('Company does not exist (%s).', $request->getParameter('id')));
+
+    $this->form = new CompanyForm($company);
+
+    $this->processForm($request, $this->form);
+
+    $this->setTemplate('edit');
+  }
+
+  public function executeDelete(sfWebRequest $request)
+  {
+    $request->checkCSRFProtection();
+
+    $this->forward404Unless($company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id'))), sprintf('Object company does not exist (%s).', $request->getParameter('id')));
+    $company->delete();
+
+    $this->redirect('company/index');
+  }
+
+  protected function processForm(sfWebRequest $request, sfForm $form)
+  {
+    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
+    if ($form->isValid())
+    {
+      $company = $form->save();
+
+      $this->redirect('company/edit?id='.$company->getId());
+    }
+  }
+}
diff --git a/apps/companyfront/modules/company/templates/_form.php b/apps/companyfront/modules/company/templates/_form.php
new file mode 100644 (file)
index 0000000..9b6d065
--- /dev/null
@@ -0,0 +1,23 @@
+<?php use_stylesheets_for_form($form) ?>
+<?php use_javascripts_for_form($form) ?>
+
+<form action="<?php echo url_for('company/'.($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('company/index') ?>"><?php echo __('Back to list') ?></a>
+          <input type="submit" value=<?php echo __('Update') ?> />
+        </td>
+      </tr>
+    </tfoot>
+    <tbody>
+      <?php echo $form->renderGlobalErrors(false) ?>
+      <?php echo $form ?>
+    </tbody>
+  </table>
+</form>
diff --git a/apps/companyfront/modules/company/templates/editSuccess.php b/apps/companyfront/modules/company/templates/editSuccess.php
new file mode 100644 (file)
index 0000000..a55e220
--- /dev/null
@@ -0,0 +1,4 @@
+<h2><?php echo __('Edit Your Data Company') ?></h2>
+
+
+<?php include_partial('form', array('form' => $form)) ?>
diff --git a/apps/companyfront/modules/company/templates/indexSuccess.php b/apps/companyfront/modules/company/templates/indexSuccess.php
new file mode 100644 (file)
index 0000000..6db6761
--- /dev/null
@@ -0,0 +1,24 @@
+<h2><?php echo __('Your Company') ?></h2>
+
+<table id="rounded-corner">
+  <thead>
+    <tr>
+      <th scope="col" class="rounded-company"><?php echo __('Name') ?></th>
+      <th scope="col" class="rounded-q4"><?php echo __('CIF') ?></th>
+    </tr>
+  </thead>
+  <tfoot>
+    <tr>
+        <td colspan="1" class="rounded-foot-left"><em><?php echo __('Your data company') ?></em></td>
+        <td class="rounded-foot-right">&nbsp;</td>
+    </tr>
+  </tfoot>
+  <tbody>
+    <tr>
+      <td><?php echo $company->getCompanyName() ?></td>
+      <td><?php echo $company->getCompany()->getCompanyCif() ?></td>
+    </tr>
+  </tbody>
+</table>
+
+<a href="<?php echo url_for('company/edit?id='.$company->getCompany()->getId()) ?>" class="bt_green"><span class="bt_green_lft"></span><strong><?php echo __('Edit Your Company') ?></strong><span class="bt_green_r"></span></a>
diff --git a/apps/companyfront/modules/company/templates/newSuccess.php b/apps/companyfront/modules/company/templates/newSuccess.php
new file mode 100644 (file)
index 0000000..a49c584
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>New Company</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
diff --git a/apps/companyfront/modules/company/templates/showSuccess.php b/apps/companyfront/modules/company/templates/showSuccess.php
new file mode 100644 (file)
index 0000000..365f64f
--- /dev/null
@@ -0,0 +1,22 @@
+<table>
+  <tbody>
+    <tr>
+      <th>Id:</th>
+      <td><?php echo $company->getId() ?></td>
+    </tr>
+    <tr>
+      <th>User:</th>
+      <td><?php echo $company->getUserId() ?></td>
+    </tr>
+    <tr>
+      <th>Company cif:</th>
+      <td><?php echo $company->getCompanyCif() ?></td>
+    </tr>
+  </tbody>
+</table>
+
+<hr />
+
+<a href="<?php echo url_for('company/edit?id='.$company->getId()) ?>">Edit</a>
+&nbsp;
+<a href="<?php echo url_for('company/index') ?>">List</a>
index e810b3d..5de1ec1 100644 (file)
@@ -5,12 +5,25 @@
  *
  * @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 CompanyDescriptionForm extends BaseCompanyDescriptionForm
 {
   public function configure()
   {
+    unset($this['company_id']);
+
+    $this->widgetSchema->setLabels(array('language_id' => 'Language: '));
+    $this->widgetSchema->setLabels(array('company_name' => 'Company Name: '));
+
+    if ($this->object->exists())
+    {
+      $this->widgetSchema['delete'] = new sfWidgetFormInputCheckbox();
+      $this->validatorSchema['delete'] = new sfValidatorPass();
+    }
+
+    //i18n (Internationalization)
+    $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('company_description_form');
   }
 }
index 08f6a47..89a32c3 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 CompanyForm extends BaseCompanyForm
 {
+  /*Stores Doctrine Records to be removed from the database.*/
+  protected $scheduledForDeletion = array();
+
   public function configure()
   {
+    $this->useFields(array('company_cif'));
+
+
+    $this->widgetSchema->setLabels(array('company_cif' => 'CIF: '));
+
+
+    //Company create new description form
+    $companyDescription = new CompanyDescription();
+    $companyDescription->Company = $this->getObject();
+    $newCompanyDescriptionForm = new CompanyDescriptionForm($companyDescription);
+
+    $this->embedForm('new', $newCompanyDescriptionForm);
+
+    $this->embedRelation('CompanyDescription');
+
+
+    //i18n (Internationalization)
+    $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('company_form');
+  }
+
+  protected function doBind(array $values)
+  {
+    if ('' === trim($values['new']['company_name']))
+    {
+      unset($values['new'], $this['new']);
+    }
+
+    if (isset($values['CompanyDescription']))
+    {
+      foreach ($values['CompanyDescription'] as $i => $companyDescriptionValues)
+      {
+        if (isset($companyDescriptionValues['delete']) && $companyDescriptionValues['id'])
+        {
+          $this->scheduledForDeletion[$i] = $companyDescriptionValues['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['CompanyDescription'][$index]);
+        unset($this->object['CompanyDescription'][$index]);
+        Doctrine::getTable('CompanyDescription')->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());
+      }
+    }
   }
 }
diff --git a/test/functional/companyfront/companyActionsTest.php b/test/functional/companyfront/companyActionsTest.php
new file mode 100644 (file)
index 0000000..1e4caa9
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+include(dirname(__FILE__).'/../../bootstrap/functional.php');
+
+$browser = new sfTestFunctional(new sfBrowser());
+
+$browser->
+  get('/company/index')->
+
+  with('request')->begin()->
+    isParameter('module', 'company')->
+    isParameter('action', 'index')->
+  end()->
+
+  with('response')->begin()->
+    isStatusCode(200)->
+    checkElement('body', '!/This is a temporary page/')->
+  end()
+;