First steps with Ads. Index web page.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 15 May 2012 17:14:19 +0000 (19:14 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 15 May 2012 17:14:19 +0000 (19:14 +0200)
Index web page with internationalization.
We can change the shown language without JavaScript.

apps/companyfront/config/app.yml
apps/companyfront/config/routing.yml
apps/companyfront/modules/ad/actions/actions.class.php [new file with mode: 0644]
apps/companyfront/modules/ad/templates/_form.php [new file with mode: 0644]
apps/companyfront/modules/ad/templates/_list.php [new file with mode: 0644]
apps/companyfront/modules/ad/templates/editSuccess.php [new file with mode: 0644]
apps/companyfront/modules/ad/templates/indexSuccess.php [new file with mode: 0644]
apps/companyfront/modules/ad/templates/newSuccess.php [new file with mode: 0644]
apps/companyfront/modules/ad/templates/showSuccess.php [new file with mode: 0644]
lib/model/doctrine/AdDescriptionTable.class.php
test/functional/companyfront/adActionsTest.php [new file with mode: 0644]

index 1bd9aa3..7cad5cf 100644 (file)
@@ -4,6 +4,7 @@
 # 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
index cd57e68..a5d1a2c 100644 (file)
@@ -14,6 +14,12 @@ offices_index:
   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:
diff --git a/apps/companyfront/modules/ad/actions/actions.class.php b/apps/companyfront/modules/ad/actions/actions.class.php
new file mode 100644 (file)
index 0000000..869ea1b
--- /dev/null
@@ -0,0 +1,139 @@
+<?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());
+  }
+}
diff --git a/apps/companyfront/modules/ad/templates/_form.php b/apps/companyfront/modules/ad/templates/_form.php
new file mode 100644 (file)
index 0000000..055a634
--- /dev/null
@@ -0,0 +1,60 @@
+<?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) ?>
+          &nbsp;<a href="<?php echo url_for('ad/index') ?>">Back to list</a>
+          <?php if (!$form->getObject()->isNew()): ?>
+            &nbsp;<?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>
diff --git a/apps/companyfront/modules/ad/templates/_list.php b/apps/companyfront/modules/ad/templates/_list.php
new file mode 100644 (file)
index 0000000..1ddfa3d
--- /dev/null
@@ -0,0 +1,29 @@
+<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">&nbsp;</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>
diff --git a/apps/companyfront/modules/ad/templates/editSuccess.php b/apps/companyfront/modules/ad/templates/editSuccess.php
new file mode 100644 (file)
index 0000000..b6d77be
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>Edit Ad</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
diff --git a/apps/companyfront/modules/ad/templates/indexSuccess.php b/apps/companyfront/modules/ad/templates/indexSuccess.php
new file mode 100644 (file)
index 0000000..04d0d0c
--- /dev/null
@@ -0,0 +1,38 @@
+<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>
+
diff --git a/apps/companyfront/modules/ad/templates/newSuccess.php b/apps/companyfront/modules/ad/templates/newSuccess.php
new file mode 100644 (file)
index 0000000..3d12b7f
--- /dev/null
@@ -0,0 +1,3 @@
+<h1>New Ad</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
diff --git a/apps/companyfront/modules/ad/templates/showSuccess.php b/apps/companyfront/modules/ad/templates/showSuccess.php
new file mode 100644 (file)
index 0000000..e7007e9
--- /dev/null
@@ -0,0 +1,34 @@
+<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>
+&nbsp;
+<a href="<?php echo url_for('ad/index') ?>">List</a>
index fe6b171..4a0355a 100644 (file)
@@ -16,4 +16,19 @@ class AdDescriptionTable extends Doctrine_Table
     {
         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');
+    }
+
+}
diff --git a/test/functional/companyfront/adActionsTest.php b/test/functional/companyfront/adActionsTest.php
new file mode 100644 (file)
index 0000000..a908bd7
--- /dev/null
@@ -0,0 +1,19 @@
+<?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()
+;