public function editGroup(UserGroup $group) { if (empty($group->id) or !is_numeric($group->id)) { throw new InvalidArgumentException("Group id have to be non empty string"); } if (empty($group->name)) { throw new InvalidArgumentException("Group name have to be non empty string"); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_GROUPS', 'UserManager'))->set(new Field('name'), $group->name)->set(new Field('description'), $group->description)->where($qb->expr()->equal(new Field('id'), $group->id)); return $this->query->exec($qb->getSQL())->affected(); }
public function updateAttachmentMessageId($attachmentId, $newMessageId) { if (empty($attachmentId) or !is_numeric($attachmentId)) { throw new InvalidIntegerArgumentException("\$attachmentId have to be non zero integer."); } if (empty($newMessageId) or !is_numeric($newMessageId)) { throw new InvalidIntegerArgumentException("\$newMessageId have to be non zero integer."); } $convMgr = Reg::get(ConfigManager::getConfig("Messaging", "Conversations")->Objects->ConversationManager); $filter = new ConversationMessagesFilter(); $filter->setId($newMessageId); $message = $convMgr->getConversationMessage($filter); $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_CONVERSATION_ATTACHEMENTS'))->set(new Field('message_id'), $message->id)->where($qb->expr()->equal(new Field('id'), $attachmentId)); MySqlDbManager::getDbObject()->startTransaction(); try { $convMgr->setMessageHasAttachment($message); $affected = $this->query->exec($qb->getSQL())->affected(); if (!MySqlDbManager::getDbObject()->commit()) { MySqlDbManager::getDbObject()->rollBack(); } } catch (Exception $e) { MySqlDbManager::getDbObject()->rollBack(); throw $e; } }
/** * Update current yubikey * @param YubikeyObject $key * @throws YubikeyException * @return Ambigous <boolean, number> */ public function updateYubikey(YubikeyObject $key) { if (empty($key)) { throw new YubikeyException("given yey object is empty"); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_KEYS', 'YubikeyUserAuthorization'))->set(new Field("key"), $key->key)->set(new Field("Description"), $key->description)->set(new Field("status"), $key->status)->where($qb->expr()->equal(new Field('id'), $key->id)); return $this->query->exec($qb->getSQL())->affected(); }
public static function updateHost(Host $host) { if (empty($host->id)) { throw new InvalidArgumentException("HostId is empty!"); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_HOSTS', 'Host'))->set(new Field('host'), $host->host)->set(new Field('subdomain'), $host->subdomain)->where($qb->expr()->equal(new Field('id'), $host->id)); $sql = MySqlDbManager::getQueryObject(); $sql->exec($qb->getSQL()); }
/** * Update aliase map of record with given id * * @param int $id alias Id * @param string $map new aliase map * @return bool */ public function updateMap($id, $map) { if (empty($id) or !is_numeric($id)) { throw new InvalidArgumentException("\$id have to be non zero integer"); } if (empty($map)) { throw new InvalidArgumentException("\$map have to be non empty string"); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_ALIAS'))->set(new Field('map'), $map)->where($qb->expr()->equal(new Field('id'), $id)); $this->query->exec($qb->getSQL()); }
public function updateGroup(TextsGroup $group) { if (empty($group->id)) { throw new InvalidArgumentException("Group ID have to be specified"); } if (!is_numeric($group->id)) { throw new InvalidArgumentException("Group ID have to be integer"); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_TEXTS_GROUPS'))->set(new Field('name'), $group->name)->where($qb->expr()->equal(new Field('id'), $group->id)); $this->query->exec($qb->getSQL()); return $this->query->affected(); }
/** * Validate given code using paramsArray * @param string $code * @param array $paramsArray * @return boolean */ public function validate($code, $paramsArray = array()) { if (empty($code)) { throw new InvalidArgumentException("Empty \$code supplied for validation!"); } $qb = new QueryBuilder(); $orX = new Orx(); $orX->add($qb->expr()->isNull(new Field('valid_until'))); $orX->add($qb->expr()->greaterEqual(new Field('valid_until'), new Func('NOW'))); $qb->select(new Field('*'))->from(Tbl::get('TBL_ONE_TIME_CODES'))->where($qb->expr()->equal(new Field('code'), $code))->andWhere($orX); $this->query->exec($qb->getSQL()); if ($this->query->countRecords() == 0) { return false; } $dbRow = $this->query->fetchRecord(); $paramsArrayFromCode = $this->getArrayFromCode($dbRow['code']); if ($paramsArrayFromCode === false) { return false; } $resultingArray = array_diff_assoc($paramsArray, $paramsArrayFromCode); if (count($resultingArray) != 0) { return false; } if ($dbRow['multi'] == '1') { if ($dbRow['usage_limit'] > 0) { $qb = new QueryBuilder(); if ($dbRow['usage_count'] < $dbRow['usage_limit']) { $qb->update(Tbl::get('TBL_ONE_TIME_CODES'))->set(new Field('usage_count'), $qb->expr()->sum(new Field('usage_count'), 1))->where($qb->expr()->equal(new Field('id'), $dbRow['id'])); $this->query->exec($qb->getSQL()); } else { $qb->delete(Tbl::get('TBL_ONE_TIME_CODES'))->where($qb->expr()->equal(new Field("id"), $dbRow['id'])); $this->query->exec($qb->getSQL()); return false; } } } else { $qb = new QueryBuilder(); $qb->delete(Tbl::get('TBL_ONE_TIME_CODES'))->where($qb->expr()->equal(new Field("id"), $dbRow['id'])); $this->query->exec($qb->getSQL()); } return true; }
public static function attempt(array $credentials, $remember_me = false) { $input_email = filter_var($credentials['email'], FILTER_SANITIZE_STRING); $input_password = filter_var($credentials['pass'], FILTER_SANITIZE_STRING); //run validation $v = new Validator($credentials); $v->rule('email', 'email')->message('Please provide an valid Email address'); $v->rule('required', 'email')->message('Email is required'); $v->rule('required', 'pass')->message('Please provide an password'); if (!$v->validate()) { return array('errors' => $v->errors()); } //data valid so proceed to next try { $m = new \QueryBuilder(); $user = $m->select('users', array('email = :email', array('email' => array($input_email, \PDO::PARAM_STR)))); if (!$user) { throw new \Exception('No user found with this credential', 5002); } $stored_password_hash = $user['password']; if (password_verify($input_password, $stored_password_hash)) { $_SESSION['auth.user.logged_in'] = true; $_SESSION['auth.user.id'] = $user['id']; if ($remember_me === true) { $auth_config = load_config('auth'); //set token and it's validity period $remember_token = uniqid('rem_'); $remember_validity = time() + $auth_config['login_cookie_expire']; $m->update('users', array('remember_token' => array($remember_token, \PDO::PARAM_STR), 'token_validity' => array(date('Y-m-d H:i:s', $remember_validity), \PDO::PARAM_STR)), array('id = :id', array('id' => array(intval($user['id']), \PDO::PARAM_INT)))); setcookie($auth_config['login_cookie_name'], $remember_token, $remember_validity); } return true; } else { throw new \Exception('Invalid credential. Please provide valid username and password.', 5003); } } catch (\Exception $ex) { throw $ex; } }
public function declinePhoto(UserPhoto $photo) { if (empty($photo->id)) { throw new InvalidArgumentException("UserPhoto object has no id!"); } if (empty($photo->userId)) { $photo = $this->getPhoto($photo->id); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_USERS_PHOTOS'))->set(new Field('status'), static::MODERATION_STATUS_DECLINED)->set(new Field('modification_date'), new Func('NOW'))->where($qb->expr()->equal(new Field('id'), $photo->id)); $this->query->exec($qb->getSQL()); $this->correctDefaultPhoto($photo->userId); }
public function save() { if (empty($this->modified_data) && !$this->new) { return true; } if ($this->validate()) { $data = array_intersect_key($this->data, array_flip(static::$fields)); if ($this->new) { $sql = QueryBuilder::insert(get_called_class()); $sql->values($data); } else { $pk = static::$primary_key; $data = array_intersect_key($data, array_flip($this->modified_data)); $sql = QueryBuilder::update(get_called_class()); $sql->set($data); $sql->where(array($pk => $this->{$pk})); } if (method_exists($this, 'before_save')) { if (!$this->before_save($sql)) { $this->errors[] = "before_save failed"; return false; } } if (!$sql->execute()) { $this->error[] = "Save failed on SQL-Level!"; return false; } if (method_exists($this, 'after_create') && $this->new) { $this->after_create(); } elseif (method_exists($this, 'after_update')) { $this->after_update(); } if (method_exists($this, 'after_save')) { $this->after_save(); } return true; } else { $this->errors[] = "validation failed"; return false; } }
/** * Lock job queue for working on current job * Helper funcion * @access private * @param JobQueueObj $job * @return TRUE|FALSE */ private function lockJob(JobQueueObj $job) { if (!is_numeric($job->id)) { throw JobQueueException("Job id is not numeric!"); } $qb = new QueryBuilder(); $qb->update(TBL::get('TBL_JOB_QUEUE'))->set(new Field('status'), self::JOB_STATUS_IN_PROCESS)->set(new Field('start_date'), new Func('NOW'))->where($qb->expr()->equal(new Field('id'), $job->id)); return $this->query->exec($qb->getSQL())->affected(); }
private static function updateQueryString(array $pageInfo, $id) { $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_PAGE_INFO', 'PageInfo'))->set(new Field('title'), $pageInfo['title'])->set(new Field('meta_keywords'), $pageInfo['keywords'])->set(new Field('meta_description'), $pageInfo['description'])->where($qb->expr()->equal(new Field('id'), $id)); return $qb->getSQL(); }
/** * Generated from @assert update('user')->set(array('key1'=>'value1', 'key2'=>2))->where(array(array('id', 1), array('id', 1)))->text() [==] "UPDATE `user` SET `key1`=:Skey1, `key2`=:Skey2 WHERE `id` = 1 AND `id` = 1". * * @covers Kotchasan\Database\QueryBuilder::update */ public function testUpdate() { $this->assertEquals("UPDATE `user` SET `key1`=:Skey1, `key2`=:Skey2 WHERE `id` = 1 AND `id` = 1", $this->object->update('user')->set(array('key1' => 'value1', 'key2' => 2))->where(array(array('id', 1), array('id', 1)))->text()); }
public function changeConversationHasAttachmentStatus($uuid, $status, $userId = null) { if (empty($uuid) or !is_numeric($uuid)) { throw new InvalidIntegerArgumentException("\$uuid have to be non zero integer."); } if (!is_numeric($status) or !in_array($status, $this->getConstsArray("STATUS_HAS_ATTACHMENT"))) { throw new InvalidIntegerArgumentException("Invalid \$status specified"); } if ($userId !== null and (empty($userId) or !is_numeric($userId))) { throw new InvalidIntegerArgumentException("\$userId have to be non zero integer."); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_CONVERSATIONS'))->set(new Field('has_attachment'), $status)->where($qb->expr()->equal(new Field('uuid'), $uuid)); if ($userId !== null and !empty($userId) and is_numeric($userId)) { $qb->andWhere($qb->expr()->equal(new Field('user_id'), $userId)); } $this->query->exec($qb->getSQL()); $hookParams = array('type' => 'hasAttach', 'uuid' => $uuid, 'hasAttach' => $status); HookManager::callHook("ConversationUpdate", $hookParams); }
public static function setHostsDefaultLanguage(Host $host, Language $language) { $sql = MySqlDbManager::getQueryObject(); $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_HOST_LANGUAGE'))->set(new Field("default"), 0)->where($qb->expr()->equal(new Field('host_id'), $host->id)); $sql->exec($qb->getSQL()); $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_HOST_LANGUAGE'))->set(new Field("default"), 1)->where($qb->expr()->equal(new Field('host_id'), $host->id))->andWhere($qb->expr()->equal(new Field('lang_id'), $language->id)); $sql->exec($qb->getSQL()); }
public function updateText(Text $text) { if (empty($text->id)) { throw new InvalidArgumentException("Text ID have to be specified"); } if (!is_numeric($text->id)) { throw new InvalidArgumentException("Text ID have to be integer"); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_TEXTS'))->set('group_id', $text->group->id)->set('name', $text->name)->set('description', $text->description)->where($qb->expr()->equal(new Field('id'), $text->id)); $this->query->exec($qb->getSQL()); return $this->query->affected(); }
/** * Edit an already created answer option * * @param integer $profile_id * @param string $new_answer * @param integer $sort_id */ public function editOptionById($profile_id, $new_answer, $sort_id = null) { $additional_sql = ''; $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_PROFILE_KEYS'))->set(new Field('value'), $new_answer); if (empty($profile_id) or !is_numeric($profile_id)) { throw new InvalidArgumentException("\$profile have to be numeric id of the profile"); } if (empty($new_answer)) { throw new InvalidArgumentException("\$new_answer have be not null string"); } if (!empty($sort_id) and !is_numeric($sort_id)) { throw new InvalidArgumentException("\$sort_id have to have numeric value"); } else { $qb->set(new Field('sort_id'), $sort_id); $additional_sql .= ", `sort_id`='{$sort_id}'"; } $qb->where($qb->expr()->equal(new Field('id'), $profile_id)); $this->query->exec($qb->getSQL()); }
/** * Update DB Config * @param ConfigDB $oldDBCOnfig * @param ConfigDB $newDBConfig * @throws InvalidArgumentException */ public static function updateConfigValue(ConfigDB $oldDBCOnfig, ConfigDB $newDBConfig) { if (empty($oldDBCOnfig) || empty($newDBConfig)) { throw new InvalidArgumentException("new or old DB config object is empty"); } if (!isset($oldDBCOnfig->location) or empty($oldDBCOnfig->location)) { throw new InvalidArgumentException("odl Location of config should be non empty array"); } if (!isset($newDBConfig->location) or empty($newDBConfig->location)) { throw new InvalidArgumentException("New Location of config should be non empty array"); } if (!isset($oldDBCOnfig->name) or empty($oldDBCOnfig->name)) { throw new InvalidArgumentException("Old Name of config should be specified"); } if (!isset($newDBConfig->name) or empty($newDBConfig->name)) { throw new InvalidArgumentException("New Name of config should be specified"); } if (!isset($newDBConfig->value)) { throw new InvalidArgumentException("Value of new config should be specified"); } $odlHostLangid = null; if (!empty($oldDBCOnfig->host) && !empty($oldDBCOnfig->language)) { $odlHostLangid = HostLanguageManager::getHostLanguageId($oldDBCOnfig->host, $oldDBCOnfig->language); } $newHostLangId = null; if (!empty($newDBConfig->host) && !empty($newDBConfig->language)) { $newHostLangId = HostLanguageManager::getHostLanguageId($newDBConfig->host, $newDBConfig->language); } $sql = MySqlDbManager::getQueryObject(); $qb = new QueryBuilder(); $qb->update(Tbl::get("TBL_CONFIGS"))->set(new Field("location"), implode(":", $newDBConfig->location))->set(new Field("name"), $newDBConfig->name)->set(new Field("value"), $newDBConfig->value)->where($qb->expr()->equal(new Field("location"), implode(":", $oldDBCOnfig->location)))->andWhere($qb->expr()->equal(new Field("name"), $oldDBCOnfig->name)); if ($newHostLangId !== null) { $qb->set(new Field("host_lang_id"), $newHostLangId); } else { $qb->set(new Field("host_lang_id"), new Literal("null")); } if ($odlHostLangid !== null) { $qb->andWhere($qb->expr()->equal(new Field("host_lang_id"), $odlHostLangid)); } else { $qb->andWhere($qb->expr()->isNull(new Field("host_lang_id"))); } $sql->exec($qb->getSQL()); }
/** * @param integer $inviterUserId * @param integer $invitedUserId * @deprecated Sessions log insertd by mysql TRIGGER chat_sessions_log */ protected function insertSessionLog($inviterUserId, $invitedUserId) { if ($inviterUserId > $invitedUserId) { $userId1 = $inviterUserId; $userId2 = $invitedUserId; } else { $userId1 = $invitedUserId; $userId2 = $inviterUserId; } $qb = new QueryBuilder(); $qb->select(new Field('id'))->from(Tbl::get('TBL_CHAT_SESSIONS_LOG')); $andClause1 = new Andx(); $andClause1->add($qb->expr()->equal(new Field('user1_id', Tbl::get('TBL_CHAT_SESSIONS_LOG')), $userId1)); $andClause1->add($qb->expr()->equal(new Field('user2_id', Tbl::get('TBL_CHAT_SESSIONS_LOG')), $userId2)); $andClause2 = new Andx(); $andClause2->add($qb->expr()->equal(new Field('user1_id', Tbl::get('TBL_CHAT_SESSIONS_LOG')), $userId2)); $andClause2->add($qb->expr()->equal(new Field('user2_id', Tbl::get('TBL_CHAT_SESSIONS_LOG')), $userId1)); $orClause = new Orx(); $orClause->add($andClause1); $orClause->add($andClause2); $qb->andWhere($orClause); $this->query->exec($qb->getSQL()); $qb = new QueryBuilder(); if ($this->query->countRecords()) { $sesionId = $this->query->fetchField("id"); $qb->update(Tbl::get('TBL_CHAT_SESSIONS_LOG'))->set(new Field('datetime'), date(DEFAULT_DATETIME_FORMAT))->where($qb->expr()->equal(new Field('id'), $sesionId)); } else { $qb->insert(Tbl::get('TBL_CHAT_SESSIONS_LOG'))->values(array('user1_id' => $userId1, 'user2_id' => $userId2, 'datetime' => date(DEFAULT_DATETIME_FORMAT))); } $this->query->exec($qb->getSQL()); return $this->query->affected(); }
/** * Updated User Properties * * @param UserProperties $properties * @return integer Affected */ protected function updateUserProperties(UserProperties $properties) { if (count($this->config->userPropertiesMap->toArray()) > 0) { $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_USERS_PROPERTIES')); foreach ($this->config->userPropertiesMap as $objectKey => $dbKey) { $qb->set(new Field($dbKey), $properties->{$objectKey}); } $qb->where($qb->expr()->equal(new Field('user_id'), $properties->userId)); return $this->query->exec($qb->getSQL())->affected(); } return 0; }
public function updateTextValue(TextValue $textValue) { if (empty($textValue->id) or !is_numeric($textValue->id)) { throw new InvalidArgumentException("No ID specified in TextValue object"); } if (empty($textValue->text) or !is_a($textValue->text, "Text")) { throw new InvalidArgumentException("You have to specify valid Text object"); } if (empty($textValue->value)) { throw new InvalidArgumentException("You have to specify Value attribute"); } if (is_null($textValue->display) or !is_numeric($textValue->display)) { throw new InvalidArgumentException("You have to specify valid Display attribute"); } if (!empty($textValue->hostLanguageId) and is_numeric($textValue->hostLanguageId)) { $hostLanguageId = $textValue->hostLanguageId; } else { if (empty($textValue->host) or !is_a($textValue->host, "Host")) { throw new InvalidArgumentException("You have to specify valid Host object"); } if (empty($textValue->language) or !is_a($textValue->language, "Language")) { throw new InvalidArgumentException("You have to specify valid Language object"); } $hostLanguageId = HostLanguageManager::getHostLanguageId($textValue->host, $textValue->language); } $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_TEXTS_VALUES'))->set(new Field('text_id'), $textValue->text->id)->set(new Field('value'), $textValue->value)->set(new Field('host_language'), $hostLanguageId)->set(new Field('display'), $textValue->display)->set(new Field('text_id'), $textValue->text->id)->where($qb->expr()->equal(new Field('id'), $textValue->id)); $this->query->exec($qb->getSQL()); return $this->query->affected(); }
protected function addMessageToConversation($uuid, $senderId, $message) { if (empty($uuid) or !is_numeric($uuid)) { throw InvalidArgumentException("UUID have to be non zero integer."); } if (empty($senderId) or !is_numeric($senderId)) { throw new InvalidIntegerArgumentException("senderId have to be non zero integer."); } // Get Conversation $filter = new ConversationFilter(); $filter->setUUID($uuid); $filter->setUserId($senderId); $conversation = $this->getConversation($filter, true); // Insert new message into DB $qb = new QueryBuilder(); $qb->insert(Tbl::get('TBL_CONVERSATION_MESSAGES'))->values(array('uuid' => $uuid, 'sender_id' => $senderId, 'receiver_id' => $conversation->interlocutorId, 'message' => $message)); $this->query->exec($qb->getSQL()); $messageId = $this->query->getLastInsertId(); // Mark conversation as unread for interlocutor $this->markConversationAsUnread($conversation->interlocutorId, $uuid); // Get Interlocutors Conversation $filter = new ConversationFilter(); $filter->setUUID($uuid); $filter->setUserId($conversation->interlocutorId); $interConv = $this->getConversation($filter, true); // Restore conversation if it is trashed or deleted if ($interConv->trashed != self::STATUS_TRASHED_NOT_TRAHSED) { $this->restoreConversation($conversation->interlocutorId, $uuid); } // Update Conversation last message date $qb = new QueryBuilder(); $qb->update(Tbl::get('TBL_CONVERSATIONS'))->set(new Field('last_msg_date'), new Literal((string) new Func("NOW")))->where($qb->expr()->equal(new Field('uuid'), $uuid)); $this->query->exec($qb->getSQL()); return $messageId; }