From 26fcb859e7f56ddeaf7e9ce57c514c76a8da18a6 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 21 May 2012 02:09:39 +0200 Subject: [PATCH] Web pages for company data management. --- apps/companyfront/config/routing.yml | 10 +- .../modules/company/actions/actions.class.php | 134 +++++++++++++++++++++ .../modules/company/templates/_form.php | 23 ++++ .../modules/company/templates/editSuccess.php | 4 + .../modules/company/templates/indexSuccess.php | 24 ++++ .../modules/company/templates/newSuccess.php | 3 + .../modules/company/templates/showSuccess.php | 22 ++++ lib/form/doctrine/CompanyDescriptionForm.class.php | 17 ++- lib/form/doctrine/CompanyForm.class.php | 102 +++++++++++++++- .../functional/companyfront/companyActionsTest.php | 19 +++ 10 files changed, 352 insertions(+), 6 deletions(-) create mode 100644 apps/companyfront/modules/company/actions/actions.class.php create mode 100644 apps/companyfront/modules/company/templates/_form.php create mode 100644 apps/companyfront/modules/company/templates/editSuccess.php create mode 100644 apps/companyfront/modules/company/templates/indexSuccess.php create mode 100644 apps/companyfront/modules/company/templates/newSuccess.php create mode 100644 apps/companyfront/modules/company/templates/showSuccess.php create mode 100644 test/functional/companyfront/companyActionsTest.php diff --git a/apps/companyfront/config/routing.yml b/apps/companyfront/config/routing.yml index 927ab60..41a4507 100644 --- a/apps/companyfront/config/routing.yml +++ b/apps/companyfront/config/routing.yml @@ -5,8 +5,14 @@ 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 diff --git a/apps/companyfront/modules/company/actions/actions.class.php b/apps/companyfront/modules/company/actions/actions.class.php new file mode 100644 index 0000000..b483044 --- /dev/null +++ b/apps/companyfront/modules/company/actions/actions.class.php @@ -0,0 +1,134 @@ +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()); + } + } +} diff --git a/apps/companyfront/modules/company/templates/_form.php b/apps/companyfront/modules/company/templates/_form.php new file mode 100644 index 0000000..9b6d065 --- /dev/null +++ b/apps/companyfront/modules/company/templates/_form.php @@ -0,0 +1,23 @@ + + + +
isMultipart() and print 'enctype="multipart/form-data" ' ?>> +getObject()->isNew()): ?> + + + + + + + + + + renderGlobalErrors(false) ?> + + +
+ renderHiddenFields(false) ?> +   + /> +
+
diff --git a/apps/companyfront/modules/company/templates/editSuccess.php b/apps/companyfront/modules/company/templates/editSuccess.php new file mode 100644 index 0000000..a55e220 --- /dev/null +++ b/apps/companyfront/modules/company/templates/editSuccess.php @@ -0,0 +1,4 @@ +

+ + + $form)) ?> diff --git a/apps/companyfront/modules/company/templates/indexSuccess.php b/apps/companyfront/modules/company/templates/indexSuccess.php new file mode 100644 index 0000000..6db6761 --- /dev/null +++ b/apps/companyfront/modules/company/templates/indexSuccess.php @@ -0,0 +1,24 @@ +

+ + + + + + + + + + + + + + + + + + + + +
 
getCompanyName() ?>getCompany()->getCompanyCif() ?>
+ + diff --git a/apps/companyfront/modules/company/templates/newSuccess.php b/apps/companyfront/modules/company/templates/newSuccess.php new file mode 100644 index 0000000..a49c584 --- /dev/null +++ b/apps/companyfront/modules/company/templates/newSuccess.php @@ -0,0 +1,3 @@ +

New Company

+ + $form)) ?> diff --git a/apps/companyfront/modules/company/templates/showSuccess.php b/apps/companyfront/modules/company/templates/showSuccess.php new file mode 100644 index 0000000..365f64f --- /dev/null +++ b/apps/companyfront/modules/company/templates/showSuccess.php @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + +
Id:getId() ?>
User:getUserId() ?>
Company cif:getCompanyCif() ?>
+ +
+ +Edit +  +List diff --git a/lib/form/doctrine/CompanyDescriptionForm.class.php b/lib/form/doctrine/CompanyDescriptionForm.class.php index e810b3d..5de1ec1 100644 --- a/lib/form/doctrine/CompanyDescriptionForm.class.php +++ b/lib/form/doctrine/CompanyDescriptionForm.class.php @@ -5,12 +5,25 @@ * * @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'); } } diff --git a/lib/form/doctrine/CompanyForm.class.php b/lib/form/doctrine/CompanyForm.class.php index 08f6a47..89a32c3 100644 --- a/lib/form/doctrine/CompanyForm.class.php +++ b/lib/form/doctrine/CompanyForm.class.php @@ -5,12 +5,110 @@ * * @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()); + } + } } } diff --git a/test/functional/companyfront/companyActionsTest.php b/test/functional/companyfront/companyActionsTest.php new file mode 100644 index 0000000..1e4caa9 --- /dev/null +++ b/test/functional/companyfront/companyActionsTest.php @@ -0,0 +1,19 @@ + + 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() +; -- 2.1.4