public function create(HumanModel $human, SiteModel $site, UserAccountModel $creator = null) { global $DB; try { $DB->beginTransaction(); $stat = $DB->prepare("SELECT max(slug) AS c FROM human_information WHERE site_id=:site_id"); $stat->execute(array('site_id' => $site->getId())); $data = $stat->fetch(); $human->setSlug($data['c'] + 1); $stat = $DB->prepare("INSERT INTO human_information (site_id, slug, title,description,created_at,approved_at, is_deleted, email, twitter, image_url, party, url) " . "VALUES (:site_id, :slug, :title, :description, :created_at,:approved_at, '0', :email, :twitter, :image_url, :party, :url) RETURNING id"); $stat->execute(array('site_id' => $site->getId(), 'slug' => $human->getSlug(), 'title' => substr($human->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'twitter' => substr($human->getTwitter(), 0, VARCHAR_COLUMN_LENGTH_USED), 'party' => substr($human->getParty(), 0, VARCHAR_COLUMN_LENGTH_USED), 'email' => substr($human->getEmail(), 0, VARCHAR_COLUMN_LENGTH_USED), 'url' => substr($human->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'image_url' => $human->getImageUrl(), 'description' => $human->getDescription(), 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase())); $data = $stat->fetch(); $human->setId($data['id']); $stat = $DB->prepare("INSERT INTO human_history (human_id, title, description, user_account_id , created_at, approved_at, is_new, is_deleted, email, twitter, image_url, party, url) VALUES " . "(:human_id, :title, :description, :user_account_id , :created_at, :approved_at, '1', '0', :email, :twitter, :image_url, :party, :url)"); $stat->execute(array('human_id' => $human->getId(), 'title' => substr($human->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'twitter' => substr($human->getTwitter(), 0, VARCHAR_COLUMN_LENGTH_USED), 'party' => substr($human->getParty(), 0, VARCHAR_COLUMN_LENGTH_USED), 'email' => substr($human->getEmail(), 0, VARCHAR_COLUMN_LENGTH_USED), 'url' => substr($human->getUrl(), 0, VARCHAR_COLUMN_LENGTH_USED), 'image_url' => $human->getImageUrl(), 'description' => $human->getDescription(), 'user_account_id' => $creator ? $creator->getId() : null, 'created_at' => \TimeSource::getFormattedForDataBase(), 'approved_at' => \TimeSource::getFormattedForDataBase())); $DB->commit(); } catch (Exception $e) { $DB->rollBack(); } }
public function update(HumanModel $human, $fields, UserAccountModel $user = null) { $alreadyInTransaction = $this->db->inTransaction(); // Make Information Data $fieldsSQL1 = array(); $fieldsParams1 = array('id' => $human->getId()); foreach ($fields as $field) { $fieldsSQL1[] = " " . $field . "=:" . $field . " "; if ($field == 'title') { $fieldsParams1['title'] = substr($human->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED); } else { if ($field == 'description') { $fieldsParams1['description'] = $human->getDescription(); } else { if ($field == 'email') { $fieldsParams1['email'] = $human->getEmail(); } else { if ($field == 'twitter') { $fieldsParams1['twitter'] = $human->getTwitter(); } else { if ($field == 'party') { $fieldsParams1['party'] = $human->getParty(); } else { if ($field == 'image_url') { $fieldsParams1['image_url'] = $human->getImageUrl(); } else { if ($field == 'url') { $fieldsParams1['url'] = $human->getUrl(); } else { if ($field == 'is_deleted') { $fieldsParams1['is_deleted'] = $human->getIsDeleted() ? 1 : 0; } else { if ($field == 'is_duplicate_of_id') { $fieldsParams1['is_duplicate_of_id'] = $human->getIsDuplicateOfId(); } } } } } } } } } } // Make History Data $fieldsSQL2 = array('human_id', 'user_account_id', 'created_at', 'approved_at'); $fieldsSQLParams2 = array(':human_id', ':user_account_id', ':created_at', ':approved_at'); $fieldsParams2 = array('human_id' => $human->getId(), 'user_account_id' => $user ? $user->getId() : null, 'created_at' => $this->timesource->getFormattedForDataBase(), 'approved_at' => $this->timesource->getFormattedForDataBase()); foreach ($this->possibleFields as $field) { if (in_array($field, $fields) || $field == 'title') { $fieldsSQL2[] = " " . $field . " "; $fieldsSQLParams2[] = " :" . $field . " "; if ($field == 'title') { $fieldsParams2['title'] = substr($human->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED); } else { if ($field == 'description') { $fieldsParams2['description'] = $human->getDescription(); } else { if ($field == 'image_url') { $fieldsParams2['image_url'] = $human->getImageUrl(); } else { if ($field == 'email') { $fieldsParams2['email'] = $human->getEmail(); } else { if ($field == 'twitter') { $fieldsParams2['twitter'] = $human->getTwitter(); } else { if ($field == 'party') { $fieldsParams2['party'] = $human->getParty(); } else { if ($field == 'url') { $fieldsParams2['url'] = $human->getUrl(); } else { if ($field == 'is_deleted') { $fieldsParams2['is_deleted'] = $human->getIsDeleted() ? 1 : 0; } else { if ($field == 'is_duplicate_of_id') { $fieldsParams2['is_duplicate_of_id'] = $human->getIsDuplicateOfId(); } } } } } } } } } $fieldsSQL2[] = " " . $field . "_changed "; $fieldsSQLParams2[] = " 0 "; } else { $fieldsSQL2[] = " " . $field . "_changed "; $fieldsSQLParams2[] = " -2 "; } } try { if (!$alreadyInTransaction) { $this->db->beginTransaction(); } // Information SQL $stat = $this->db->prepare("UPDATE human_information SET " . implode(",", $fieldsSQL1) . " WHERE id=:id"); $stat->execute($fieldsParams1); // History SQL $stat = $this->db->prepare("INSERT INTO human_history (" . implode(",", $fieldsSQL2) . ") VALUES (" . implode(",", $fieldsSQLParams2) . ")"); $stat->execute($fieldsParams2); if (!$alreadyInTransaction) { $this->db->commit(); } } catch (Exception $e) { if (!$alreadyInTransaction) { $this->db->rollBack(); } throw $e; } }