Index web page with internationalization.
We can change the shown language without JavaScript.
# default values
all:
max_offices_on_pager: 3
+ max_ads_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 }
+ads_index:
+ url: /ad/index
+ class: sfDoctrineRoute
+ param: { module: ad, action: index }
+ options: { model: Ad, type: object }
+
# generic rules
# please, remove them by adding more specific rules
default_index:
--- /dev/null
+<?php
+
+/**
+ * ad actions.
+ *
+ * @package mobiads
+ * @subpackage ad
+ * @author Gustavo Martin Morcuende
+ * @version
+ */
+class adActions extends sfActions
+{
+ public function executeIndex(sfWebRequest $request)
+ {
+ //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();
+ }
+
+
+ //Init combobox with the right value.
+ $this->formLanguage = new LanguageListForm($language);
+
+
+ //Doctrine Query used to show a pager with the Ads.
+ $query=AdDescriptionTable::getInstance()->getAdsByCompanyandLanguageIdQuery($companyId, $language->getId());
+
+
+ $this->pager = new sfDoctrinePager('AdDescription', sfConfig::get('app_max_ads_on_pager'));
+ $this->pager->setQuery($query);
+ $this->pager->setPage($request->getParameter('page', 1));
+ $this->pager->init();
+ }
+
+ public function executeShow(sfWebRequest $request)
+ {
+ $this->ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id')));
+ $this->forward404Unless($this->ad);
+ }
+
+ public function executeNew(sfWebRequest $request)
+ {
+ $this->form = new AdForm();
+ }
+
+ public function executeCreate(sfWebRequest $request)
+ {
+ $this->forward404Unless($request->isMethod(sfRequest::POST));
+
+ $this->form = new AdForm();
+
+ $this->processForm($request, $this->form);
+
+ $this->setTemplate('new');
+ }
+
+ public function executeEdit(sfWebRequest $request)
+ {
+ $this->forward404Unless($ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id'))), sprintf('Object ad does not exist (%s).', $request->getParameter('id')));
+ $this->form = new AdForm($ad);
+ }
+
+ public function executeUpdate(sfWebRequest $request)
+ {
+ $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
+ $this->forward404Unless($ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id'))), sprintf('Object ad does not exist (%s).', $request->getParameter('id')));
+ $this->form = new AdForm($ad);
+
+ $this->processForm($request, $this->form);
+
+ $this->setTemplate('edit');
+ }
+
+ public function executeDelete(sfWebRequest $request)
+ {
+ $request->checkCSRFProtection();
+
+ $this->forward404Unless($ad = Doctrine_Core::getTable('Ad')->find(array($request->getParameter('id'))), sprintf('Object ad does not exist (%s).', $request->getParameter('id')));
+ $ad->delete();
+
+ $this->redirect('ad/index');
+ }
+
+ protected function processForm(sfWebRequest $request, sfForm $form)
+ {
+ $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
+ if ($form->isValid())
+ {
+ $ad = $form->save();
+
+ $this->redirect('ad/edit?id='.$ad->getId());
+ }
+ }
+
+ public function executeIndexLanguage(sfWebRequest $request)
+ {
+ $this->forward404Unless($request->isMethod(sfRequest::POST));
+
+ $formLanguage = new LanguageListForm();
+
+ //Retrieve post parameters
+ $postArray = $request->getPostParameter($formLanguage->getName());
+
+ //Retrieve Doctrine Record related to the chosen language
+ $language = LanguageTable::getInstance()->findOneById($postArray['id']);
+
+ $this->forward404Unless($language != null, sprintf('Language does not exist'));
+
+ $formLanguage = new LanguageListForm($language);
+
+ $formLanguage->bind($request->getParameter($formLanguage->getName()), $request->getFiles($formLanguage->getName()));
+ if ($formLanguage->isValid())
+ {
+ $this->redirect('ad/index?language='.$language->getCode());
+ }
+
+ //By default current user's language
+ $this->redirect('ad/index?language='.$this->getUser()->getGuardUser()->getLanguage()->getCode());
+ }
+}
--- /dev/null
+<?php use_stylesheets_for_form($form) ?>
+<?php use_javascripts_for_form($form) ?>
+
+<form action="<?php echo url_for('ad/'.($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('ad/index') ?>">Back to list</a>
+ <?php if (!$form->getObject()->isNew()): ?>
+ <?php echo link_to('Delete', 'ad/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['company_categ_id']->renderLabel() ?></th>
+ <td>
+ <?php echo $form['company_categ_id']->renderError() ?>
+ <?php echo $form['company_categ_id'] ?>
+ </td>
+ </tr>
+ <tr>
+ <th><?php echo $form['ad_mobile_image_link']->renderLabel() ?></th>
+ <td>
+ <?php echo $form['ad_mobile_image_link']->renderError() ?>
+ <?php echo $form['ad_mobile_image_link'] ?>
+ </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>
+ </tbody>
+ </table>
+</form>
--- /dev/null
+<table id="rounded-corner">
+ <thead>
+ <tr>
+ <th scope="col" class="rounded-company"><?php echo __('Phone Image Link') ?></th>
+ <th scope="col" class="rounded"><?php echo __('Company Category') ?></th>
+ <th scope="col" class="rounded"><?php echo __('Ad Name') ?></th>
+ <th scope="col" class="rounded"><?php echo __('Edit') ?></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 __('Ads List') ?></em></td>
+ <td class="rounded-foot-right"> </td>
+ </tr>
+ </tfoot>
+ <tbody>
+ <?php foreach ($ads as $ad): ?>
+ <tr>
+ <td><?php echo $ad->getAd()->getAdMobileImageLink() ?></td>
+ <td><?php echo $ad->getAd()->getCompanyCategId() ?></td>
+ <td><?php echo $ad->getAdName() ?></td>
+ <td><a href="<?php echo url_for('ad/edit?id='.$ad->getId()) ?>"><img src="/images/pencil_add.png" alt="" title="" border="0" /></a></td>
+ <td><?php echo link_to('<img src="/images/inadminpanel/images/trash.png" alt="" title="" border="0" />', 'ad/delete?id='.$ad->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?></td>
+
+ </tr>
+ <?php endforeach; ?>
+ </tbody>
+</table>
--- /dev/null
+<h1>Edit Ad</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
--- /dev/null
+<h2><?php echo __('Ads List') ?></h2>
+
+<form action="<?php echo url_for('ad/indexLanguage') ?>" method="post" <?php $formLanguage->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
+ <table>
+ <tbody>
+ <tr>
+ <td><?php echo $formLanguage->renderHiddenFields(false) ?></td>
+ <td><?php echo $formLanguage ?></td>
+ <td><input type="submit" value=<?php echo __('Update') ?> /></td>
+ </tr>
+ </tbody>
+ </table>
+</form>
+
+<?php include_partial('ad/list', array('ads' => $pager->getResults())) ?>
+
+<?php if ($pager->haveToPaginate()): ?>
+ <div class="pagination">
+ <a href="<?php echo url_for('ads_index') ?>?page=1"><?php echo __('first page') ?></a>
+
+ <a href="<?php echo url_for('ads_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('ads_index') ?>?page=<?php echo $page ?>"><?php echo $page ?></a>
+ <?php endif; ?>
+ <?php endforeach; ?>
+
+ <a href="<?php echo url_for('ads_index') ?>?page=<?php echo $pager->getNextPage() ?>"><?php echo __('next >>') ?></a>
+
+ <a href="<?php echo url_for('ads_index') ?>?page=<?php echo $pager->getLastPage() ?>"><?php echo __('last page') ?></a>
+ </div>
+<?php endif; ?>
+
+ <a href="<?php echo url_for('ad/new') ?>" class="bt_green"><span class="bt_green_lft"></span><strong><?php echo __('Ad Office') ?></strong><span class="bt_green_r"></span></a>
+
--- /dev/null
+<h1>New Ad</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
--- /dev/null
+<table>
+ <tbody>
+ <tr>
+ <th>Id:</th>
+ <td><?php echo $ad->getId() ?></td>
+ </tr>
+ <tr>
+ <th>Company:</th>
+ <td><?php echo $ad->getCompanyId() ?></td>
+ </tr>
+ <tr>
+ <th>Company categ:</th>
+ <td><?php echo $ad->getCompanyCategId() ?></td>
+ </tr>
+ <tr>
+ <th>Ad mobile image link:</th>
+ <td><?php echo $ad->getAdMobileImageLink() ?></td>
+ </tr>
+ <tr>
+ <th>Created at:</th>
+ <td><?php echo $ad->getCreatedAt() ?></td>
+ </tr>
+ <tr>
+ <th>Updated at:</th>
+ <td><?php echo $ad->getUpdatedAt() ?></td>
+ </tr>
+ </tbody>
+</table>
+
+<hr />
+
+<a href="<?php echo url_for('ad/edit?id='.$ad->getId()) ?>">Edit</a>
+
+<a href="<?php echo url_for('ad/index') ?>">List</a>
{
return Doctrine_Core::getTable('AdDescription');
}
-}
\ No newline at end of file
+
+
+ /**
+ * Returns ads with ad descriptions by company and language id.
+ *
+ * @return related ad to a company and language id as Doctrine Query
+ */
+ public function getAdsByCompanyandLanguageIdQuery($companyId, $languageId)
+ {
+ return $this->createQuery('addescription')->where('ad.company_id = ?', $companyId)
+ ->andWhere('addescription.language_id = ?', $languageId)
+ ->innerjoin('addescription.Ad ad')
+ ->orderBy('ad.id');
+ }
+
+}
--- /dev/null
+<?php
+
+include(dirname(__FILE__).'/../../bootstrap/functional.php');
+
+$browser = new sfTestFunctional(new sfBrowser());
+
+$browser->
+ get('/ad/index')->
+
+ with('request')->begin()->
+ isParameter('module', 'ad')->
+ isParameter('action', 'index')->
+ end()->
+
+ with('response')->begin()->
+ isStatusCode(200)->
+ checkElement('body', '!/This is a temporary page/')->
+ end()
+;