I hate Symfony and PHP.
all:
max_offices_on_pager: 3
max_ads_on_pager: 2
+ max_categories_on_pager: 2
default_language: eng # Everything must exist at least with this language
sf_guard_plugin:
//Just 1 user owns a company. Should this be improved?
$companyId = CompanyTable::getInstance()->findOneByUserId($userId)->getId();
- $query=OfficeTable::getInstance()->getOfficesByCompanyIdQuery($companyId);
+ //Sort list
+ $this->sort = $request->getParameter('sort', 'id');
+ $orderBy = $this->validateSort($this->sort);
+
+ $query=OfficeTable::getInstance()->getOfficesByCompanyIdWithSortQuery($companyId, $orderBy);
$this->pager = new sfDoctrinePager('Office', sfConfig::get('app_max_offices_on_pager'));
$this->pager->setQuery($query);
public function executeNew(sfWebRequest $request)
{
$this->form = new OfficeForm();
+ $this->sort = $request->getParameter('sort', 'id');
+ $this->page = $request->getParameter('page', 1);
}
public function executeCreate(sfWebRequest $request)
$this->form = new OfficeForm($officeInit);
- $this->processForm($request, $this->form);
+ $this->sort = $request->getParameter('sort', 'id');
+ $this->page = $request->getParameter('page', 1);
+
+ $this->processForm($request, $this->form, $this->sort, $this->page);
$this->setTemplate('new');
}
$this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id')));
+ $this->sort = $request->getParameter('sort', 'id');
+ $this->page = $request->getParameter('page', 1);
+
$this->form = new OfficeForm($office);
}
$this->form = new OfficeForm($office);
- $this->processForm($request, $this->form);
+ $this->sort = $request->getParameter('sort', 'id');
+ $this->page = $request->getParameter('page', 1);
+
+ $this->processForm($request, $this->form, $this->sort, $this->page);
$this->setTemplate('edit');
}
$office->delete();
- $this->redirect('office/index');
+ $sort = $request->getParameter('sort', 'id');
+ $page = $request->getParameter('page', 1);
+
+ $this->redirect('office/index?page='.$page.'&sort='.$sort);
}
- protected function processForm(sfWebRequest $request, sfForm $form)
+ protected function processForm(sfWebRequest $request, sfForm $form, $sort, $page)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$office = $form->save();
- $this->redirect('office/edit?id='.$office->getId());
+ $this->redirect('office/edit?id='.$office->getId().'&page='.$page.'&sort='.$sort);
}
}
$officeAds = OfficeAdsTable::getInstance()->findOneByOfficeId($officeId);
$this->form = new OfficeAdsForm($officeAds, array('companyId' => $companyOfficeId));
+
+ $this->sort = $request->getParameter('sort', 'id');
+ $this->page = $request->getParameter('page', 1);
+ $this->officeId = $officeId;
+ }
+
+ public function executeCreateLink(sfWebRequest $request)
+ {
+ $this->forward404Unless($request->isMethod(sfRequest::POST));
+
+ $officeId = $request->getParameter('officeId');
+
+ //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 company owned by that user and insert value in form
+ $companyOfficeId = OfficeTable::getInstance()->findOneById($officeId)->getCompanyId();
+
+ $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('officeId')));
+
+ $officeAdsInit = new OfficeAds();
+ $officeAdsInit->office_id = $officeId;
+
+ $this->form = new OfficeAdsForm($officeAdsInit, array('companyId' => $companyOfficeId));
+
+ $this->sort = $request->getParameter('sort', 'id');
+ $this->page = $request->getParameter('page', 1);
+ $this->officeId = $officeId;
+
+ $this->processAdsForm($request, $this->form, $this->sort, $this->page);
+
+ $this->setTemplate('link');
+ }
+
+ public function executeUpdateLink(sfWebRequest $request)
+ {
+ $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
+ $this->forward404Unless($officeAds = Doctrine_Core::getTable('OfficeAds')->find(array($request->getParameter('id'))), sprintf('Object office ads 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)
+ $officeAdsId = $request->getParameter('id');
+ $companyOfficeId = OfficeAdsTable::getInstance()->findOneById($officeAdsId)->getOffice()->getCompanyId();
+
+ $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Office does not exist (%s).', $request->getParameter('id')));
+
+
+ $this->form = new OfficeAdsForm($officeAds, array('companyId' => $companyOfficeId));
+
+ $this->sort = $request->getParameter('sort', 'id');
+ $this->page = $request->getParameter('page', 1);
+ $this->officeId = $officeAds->getOfficeId();
+
+ $this->processAdsForm($request, $this->form, $this->sort, $this->page);
+
+ $this->setTemplate('link');
+ }
+
+
+ protected function processAdsForm(sfWebRequest $request, sfForm $form, $sort, $page)
+ {
+ $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
+ if ($form->isValid())
+ {
+ $officeAds = $form->save();
+
+ $this->redirect('office/link?id='.$officeAds->getOfficeId().'&page='.$page.'&sort='.$sort);
+ }
+ }
+
+
+ /**
+ * We must validate the data coming from the user.
+ *
+ * @param sort value as string
+ * @return custom sort value, the office id by default
+ */
+ private function validateSort($sort)
+ {
+ switch ($sort) {
+ case "country":
+ $orderBy = "country.id";
+ break;
+ case "region":
+ $orderBy = "region.id";
+ break;
+ case "city":
+ $orderBy = "city.id";
+ break;
+ case "address":
+ $orderBy = "office_street_address";
+ break;
+ case "zip":
+ $orderBy = "office_zip";
+ break;
+ default:
+ $orderBy = "office.id";
+ }
+
+ return $orderBy;
}
}
<tr>
<td colspan="2">
<?php echo $form->renderHiddenFields(false) ?>
- <a href="<?php echo url_for('office/index') ?>"><?php echo __('Back to list') ?></a>
+ <a href="<?php echo url_for('office/index?'.'&page='.$page.'&sort='.$sort) ?>">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 echo link_to('Delete', 'office/delete?id='.$form->getObject()->getId().'&page='.$page.'&sort='.$sort, array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
<?php endif; ?>
<input type="submit" value=<?php echo __('Save') ?> />
</td>
<?php use_javascripts_for_form($form) ?>
<?php use_javascript('/sfFormExtraPlugin/js/double_list.js') ?>
-<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" ' ?>>
+<form action="<?php echo url_for('office/'.($form->getObject()->isNew() ? 'createLink' : 'updateLink').'?officeId='.$officeId.(!$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; ?>
<tr>
<td colspan="2">
<?php echo $form->renderHiddenFields(false) ?>
- <a href="<?php echo url_for('office/index') ?>"><?php echo __('Back to list') ?></a>
+ <a href="<?php echo url_for('office/index?page='.$page.'&sort='.$sort) ?>"><?php echo __('Back to list') ?></a>
<input type="submit" value=<?php echo __('Save') ?> />
</td>
</tr>
<table id="rounded-corner">
<thead>
<tr>
- <th scope="col" class="rounded-company"><?php echo __('Street Address') ?></th>
+ <th scope="col" class="rounded-company"><a href="<?php echo url_for('offices_index') ?>?page=<?php echo $page ?>&sort=country"><?php echo __('Country') ?></a></th>
+ <th scope="col" class="rounded"><a href="<?php echo url_for('offices_index') ?>?page=<?php echo $page ?>&sort=region"><?php echo __('Region') ?></a></th>
+ <th scope="col" class="rounded"><a href="<?php echo url_for('offices_index') ?>?page=<?php echo $page ?>&sort=city"><?php echo __('City') ?></a></th>
+ <th scope="col" class="rounded"><a href="<?php echo url_for('offices_index') ?>?page=<?php echo $page ?>&sort=address"><?php echo __('Street Address') ?></a></th>
+ <th scope="col" class="rounded"><a href="<?php echo url_for('offices_index') ?>?page=<?php echo $page ?>&sort=zip"><?php echo __('ZIP') ?></a></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>
+ <th scope="col" class="rounded-q4"></th>
</tr>
</thead>
<tfoot>
<tr>
- <td colspan="5" class="rounded-foot-left"><em><?php echo __('Offices List') ?></em></td>
+ <td colspan="9" 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->getCity()->getRegion()->getCountry() ?></td>
+ <td><?php echo $office->getCity()->getRegion() ?></td>
+ <td><?php echo $office->getCity() ?></td>
<td><?php echo $office->getOfficeStreetAddress() ?></td>
+ <td><?php echo $office->getOfficeZip() ?></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 href="<?php echo url_for('office/link?id='.$office->getId()) ?>"><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>
+ <td><a href="<?php echo url_for('office/edit?id='.$office->getId().'&page='.$page.'&sort='.$sort) ?>"><img src="/images/pencil_add.png" alt="" title="" border="0" /></a></td>
+ <td><a href="<?php echo url_for('office/link?id='.$office->getId().'&page='.$page.'&sort='.$sort) ?>"><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().'&page='.$page.'&sort='.$sort, array('method' => 'delete', 'confirm' => 'Are you sure?')) ?></td>
</tr>
<?php endforeach; ?>
<h2><?php echo __('Edit Office') ?></h2>
-<?php include_partial('formCustomOffice', array('form' => $form)) ?>
+<?php include_partial('formCustomOffice', array('form' => $form, 'page' => $page, 'sort' => $sort)) ?>
-<h2><?php echo __('Offices List') ?></h2>
+<h2><?php echo __('Offices Index') ?></h2>
+
+<?php if ($pager->haveToPaginate()): ?>
+ <?php include_partial('office/list', array('offices' => $pager->getResults(), 'page' => $pager->getPage(), 'sort' => $sort)) ?>
+<?php else: ?>
+ <?php include_partial('office/list', array('offices' => $pager->getResults(), 'page' => '1', 'sort' => $sort)) ?>
+<?php endif; ?>
-<?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=1&sort=<?php echo $sort ?>"><?php echo __('first page') ?></a>
- <a href="<?php echo url_for('offices_index') ?>?page=<?php echo $pager->getPreviousPage() ?>"><?php echo __('<< prev') ?></a>
+ <a href="<?php echo url_for('offices_index') ?>?page=<?php echo $pager->getPreviousPage() ?>&sort=<?php echo $sort ?>"><?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>
+ <a href="<?php echo url_for('offices_index') ?>?page=<?php echo $page ?>&sort=<?php echo $sort ?>"><?php echo $page ?></a>
<?php endif; ?>
<?php endforeach; ?>
- <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->getNextPage() ?>&sort=<?php echo $sort ?>"><?php echo __('next >>') ?></a>
- <a href="<?php echo url_for('offices_index') ?>?page=<?php echo $pager->getLastPage() ?>"><?php echo __('last page') ?></a>
+ <a href="<?php echo url_for('offices_index') ?>?page=<?php echo $pager->getLastPage() ?>&sort=<?php echo $sort ?>"><?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>
+ <a href="<?php echo url_for('office/new?page='.$page.'&sort='.$sort) ?>" class="bt_green"><span class="bt_green_lft"></span><strong><?php echo __('Create new Office') ?></strong><span class="bt_green_r"></span></a>
<h2><?php echo __('Link to Ads') ?></h2>
-<?php include_partial('formCustomOfficeAds', array('form' => $form)) ?>
+<?php include_partial('formCustomOfficeAds', array('form' => $form, 'page' => $page, 'sort' => $sort, 'officeId' => $officeId)) ?>
<h2><?php echo __('New Office') ?></h2>
-<?php include_partial('formCustomOffice', array('form' => $form)) ?>
+<?php include_partial('formCustomOffice', array('form' => $form, 'page' => $page, 'sort' => $sort)) ?>
--- /dev/null
+<?php
+
+/**
+ * Max items per page.
+ *
+ * Combobox to show the available languages.
+ *
+ * @package mobiads
+ * @subpackage form
+ * @author Gustavo Martin Morcuende
+ * @version
+ */
+class MaxItemsPerPage extends sfFormSymfony
+{
+ public function setup()
+ {
+ $this->setWidgets(array('max_items' => new sfWidgetFormInputText(),));
+
+ $this->setValidators(array('max_items' => new sfValidatorNumber(array('required' => true,
+ 'trim' => true),
+ array('required' => 'The latitude field is required')),));
+
+
+ $this->widgetSchema->setNameFormat('max_items_page[%s]');
+
+
+ $this->widgetSchema->setLabels(array('max_items' => 'Max items per page: ',));
+
+
+ $this->validatorSchema->setOption('allow_extra_fields', false);
+ $this->validatorSchema->setOption('filter_extra_fields', true);
+
+ $this->widgetSchema->setFormFormatterName('table');
+
+ $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
+
+ //i18n (Internationalization)
+ $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('max_items_form');
+
+ parent::setup();
+ }
+}
*/
class OfficeAdsForm extends BaseOfficeAdsForm
{
+ protected $scheduledForSave = array();
+
+
public function configure()
{
//Narrow down options.
'renderer_class' => 'sfWidgetFormSelectDoubleList',
'query' => $query));
+ $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['ad_id'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Ad'),
'multiple' => true,
'query' => $query));
+
+ $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('office_ads_form');
}
/**
$this->setDefault('ad_id', $already_chosen);
}
}
+
+
+ /**
+ * Overriding doSave method from lib/vendor/symfony/lib/form/addon/sfFormObject.class.php
+ *
+ * We are updating the data base in just 1 transaction
+ */
+ protected function doSave($con = null)
+ {
+
+ if (null === $con)
+ {
+ $con = $this->getConnection();
+ }
+
+
+ foreach ($this->scheduledForSave as $index => $value)
+ {
+ $value->save($con);
+ }
+ }
+
+
+ /**
+ * Overriding doBind method
+ *
+ * TODO: I am breaking the validations. How could I do this in a right way?
+ */
+ protected function doBind(array $values)
+ {
+ if (!isset($values['ad_id']))
+ {
+ if (!$this->getObject()->isNew())
+ {
+ $officeAds = OfficeAdsTable::getInstance()->findByOfficeId($this->getObject()->getOfficeId());
+
+ foreach ($officeAds as $officeAd)
+ {
+ $officeAd->delete();
+ }
+ }
+ return;
+ }
+
+ $officeAds = OfficeAdsTable::getInstance()->findByOfficeId($this->getObject()->getOfficeId());
+
+ foreach ($values['ad_id'] as $index => $value)
+ {
+ if (!$this->getObject()->isNew())
+ {
+ foreach ($officeAds as $officeAd)
+ {
+ if ($officeAd->getAdId() == $value)
+ continue 2;
+ }
+ }
+ $officeAds = new OfficeAds();
+ $officeAds->office_id = $this->getObject()->getOfficeId();
+ $officeAds->ad_id = $value;
+ $this->scheduledForSave[$index] = $officeAds;
+ }
+ }
}
'office_street_address' => 'Address: ',
'office_zip' => 'ZIP:',));
+
+ //In the future the companies could pay for improvements in their accounts (premium accounts)
+ //1 premium company could avoid to other companies on the same GPS point. This validator should check if that GPS point is being used by
+ //another company and if that company is premium or not.
+ $this->validatorSchema->setPostValidator(
+ new sfValidatorAnd(array(
+ new sfValidatorDoctrineUnique(array('model' => $this->getModelName(), 'column' => array('office_gps'))),
+ ))
+ );
+
+
$this->validatorSchema->setOption('allow_extra_fields', false);
$this->validatorSchema->setOption('filter_extra_fields', true);
*
* @return string
*/
- public function __toString()
- {
- return (string) $this->getCityName();
- }
+ public function __toString()
+ {
+ return (string) $this->getCityName();
+ }
}
*/
class Country extends BaseCountry
{
+ /**
+ * Returns the string representation of this object.
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return (string) $this->getCountryName();
+ }
}
*
* @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 Office extends BaseOffice
{
/**
- * Returns offices by company id.
+ * Returns offices by company id with sort
*
* @return related offices to a company id.
+ * TODO: order by longitude and latitude
*/
- public function getOfficesByCompanyIdQuery($companyId)
+ public function getOfficesByCompanyIdWithSortQuery($companyId, $sort)
{
return $this->createQuery('office')->where('office.company_id = ?', $companyId)
- ->orderBy('office.id');
+ ->innerjoin('office.City city')
+ ->innerjoin('city.Region region')
+ ->innerjoin('region.Country country')
+ ->orderBy($sort);
}
}
*
* @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 Region extends BaseRegion
{
+ /**
+ * Returns the string representation of this object.
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return (string) $this->getRegionName();
+ }
}
--- /dev/null
+../plugins/sfFormExtraPlugin/web
\ No newline at end of file