* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * */ require_once "Libs/autoload.php"; $config = new Config(); $webPage = new PJSWebPage($config->getTitle() . ' - Edit Contact'); $act = Tools::Param('act'); if ("Edit Contact" === $act) { $contactModel = new ContactModel(); $contactModel->populateFromForm(); if (!$contactModel->validateForUpdate()) { $view = new ContactFormView('Edit Contact', $contactModel); $body = "<h2>Invalid data</h2>\n" . $view->getForm(); } else { $contactController = new ContactController(); $newId = $contactController->update($contactModel); if ($newId > 0) { $body = "Edited contact # " . $newId . "<br />\n"; } } } else { $contactController = new ContactController(); $contactModel = $contactController->get(Tools::param('id')); $view = new ContactFormView('Edit Contact', $contactModel); $body = $view->getForm(); }
/** * @param ContactModel $model * @see ControllerBase::update() */ public function update($model) { if ($model->validateForUpdate()) { try { $query = <<<SQL UPDATE contact SET contactCompanyId = ? , contactName = ? , contactEmail = ? , contactPhone = ? , contactAlternatePhone = ? WHERE id = ? SQL; $id = $model->getId(); $contactCompanyId = $model->getContactCompanyId(); $contactName = $model->getContactName(); $contactEmail = $model->getContactEmail(); $contactPhone = $model->getContactPhone(); $contactAlternatePhone = $model->getContactAlternatePhone(); $stmt = $this->_dbh->prepare($query); if (!$stmt) { throw new ControllerException('Prepared statement failed for ' . $query); } if (!$stmt->bind_param('issssi', $contactCompanyId, $contactName, $contactEmail, $contactPhone, $contactAlternatePhone, $id)) { throw new ControllerException('Binding parameters for prepared statement failed.'); } if (!$stmt->execute()) { throw new ControllerException('Failed to execute UPDATE statement. (' . $this->_dbh->error . ')'); } /** * @SuppressWarnings checkAliases */ if (!$stmt->close()) { throw new ControllerException('Something broke while trying to close the prepared statement.'); } return $id; } catch (Exception $e) { throw new ControllerException($e->getMessage()); } } else { throw new ControllerException("Invalid data."); } }