/** Edit an organisation's details */ public function editAction() { $form = new OrganisationForm(); $form->submit->setLabel('Update organisation'); $this->view->form = $form; if ($this->_request->isPost()) { $formData = $this->_request->getPost(); if ($form->isValid($formData)) { $address = $form->getValue('address') . ',' . $form->getValue('town_city') . ',' . $form->getValue('county') . ',' . $form->getValue('postcode') . ',' . $form->getValue('country'); $coords = $this->_geocoder->getCoordinates($address); if ($coords) { $lat = $coords['lat']; $long = $coords['lon']; } else { $lat = NULL; $lon = NULL; } $pm = new Placemaker(); $place = $pm->get($address); $woeid = $place->woeid; $updateData = array(); $updateData['name'] = $form->getValue('name'); $updateData['website'] = $form->getValue('website'); $updateData['address1'] = $form->getValue('address1'); $updateData['address2'] = $form->getValue('address2'); $updateData['address3'] = $form->getValue('address3'); $updateData['address'] = $form->getValue('address'); $updateData['postcode'] = $form->getValue('postcode'); $updateData['town_city'] = $form->getValue('town_city'); $updateData['county'] = $form->getValue('county'); $updateData['country'] = $form->getValue('country'); $updateData['contactpersonID'] = $form->getValue('contactpersonID'); $updateData['lat'] = $lat; $updateData['lon'] = $lon; $updateData['woeid'] = $woeid; $updateData['updated'] = $this->getTimeForForms(); $updateData['updatedBy'] = $this->getIdentityForForms(); foreach ($updateData as $key => $value) { if (is_null($value) || $value == "") { $updateData[$key] = NULL; } } $auditData = $updateData; $audit = $this->_organisations->fetchRow('id=' . $this->_getParam('id')); $oldarray = $audit->toArray(); $where = array(); $where = $this->_organisations->getAdapter()->quoteInto('id = ?', $this->_getParam('id')); $update = $this->_organisations->update($updateData, $where); if (!empty($auditData)) { // look for new fields with empty/null values foreach ($auditData as $item => $value) { if (empty($value)) { if (!array_key_exists($item, $oldarray)) { // value does not exist in $oldarray, so remove from $newarray unset($updateData[$item]); } // if } else { // remove slashes (escape characters) from $newarray $auditData[$item] = stripslashes($auditData[$item]); } // if } // foreach // remove entry from $oldarray which does not exist in $newarray foreach ($oldarray as $item => $value) { if (!array_key_exists($item, $auditData)) { unset($oldarray[$item]); } // if } // foreach } // $fieldarray = array(); $ix = 0; $editID = md5($this->getTimeForForms()); foreach ($oldarray as $field_id => $old_value) { $ix++; $fieldarray[$ix]['orgID'] = $this->_getParam('id'); $fieldarray[$ix]['editID'] = $editID; $fieldarray[$ix]['created'] = $this->getTimeForForms(); $fieldarray[$ix]['createdBy'] = $this->getIdentityForForms(); $fieldarray[$ix]['fieldName'] = $field_id; $fieldarray[$ix]['beforeValue'] = $old_value; if (isset($auditData[$field_id])) { $fieldarray[$ix]['afterValue'] = $auditData[$field_id]; // remove matched entry from $newarray unset($auditData[$field_id]); } else { $fieldarray[$ix]['afterValue'] = ''; } // if } // foreach // process any unmatched details remaining in $newarray foreach ($auditData as $field_id => $new_value) { $ix++; $fieldarray[$ix]['orgID'] = $this->_getParam('id'); $fieldarray[$ix]['editID'] = $editID; $fieldarray[$ix]['created'] = $this->getTimeForForms(); $fieldarray[$ix]['createdBy'] = $this->getIdentityForForms(); $fieldarray[$ix]['fieldName'] = $field_id; $fieldarray[$ix]['afterValue'] = $new_value; } function filteraudit($fieldarray) { if ($fieldarray['afterValue'] != $fieldarray['beforeValue']) { return true; } } $fieldarray = array_filter($fieldarray, 'filteraudit'); foreach ($fieldarray as $f) { foreach ($f as $key => $value) { if (is_null($value) || $value == "") { $f[$key] = NULL; } } $audit = new OrganisationsAudit(); $auditBaby = $audit->insert($f); } $this->_flashMessenger->addMessage('Organisation information updated!'); $this->_redirect(self::REDIRECT . 'organisation/id/' . $this->_getParam('id')); } else { $form->populate($formData); } } else { $id = (int) $this->_request->getParam('id', 0); if ($id > 0) { $organisations = new Organisations(); $organisation = $organisations->fetchRow('id=' . $id); $form->populate($organisation->toArray()); } } }