Create, edit, remove office.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 14 May 2012 00:04:11 +0000 (02:04 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 14 May 2012 00:04:11 +0000 (02:04 +0200)
Right now very simple, wihtout using javascript.
In the future many improvements if I use javascript.

14 files changed:
apps/companyfront/config/app.yml
apps/companyfront/config/routing.yml
apps/companyfront/modules/office/actions/actions.class.php
apps/companyfront/modules/office/templates/_formCustomOffice.php [new file with mode: 0644]
apps/companyfront/modules/office/templates/_list.php [new file with mode: 0644]
apps/companyfront/modules/office/templates/editSuccess.php
apps/companyfront/modules/office/templates/indexSuccess.php
apps/companyfront/modules/office/templates/newSuccess.php
lib/form/doctrine/OfficeForm.class.php
lib/model/doctrine/City.class.php
lib/model/doctrine/CityTable.class.php
lib/model/doctrine/Office.class.php
lib/model/doctrine/OfficeTable.class.php
lib/widget/sfWidgetFormInputFloat.class.php [new file with mode: 0644]

index 4e4dcc4..1bd9aa3 100644 (file)
@@ -3,7 +3,7 @@
 
 # default values
 all:
-  max_jobs_on_category: 3
+  max_offices_on_pager: 3
 
   sf_guard_plugin:
      remember_key_expiration_age: 2592000             # 30 days in seconds
index b27bbb3..cd57e68 100644 (file)
@@ -8,7 +8,7 @@ homepage:
   param:   { module: office, action: index }
   options: { model: Office, type: object }
 
-office_index:
+offices_index:
   url:     /office/index
   class:   sfDoctrineRoute
   param:   { module: office, action: index }
index ce035a7..b4e3d73 100644 (file)
@@ -12,9 +12,22 @@ class officeActions extends sfActions
 {
   public function executeIndex(sfWebRequest $request)
   {
-    $this->offices = Doctrine_Core::getTable('Office')
-      ->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();
+
+    $this->offices = Doctrine_Core::getTable('Office')->findByCompanyId($companyId);
+
+    $query=OfficeTable::getInstance()->getOfficesByCompanyIdQuery($companyId);
+
+    $this->pager = new sfDoctrinePager('Office', sfConfig::get('app_max_offices_on_pager'));
+    $this->pager->setQuery($query);
+    $this->pager->setPage($request->getParameter('page', 1));
+    $this->pager->init();
+
   }
 
   public function executeShow(sfWebRequest $request)
@@ -32,7 +45,15 @@ class officeActions extends sfActions
   {
     $this->forward404Unless($request->isMethod(sfRequest::POST));
 
-    $this->form = new OfficeForm();
+    $officeInit = new Office();
+
+    //Get user Id
+    $userId = $this->getUser()->getGuardUser()->getId();
+
+    //Get company owned by that user and insert value in form
+    $officeInit->company_id = CompanyTable::getInstance()->findOneByUserId($userId)->getId();
+
+    $this->form = new OfficeForm($officeInit);
 
     $this->processForm($request, $this->form);
 
@@ -42,6 +63,20 @@ class officeActions extends sfActions
   public function executeEdit(sfWebRequest $request)
   {
     $this->forward404Unless($office = Doctrine_Core::getTable('Office')->find(array($request->getParameter('id'))), sprintf('Object office 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)
+    $officeId = $request->getParameter('id');
+
+    $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId();
+
+    $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id')));
+
     $this->form = new OfficeForm($office);
   }
 
@@ -49,6 +84,20 @@ class officeActions extends sfActions
   {
     $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
     $this->forward404Unless($office = Doctrine_Core::getTable('Office')->find(array($request->getParameter('id'))), sprintf('Object office 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)
+    $officeId = $request->getParameter('id');
+
+    $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId();
+
+    $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id')));
+
     $this->form = new OfficeForm($office);
 
     $this->processForm($request, $this->form);
@@ -61,6 +110,20 @@ class officeActions extends sfActions
     $request->checkCSRFProtection();
 
     $this->forward404Unless($office = Doctrine_Core::getTable('Office')->find(array($request->getParameter('id'))), sprintf('Object office 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)
+    $officeId = $request->getParameter('id');
+
+    $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId();
+
+    $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id')));
+
     $office->delete();
 
     $this->redirect('office/index');
diff --git a/apps/companyfront/modules/office/templates/_formCustomOffice.php b/apps/companyfront/modules/office/templates/_formCustomOffice.php
new file mode 100644 (file)
index 0000000..8d1b4b6
--- /dev/null
@@ -0,0 +1,25 @@
+<?php use_stylesheets_for_form($form) ?>
+<?php use_javascripts_for_form($form) ?>
+
+<form action="<?php echo url_for('office/'.($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('office/index') ?>"><?php echo __('Back to list') ?></a>
+          <?php if (!$form->getObject()->isNew()): ?>
+            &nbsp;<?php echo link_to('Delete', 'office/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 ?>
+    </tbody>
+  </table>
+</form>
diff --git a/apps/companyfront/modules/office/templates/_list.php b/apps/companyfront/modules/office/templates/_list.php
new file mode 100644 (file)
index 0000000..67c935d
--- /dev/null
@@ -0,0 +1,31 @@
+<table id="rounded-corner">
+  <thead>
+    <tr>
+         <th scope="col" class="rounded-company"><?php echo __('Street Address') ?></th>
+         <th scope="col" class="rounded"><?php echo __('Longitude') ?></th>
+         <th scope="col" class="rounded"><?php echo __('Latitude') ?></th>
+      <th scope="col" class="rounded"><?php echo __('Edit') ?></th>
+      <th scope="col" class="rounded"><?php echo __('Link to Ads') ?></th>
+      <th scope="col" class="rounded-q4"><?php echo __('Remove') ?></th>
+    </tr>
+  </thead>
+  <tfoot>
+    <tr>
+        <td colspan="5" class="rounded-foot-left"><em><?php echo __('Offices List') ?></em></td>
+        <td class="rounded-foot-right">&nbsp;</td>
+    </tr>
+  </tfoot>
+  <tbody>
+    <?php foreach ($offices as $office): ?>
+    <tr>
+      <td><?php echo $office->getOfficeStreetAddress() ?></td>
+      <td><?php echo $office->getLongitude() ?></td>
+         <td><?php echo $office->getLatitude() ?></td>
+         <td><a href="<?php echo url_for('office/edit?id='.$office->getId()) ?>"><img src="/images/pencil_add.png" alt="" title="" border="0" /></a></td>
+      <td><a><img src="/images/link.png" alt="" title="" border="0" /></a></td>
+      <td><?php echo link_to('<img src="/images/inadminpanel/images/trash.png" alt="" title="" border="0" />', 'office/delete?id='.$office->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?></td>
+
+    </tr>
+    <?php endforeach; ?>
+  </tbody>
+</table>
index 8e1c570..3ebc182 100644 (file)
@@ -1,3 +1,3 @@
-<h1>Edit Office</h1>
+<h1><?php echo __('Edit Office') ?></h1>
 
-<?php include_partial('form', array('form' => $form)) ?>
+<?php include_partial('formCustomOffice', array('form' => $form)) ?>
index a2b7a2d..2908f40 100644 (file)
@@ -1,32 +1,28 @@
-<h1>Offices List</h1>
-
-<table>
-  <thead>
-    <tr>
-      <th>Id</th>
-      <th>Company</th>
-      <th>City</th>
-      <th>Office gps</th>
-      <th>Office street address</th>
-      <th>Office zip</th>
-      <th>Created at</th>
-      <th>Updated at</th>
-    </tr>
-  </thead>
-  <tbody>
-    <?php foreach ($offices as $office): ?>
-    <tr>
-      <td><a href="<?php echo url_for('office/show?id='.$office->getId()) ?>"><?php echo $office->getId() ?></a></td>
-      <td><?php echo $office->getCompanyId() ?></td>
-      <td><?php echo $office->getCityId() ?></td>
-      <td><?php echo $office->getOfficeGps() ?></td>
-      <td><?php echo $office->getOfficeStreetAddress() ?></td>
-      <td><?php echo $office->getOfficeZip() ?></td>
-      <td><?php echo $office->getCreatedAt() ?></td>
-      <td><?php echo $office->getUpdatedAt() ?></td>
-    </tr>
+<h2><?php echo __('Offices List') ?></h2>
+
+<?php include_partial('office/list', array('offices' => $pager->getResults())) ?>
+
+<?php if ($pager->haveToPaginate()): ?>
+  <div class="pagination">
+    <a href="<?php echo url_for('offices_index') ?>?page=1"><?php echo __('first page') ?></a>
+
+    <a href="<?php echo url_for('offices_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('offices_index') ?>?page=<?php echo $page ?>"><?php echo $page ?></a>
+      <?php endif; ?>
     <?php endforeach; ?>
-  </tbody>
-</table>
 
-  <a href="<?php echo url_for('office/new') ?>">New</a>
+    <a href="<?php echo url_for('offices_index') ?>?page=<?php echo $pager->getNextPage() ?>"><?php echo __('next >>') ?></a>
+
+    <a href="<?php echo url_for('offices_index') ?>?page=<?php echo $pager->getLastPage() ?>"><?php echo __('last page') ?></a>
+  </div>
+<?php endif; ?>
+
+  <a href="<?php echo url_for('office/new') ?>" class="bt_green"><span class="bt_green_lft"></span><strong><?php echo __('Create new Office') ?></strong><span class="bt_green_r"></span></a>
+
+
+
index 9113985..ceb3620 100644 (file)
@@ -1,3 +1,3 @@
 <h1>New Office</h1>
 
-<?php include_partial('form', array('form' => $form)) ?>
+<?php include_partial('formCustomOffice', array('form' => $form)) ?>
index ba3f4f9..2e79330 100644 (file)
 <?php
 
 /**
- * Office form.
+ * Custom office form.
  *
  * @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 OfficeForm extends BaseOfficeForm
 {
   public function configure()
   {
+    $this->useFields(array('city_id', 'office_street_address', 'office_zip'));
+
+
+
+    $this->widgetSchema['longitude'] = new sfWidgetFormInputFloat();
+    $this->widgetSchema['latitude'] = new sfWidgetFormInputFloat();
+
+
+    $this->validatorSchema['longitude'] =  new sfValidatorNumber(array('max' => 180,
+                                                                       'min' => -180,
+                                                                       'required' => true,
+                                                                       'trim' => true),
+                                                                 array('invalid'  => 'Wrong Longitude',
+                                                                       'required' => 'The longitude field is required',
+                                                                       'max'      => 'Longitude "%value%" must not exceed the %max% value',
+                                                                       'min'      => 'Longitude "%value%" must be equal or higher than %min%'));
+
+
+
+    $this->validatorSchema['latitude'] = new sfValidatorNumber(array('max' => 90,
+                                                                     'min' => -90,
+                                                                     'required' => true,
+                                                                     'trim' => true),
+                                                               array('invalid'  => 'Wrong Latitude',
+                                                                     'required' => 'The latitude field is required',
+                                                                     'max'      => 'Latitude "%value%" must not exceed the %max% value',
+                                                                     'min'      => 'Latitude "%value%" must be equal or higher than %min%'));
+
+    $this->validatorSchema['city_id'] = new sfValidatorDoctrineChoice(array('model'    => $this->getRelatedModelName('City'),
+                                                                            'required' => false,
+                                                                            'query'    => $cityNamesQuery));
+
+
+
+    $this->widgetSchema->setLabels(array('city_id'               => 'City: ',
+                                         'longitude'             => 'Longitude (180 to -180): ',
+                                         'latitude'              => 'Latitude (90 to -90): ',
+                                         'office_street_address' => 'Address: ',
+                                         'office_zip'            => 'ZIP:',));
+
+    $this->validatorSchema->setOption('allow_extra_fields', false);
+    $this->validatorSchema->setOption('filter_extra_fields', true);
+
+    //i18n (Internationalization)
+    //See apps/companyfront/modules/office/i18n/office_form.es.xml file
+    $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('office_form');
+  }
+
+
+ /**
+  * Overriding doSave method from lib/vendor/symfony/lib/form/addon/sfFormObject.class.php
+  *
+  * We are updating the data base in just 1 transaction
+  * In case of unsetting longitude or latitude fields you will have to override this method.
+  * TODO: create a Doctrine_Record for PostGIS
+  */
+  protected function doSave($con = null)
+  {
+    parent::doSave($con);
+
+    //Get latitude and longitude values. They will be translated to GEOGRAPHIC data.
+    foreach ($this->values as $field => $value)
+    {
+        if ($field == 'longitude')
+            $longitude = $value;
+        if ($field == 'latitude')
+            $latitude = $value;
+    }
+    //Catch id element. We will use this id to insert the PostGIS value in the right row.
+    $arrowId = $this->getObject()->getId();
+    //Update PostGIS
+    //This connection will throw exception in case of error.
+    Doctrine_Manager::connection()->execute("UPDATE office SET office_gps=ST_GeographyFromText('SRID=4326;POINT($longitude $latitude)') WHERE id=$arrowId");
+  }
+
+
+ /**
+  * Overriding updateDefaultsFromObject method from lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php
+  *
+  * TODO: create a Doctrine_Record for PostGIS
+  */
+  protected function updateDefaultsFromObject()
+  {
+    parent::updateDefaultsFromObject();
+
+    $this->setDefault('longitude', $this->getObject()->getLongitude());
+    $this->setDefault('latitude', $this->getObject()->getLatitude());
   }
 }
index a23b985..ce0018a 100644 (file)
@@ -7,9 +7,18 @@
  * 
  * @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 City extends BaseCity
 {
+  /**
+   * Returns the string representation of this object.
+   *
+   * @return string
+   */
+  public function __toString()
+  {
+    return (string) $this->getCityName();
+  }
 }
index 7cc5f01..d985b2d 100644 (file)
@@ -16,4 +16,18 @@ class CityTable extends Doctrine_Table
     {
         return Doctrine_Core::getTable('City');
     }
-}
\ No newline at end of file
+
+
+   /**
+    * Returns a query which is able to recover the city_name field of a City Table
+    *
+    * @return object Doctrine_Query
+    */
+    public function getCityNameQuery()
+    {
+        $query = $this->createQuery('city')
+                        ->select('city.city_name');
+
+        return $query;
+    }
+}
index 9992e32..0784d22 100644 (file)
  */
 class Office extends BaseOffice
 {
+    public function getGpsST_AsText()
+    {
+        $aux=$this->getOfficeGps();
+        if ($aux)
+        {
+            //Using a PostGIS query to convert a GIS value? This is a bit strange
+            $results=Doctrine_Manager::getInstance()->getCurrentConnection()->fetchColumn("SELECT ST_AsText('$aux')");
+            return trim($results['0'], "POINT()");
+        }
+        else
+            return 0;
+    }
+
+    public function getLongitude()
+    {
+        $gpsST_AsText=$this->getGpsST_AsText();
+
+        $longitude = strstr($gpsST_AsText, ' ', true);
+
+        return $longitude;
+    }
+
+    public function getLatitude()
+    {
+        $gpsST_AsText=$this->getGpsST_AsText();
+
+        $latitude = strstr($gpsST_AsText, ' ');
+
+        return $latitude;
+    }
+
 }
index e58f275..2d0bb0f 100644 (file)
@@ -16,4 +16,17 @@ class OfficeTable extends Doctrine_Table
     {
         return Doctrine_Core::getTable('Office');
     }
-}
\ No newline at end of file
+
+
+   /**
+    * Returns offices by company id.
+    *
+    * @return related offices to a company id.
+    */
+    public function getOfficesByCompanyIdQuery($companyId)
+    {
+         return $this->createQuery('office')->where('office.company_id = ?', $companyId)
+                                            ->orderBy('office.id');
+    }
+}
+
diff --git a/lib/widget/sfWidgetFormInputFloat.class.php b/lib/widget/sfWidgetFormInputFloat.class.php
new file mode 100644 (file)
index 0000000..fc3e0a4
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * sfWidgetFormInputFloat congigures float type HTML attribute (type="float" in input HTML field)
+ *
+ * @package    mobiads
+ * @subpackage widget
+ * @author     Gustavo Martin Morcuende
+ * @version    
+ */
+class sfWidgetFormInputFloat extends sfWidgetFormInputText
+{
+       /**
+        * Configures the current widget.
+        *
+        * @param array $options     An array of options
+        * @param array $attributes  An array of default HTML attributes
+        *
+        * @see sfWidgetForm
+        */
+
+       protected function configure($options = array(), $attributes = array())
+       {
+           parent::configure($options, $attributes);
+       
+               $this->setOption('type', 'float');
+       }
+
+}