homepage:
url: /
class: sfDoctrineRoute
- param: { module: office, action: index }
- options: { model: Office, type: object }
+ param: { module: company, action: index }
+ options: { model: Company, type: object }
+
+company_index:
+ url: /company/index
+ class: sfDoctrineRoute
+ param: { module: company, action: index }
+ options: { model: Company, type: object }
offices_index:
url: /office/index
--- /dev/null
+<?php
+
+/**
+ * company actions.
+ *
+ * @package mobiads
+ * @subpackage company
+ * @author Gustavo Martin Morcuende
+ * @version
+ */
+class companyActions extends sfActions
+{
+ public function executeIndex(sfWebRequest $request)
+ {
+ $this->companys = Doctrine_Core::getTable('Company')
+ ->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();
+
+ $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();
+ }
+
+ //Retrieve Doctrine Record (just one company for user)
+ $this->company = CompanyDescriptionTable::getInstance()->findOneByCompanyIdAndLanguageId($companyId, $language->getId());
+ }
+
+ public function executeShow(sfWebRequest $request)
+ {
+ $this->company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id')));
+ $this->forward404Unless($this->company);
+ }
+
+ public function executeNew(sfWebRequest $request)
+ {
+ $this->form = new CompanyForm();
+ }
+
+ public function executeCreate(sfWebRequest $request)
+ {
+ $this->forward404Unless($request->isMethod(sfRequest::POST));
+
+ $this->form = new CompanyForm();
+
+ $this->processForm($request, $this->form);
+
+ $this->setTemplate('new');
+ }
+
+ public function executeEdit(sfWebRequest $request)
+ {
+ $this->forward404Unless($company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id'))), sprintf('Object company 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)
+ $id = $request->getParameter('id');
+
+ $companyId = CompanyTable::getInstance()->findOneById($id)->getId();
+
+ $this->forward404Unless($companyId == $companyUserId, sprintf('Company does not exist (%s).', $request->getParameter('id')));
+
+ $this->form = new CompanyForm($company);
+ }
+
+ public function executeUpdate(sfWebRequest $request)
+ {
+ $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
+ $this->forward404Unless($company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id'))), sprintf('Object company 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)
+ $id = $request->getParameter('id');
+
+ $companyId = CompanyTable::getInstance()->findOneById($id)->getId();
+
+ $this->forward404Unless($companyId == $companyUserId, sprintf('Company does not exist (%s).', $request->getParameter('id')));
+
+ $this->form = new CompanyForm($company);
+
+ $this->processForm($request, $this->form);
+
+ $this->setTemplate('edit');
+ }
+
+ public function executeDelete(sfWebRequest $request)
+ {
+ $request->checkCSRFProtection();
+
+ $this->forward404Unless($company = Doctrine_Core::getTable('Company')->find(array($request->getParameter('id'))), sprintf('Object company does not exist (%s).', $request->getParameter('id')));
+ $company->delete();
+
+ $this->redirect('company/index');
+ }
+
+ protected function processForm(sfWebRequest $request, sfForm $form)
+ {
+ $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
+ if ($form->isValid())
+ {
+ $company = $form->save();
+
+ $this->redirect('company/edit?id='.$company->getId());
+ }
+ }
+}
--- /dev/null
+<?php use_stylesheets_for_form($form) ?>
+<?php use_javascripts_for_form($form) ?>
+
+<form action="<?php echo url_for('company/'.($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('company/index') ?>"><?php echo __('Back to list') ?></a>
+ <input type="submit" value=<?php echo __('Update') ?> />
+ </td>
+ </tr>
+ </tfoot>
+ <tbody>
+ <?php echo $form->renderGlobalErrors(false) ?>
+ <?php echo $form ?>
+ </tbody>
+ </table>
+</form>
--- /dev/null
+<h2><?php echo __('Edit Your Data Company') ?></h2>
+
+
+<?php include_partial('form', array('form' => $form)) ?>
--- /dev/null
+<h2><?php echo __('Your Company') ?></h2>
+
+<table id="rounded-corner">
+ <thead>
+ <tr>
+ <th scope="col" class="rounded-company"><?php echo __('Name') ?></th>
+ <th scope="col" class="rounded-q4"><?php echo __('CIF') ?></th>
+ </tr>
+ </thead>
+ <tfoot>
+ <tr>
+ <td colspan="1" class="rounded-foot-left"><em><?php echo __('Your data company') ?></em></td>
+ <td class="rounded-foot-right"> </td>
+ </tr>
+ </tfoot>
+ <tbody>
+ <tr>
+ <td><?php echo $company->getCompanyName() ?></td>
+ <td><?php echo $company->getCompany()->getCompanyCif() ?></td>
+ </tr>
+ </tbody>
+</table>
+
+<a href="<?php echo url_for('company/edit?id='.$company->getCompany()->getId()) ?>" class="bt_green"><span class="bt_green_lft"></span><strong><?php echo __('Edit Your Company') ?></strong><span class="bt_green_r"></span></a>
--- /dev/null
+<h1>New Company</h1>
+
+<?php include_partial('form', array('form' => $form)) ?>
--- /dev/null
+<table>
+ <tbody>
+ <tr>
+ <th>Id:</th>
+ <td><?php echo $company->getId() ?></td>
+ </tr>
+ <tr>
+ <th>User:</th>
+ <td><?php echo $company->getUserId() ?></td>
+ </tr>
+ <tr>
+ <th>Company cif:</th>
+ <td><?php echo $company->getCompanyCif() ?></td>
+ </tr>
+ </tbody>
+</table>
+
+<hr />
+
+<a href="<?php echo url_for('company/edit?id='.$company->getId()) ?>">Edit</a>
+
+<a href="<?php echo url_for('company/index') ?>">List</a>
*
* @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 CompanyDescriptionForm extends BaseCompanyDescriptionForm
{
public function configure()
{
+ unset($this['company_id']);
+
+ $this->widgetSchema->setLabels(array('language_id' => 'Language: '));
+ $this->widgetSchema->setLabels(array('company_name' => 'Company Name: '));
+
+ if ($this->object->exists())
+ {
+ $this->widgetSchema['delete'] = new sfWidgetFormInputCheckbox();
+ $this->validatorSchema['delete'] = new sfValidatorPass();
+ }
+
+ //i18n (Internationalization)
+ $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('company_description_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 CompanyForm extends BaseCompanyForm
{
+ /*Stores Doctrine Records to be removed from the database.*/
+ protected $scheduledForDeletion = array();
+
public function configure()
{
+ $this->useFields(array('company_cif'));
+
+
+ $this->widgetSchema->setLabels(array('company_cif' => 'CIF: '));
+
+
+ //Company create new description form
+ $companyDescription = new CompanyDescription();
+ $companyDescription->Company = $this->getObject();
+ $newCompanyDescriptionForm = new CompanyDescriptionForm($companyDescription);
+
+ $this->embedForm('new', $newCompanyDescriptionForm);
+
+ $this->embedRelation('CompanyDescription');
+
+
+ //i18n (Internationalization)
+ $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('company_form');
+ }
+
+ protected function doBind(array $values)
+ {
+ if ('' === trim($values['new']['company_name']))
+ {
+ unset($values['new'], $this['new']);
+ }
+
+ if (isset($values['CompanyDescription']))
+ {
+ foreach ($values['CompanyDescription'] as $i => $companyDescriptionValues)
+ {
+ if (isset($companyDescriptionValues['delete']) && $companyDescriptionValues['id'])
+ {
+ $this->scheduledForDeletion[$i] = $companyDescriptionValues['id'];
+ }
+ }
+ }
+
+
+ parent::doBind($values);
+ }
+
+ /**
+ * Updates object with provided values, dealing with evantual relation deletion
+ *
+ * @see sfFormDoctrine::doUpdateObject()
+ */
+ protected function doUpdateObject($values)
+ {
+ if (count($this->scheduledForDeletion))
+ {
+ foreach ($this->scheduledForDeletion as $index => $id)
+ {
+ unset($values['CompanyDescription'][$index]);
+ unset($this->object['CompanyDescription'][$index]);
+ Doctrine::getTable('CompanyDescription')->findOneById($id)->delete();
+ }
+ }
+
+ $this->getObject()->fromArray($values);
+ }
+
+ /**
+ * Saves embedded form objects.
+ *
+ * @param mixed $con An optional connection object
+ * @param array $forms An array of forms
+ */
+ public function saveEmbeddedForms($con = null, $forms = null)
+ {
+ if (null === $con)
+ {
+ $con = $this->getConnection();
+ }
+
+ if (null === $forms)
+ {
+ $forms = $this->embeddedForms;
+ }
+
+ foreach ($forms as $form)
+ {
+ if ($form instanceof sfFormObject)
+ {
+ if (!in_array($form->getObject()->getId(), $this->scheduledForDeletion))
+ {
+ $form->saveEmbeddedForms($con);
+ $form->getObject()->save($con);
+ }
+ }
+ else
+ {
+ $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
+ }
+ }
}
}
--- /dev/null
+<?php
+
+include(dirname(__FILE__).'/../../bootstrap/functional.php');
+
+$browser = new sfTestFunctional(new sfBrowser());
+
+$browser->
+ get('/company/index')->
+
+ with('request')->begin()->
+ isParameter('module', 'company')->
+ isParameter('action', 'index')->
+ end()->
+
+ with('response')->begin()->
+ isStatusCode(200)->
+ checkElement('body', '!/This is a temporary page/')->
+ end()
+;