Right now very simple, wihtout using javascript.
In the future many improvements if I use javascript.
# 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
param: { module: office, action: index }
options: { model: Office, type: object }
-office_index:
+offices_index:
url: /office/index
class: sfDoctrineRoute
param: { module: office, action: index }
{
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)
{
$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);
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);
}
{
$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);
$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');
--- /dev/null
+<?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) ?>
+ <a href="<?php echo url_for('office/index') ?>"><?php echo __('Back to list') ?></a>
+ <?php if (!$form->getObject()->isNew()): ?>
+ <?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>
--- /dev/null
+<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"> </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>
-<h1>Edit Office</h1>
+<h1><?php echo __('Edit Office') ?></h1>
-<?php include_partial('form', array('form' => $form)) ?>
+<?php include_partial('formCustomOffice', array('form' => $form)) ?>
-<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>
+
+
+
<h1>New Office</h1>
-<?php include_partial('form', array('form' => $form)) ?>
+<?php include_partial('formCustomOffice', array('form' => $form)) ?>
<?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());
}
}
*
* @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();
+ }
}
{
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;
+ }
+}
*/
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;
+ }
+
}
{
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');
+ }
+}
+
--- /dev/null
+<?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');
+ }
+
+}