* 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() . "Contacts - Add Contact"); $body = ''; $act = Tools::Param('act'); if ("Add Contact" === $act) { $model = new ContactModel(); $model->populateFromForm(); if (!$model->validateForAdd()) { $view = new ContactFormView('Add Contact', $model); $body = "<h2>Invalid data</h2>\n" . $view->getForm(); } else { $contactController = new ContactController(); $newId = $contactController->add($model); if ($newId > 0) { $body = "Added contact # " . $newId . "<br />\n"; } } } else { $body = ""; $contactModel = new ContactModel(); $companyId = '' === Tools::param('contactCompanyId') ? 0 : Tools::param('contactCompanyId'); $contactModel->setContactCompanyId($companyId); $contactModel->setContactName(Tools::param('contactName'));
/** * @param ContactModel $model * @see ControllerBase::add() */ public function add($model) { if ($model->validateForAdd()) { try { $query = <<<SQL INSERT contact ( id , contactCompanyId , contactName , contactEmail , contactPhone , contactAlternatePhone , created , updated ) VALUES ( NULL, ?, ?, ?, ?, ?, NOW(), NOW() ) 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('issss', $contactCompanyId, $contactName, $contactEmail, $contactPhone, $contactAlternatePhone)) { throw new ControllerException('Binding parameters for prepared statement failed.'); } if (!$stmt->execute()) { throw new ControllerException('Failed to execute INSERT statement. (' . $this->_dbh->error . ')'); } $newId = $stmt->insert_id; /** * @SuppressWarnings checkAliases */ if (!$stmt->close()) { throw new ControllerException('Something broke while trying to close the prepared statement.'); } return $newId; } catch (Exception $e) { throw new ControllerException($e->getMessage()); } } else { throw new ControllerException("Invalid data."); } }