/** * Edit the profile of the person. * * @return ViewModel */ public function editAction() { $contactService = $this->getContactService()->setContactId($this->zfcUserAuthentication()->getIdentity()->getId()); //Find the amount of possible organisations $organisations = $this->getOrganisationService()->findOrganisationForProfileEditByContact($contactService->getContact()); $branches = []; if ($contactService->hasOrganisation()) { $branches = $this->getOrganisationService()->findBranchesByOrganisation($contactService->getContact()->getContactOrganisation()->getOrganisation()); } $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); $form = new Profile($this->getEntityManager(), $this->getContactService(), $contactService->getContact()); $form->bind($contactService->getContact()); /** * When the organisation name is typed, we force the value to zero */ if (!isset($data['contact_organisation']['organisation_id'])) { $form->getInputFilter()->get('contact_organisation')->remove('organisation_id'); } $form->setData($data); if ($this->getRequest()->isPost() && $form->isValid()) { $contact = $form->getData(); $fileData = $this->params()->fromFiles(); if (!empty($fileData['file']['name'])) { $photo = $contactService->getContact()->getPhoto()->first(); if (!$photo) { //Create a photo element $photo = new Photo(); } $photo->setPhoto(file_get_contents($fileData['file']['tmp_name'])); $photo->setThumb(file_get_contents($fileData['file']['tmp_name'])); $imageSizeValidator = new ImageSize(); $imageSizeValidator->isValid($fileData['file']); $photo->setWidth($imageSizeValidator->width); $photo->setHeight($imageSizeValidator->height); $photo->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($fileData['file']['type'])); $collection = new ArrayCollection(); $collection->add($photo); $contact->addPhoto($collection); } /* * Remove any unwanted photo's */ foreach ($contact->getPhoto() as $photo) { if (is_null($photo->getWidth())) { $collection = new ArrayCollection(); $collection->add($photo); $contact->removePhoto($collection); } } $contact = $this->getContactService()->updateEntity($contact); /** * The contact_organisation is different and not a drop-down. * we will extract the organisation name from the contact_organisation text-field */ $this->getContactService()->updateContactOrganisation($contact, $data['contact_organisation']); $this->flashMessenger()->setNamespace('success')->addMessage(_("txt-profile-has-successfully-been-updated")); return $this->redirect()->toRoute('community/contact/profile/view'); } return new ViewModel(['form' => $form, 'branches' => $branches, 'contactService' => $contactService, 'hasOrganisations' => sizeof($organisations) > 1, 'fullVersion' => true]); }
/** * Ensures that the validator follows expected behavior * * @return void */ public function testBasic() { $valuesExpected = array(array(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000), true), array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 200), true), array(array('minwidth' => 150, 'minheight' => 150, 'maxwidth' => 200, 'maxheight' => 200), false), array(array('minwidth' => 80, 'minheight' => 0, 'maxwidth' => 80, 'maxheight' => 200), true), array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 60, 'maxheight' => 200), false), array(array('minwidth' => 90, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 200), false), array(array('minwidth' => 0, 'minheight' => 0, 'maxwidth' => 200, 'maxheight' => 80), false), array(array('minwidth' => 0, 'minheight' => 110, 'maxwidth' => 200, 'maxheight' => 140), false)); foreach ($valuesExpected as $element) { $validator = new File\ImageSize($element[0]); $this->assertEquals($element[1], $validator->isValid(__DIR__ . '/_files/picture.jpg'), "Tested with " . var_export($element, 1)); } $validator = new File\ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000)); $this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/nofile.jpg')); $failures = $validator->getMessages(); $this->assertContains('is not readable', $failures['fileImageSizeNotReadable']); $file['name'] = 'TestName'; $validator = new File\ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000)); $this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/nofile.jpg', $file)); $failures = $validator->getMessages(); $this->assertContains('TestName', $failures['fileImageSizeNotReadable']); $validator = new File\ImageSize(array('minwidth' => 0, 'minheight' => 10, 'maxwidth' => 1000, 'maxheight' => 2000)); $this->assertEquals(false, $validator->isValid(__DIR__ . '/_files/badpicture.jpg')); $failures = $validator->getMessages(); $this->assertContains('could not be detected', $failures['fileImageSizeNotDetected']); }
/** * @group ZF-11258 */ public function testZF11258() { $validator = new File\ImageSize(array('minWidth' => 100, 'minHeight' => 1000, 'maxWidth' => 10000, 'maxHeight' => 100000)); $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo')); $this->assertTrue(array_key_exists('fileImageSizeNotReadable', $validator->getMessages())); $this->assertContains("'nofile.mo'", current($validator->getMessages())); }
public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage() { $validator = new File\ImageSize(); $this->assertFalse($validator->isValid('')); $this->assertArrayHasKey(File\ImageSize::NOT_READABLE, $validator->getMessages()); $filesArray = array('name' => '', 'size' => 0, 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'type' => ''); $this->assertFalse($validator->isValid($filesArray)); $this->assertArrayHasKey(File\ImageSize::NOT_READABLE, $validator->getMessages()); }
/** * Edit the profile of the person * @return ViewModel */ public function profileEditAction() { $contactService = $this->getContactService()->setContactId($this->zfcUserAuthentication()->getIdentity()->getId()); $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); $form = new Profile($this->getServiceLocator(), $contactService->getContact()); $form->bind($contactService->getContact()); $form->setData($data); if ($this->getRequest()->isPost() && $form->isValid()) { $contact = $form->getData(); $fileData = $this->params()->fromFiles(); if (!empty($fileData['file']['name'])) { $photo = $contactService->getContact()->getPhoto()->first(); if (!$photo) { //Create a photo element $photo = new Photo(); } $photo->setPhoto(file_get_contents($fileData['file']['tmp_name'])); $photo->setThumb(file_get_contents($fileData['file']['tmp_name'])); $imageSizeValidator = new ImageSize(); $imageSizeValidator->isValid($fileData['file']); $photo->setWidth($imageSizeValidator->width); $photo->setHeight($imageSizeValidator->height); $photo->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($fileData['file']['type'])); $collection = new ArrayCollection(); $collection->add($photo); $contact->addPhoto($collection); } /** * Remove any unwanted photo's */ foreach ($contact->getPhoto() as $photo) { if (is_null($photo->getWidth())) { $collection = new ArrayCollection(); $collection->add($photo); $contact->removePhoto($collection); } } $contact = $this->getContactService()->updateEntity($contact); /** * The contact_organisation is different and not a drop-down. * we will extract the organisation name from the contact_organisation text-field */ $this->getContactService()->updateContactOrganisation($contact, $data['contact_organisation']); $this->flashMessenger()->setNamespace('success')->addMessage(_("txt-profile-has-successfully-been-updated")); return $this->redirect()->toRoute('contact/profile'); } return new ViewModel(['form' => $form, 'contactService' => $contactService, 'fullVersion' => true]); }
/** * @return ViewModel */ public function editAction() { $organisationService = $this->getOrganisationService()->setOrganisationId($this->params('id')); $data = array_merge(['description' => $organisationService->getOrganisation()->getDescription()], $this->getRequest()->getPost()->toArray()); $form = $this->getFormService()->prepare($organisationService->getOrganisation(), $organisationService->getOrganisation(), $data); if ($this->getRequest()->isPost()) { if (isset($data['cancel'])) { return $this->redirect()->toRoute('zfcadmin/organisation/view', ['id' => $organisationService->getOrganisation()->getId()]); } if ($form->isValid()) { $this->flashMessenger()->setNamespace('success')->addMessage(sprintf($this->translate("txt-organisation-%s-has-successfully-been-updated"), $organisationService->getOrganisation())); /** * @var $organisation Organisation */ $organisation = $form->getData(); $fileData = $this->params()->fromFiles(); if (!empty($fileData['file']['name'])) { $logo = $organisationService->getOrganisation()->getLogo()->first(); if (!$logo) { //Create a logo element $logo = new Logo(); $logo->setOrganisation($organisationService->getOrganisation()); } $logo->setOrganisationLogo(file_get_contents($fileData['file']['tmp_name'])); $imageSizeValidator = new ImageSize(); $imageSizeValidator->isValid($fileData['file']); $logo->setContentType($this->getGeneralService()->findContentTypeByContentTypeName($fileData['file']['type'])); $logo->setLogoExtension($logo->getContentType()->getExtension()); $organisation->getLogo()->add($logo); /** * Remove the cached file */ if (file_exists($logo->getCacheFileName())) { unlink($logo->getCacheFileName()); } } $this->getOrganisationService()->updateEntity($organisation); return $this->redirect()->toRoute('zfcadmin/organisation/view', ['id' => $organisationService->getOrganisation()->getId()]); } } return new ViewModel(['organisationService' => $organisationService, 'form' => $form]); }