/** * Creates a new user. Returns false if error, or the new user's id if success * * @param string $email * @param string $name * @param string $password * @param boolean $admin * @return mixed * @throws UserManagementException */ public static function create($email, $name, $password, $admin = false) { // Check if email is valid if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new UserManagementException('Invalid email ' . $email); return false; } // Validate password if (!self::validatePassword($password)) { return false; } // Validate username if (empty($name) || strlen($name) > 50) { throw new UserManagementException('Name cannot be empty, and no more than 50 characters'); return false; } // Check if user with this email already exists $query = new \TaskerMAN\Core\DBQuery("SELECT `email`\r\n\t\t\tFROM `users`\r\n\t\t\tWHERE `email` = ?\r\n\t\t\tLIMIT 1\r\n\t\t"); $query->execute($email); if ($query->rowCount() > 0) { throw new UserManagementException('User with email ' . $email . ' already exists'); return false; } // Hash password $password = password_hash($password, PASSWORD_DEFAULT); // Generate API Token $api_token = API::generateAPIToken(); // Store user $query = new \TaskerMAN\Core\DBQuery("INSERT INTO `users`\r\n\t\t\t(`email`, `name`, `password`, `admin`, `api_token`)\r\n\t\t\tVALUES\r\n\t\t\t(:email, :name, :password, :admin, :api_token)\r\n\t\t"); $query->bindValue(':email', $email); $query->bindValue(':name', $name); $query->bindValue(':password', $password); $query->bindValue(':admin', (int) $admin); $query->bindValue(':api_token', $api_token); $query->execute(); return (int) $query->lastInsertID(); }
/** * Saves the task object to the database, also triggers buildSteps() if * this is a new task. * * @return boolean * @throws TaskException */ public function save() { // Check if task is new. If so, INSERT query is run if ($this->new_task) { if (empty($this->temp_steps)) { // Each task must have at least one step associated with it throw new TaskException('Task creation requires at least one associated step'); return false; } $stmt = new \TaskerMAN\Core\DBQuery("INSERT INTO `tasks`\r\n\t\t\t\t(`created_uid`, `created_time`, `assignee_uid`, `due_by`, `completed_time`, `status`, `title`)\r\n\t\t\t\tVALUES\r\n\t\t\t\t(:created_uid, NOW(), :assignee_uid, :due_by, :completed_time, :status, :title)\r\n\t\t\t"); } else { $stmt = new \TaskerMAN\Core\DBQuery("UPDATE `tasks` SET\r\n\t\t\t\t`created_uid` = :created_uid,\r\n\t\t\t\t`assignee_uid` = :assignee_uid,\r\n\t\t\t\t`due_by` = :due_by,\r\n\t\t\t\t`completed_time` = :completed_time,\r\n\t\t\t\t`status` = :status,\r\n\t\t\t\t`title`= :title\r\n\r\n\t\t\t\tWHERE `id` = :id\r\n\t\t\t\tLIMIT 1\r\n\t\t\t"); $stmt->bindValue(':id', (int) $this->id, \PDO::PARAM_INT); } // Bind variables $stmt->bindValue(':created_uid', (int) $this->created_uid, \PDO::PARAM_INT); $stmt->bindValue(':assignee_uid', (int) $this->assignee_uid, \PDO::PARAM_INT); $stmt->bindValue(':due_by', (string) $this->due_by, \PDO::PARAM_STR); $stmt->bindValue(':completed_time', (string) $this->completed_time, \PDO::PARAM_STR); $stmt->bindValue(':status', (int) $this->status, \PDO::PARAM_INT); $stmt->bindValue(':title', (string) $this->title, \PDO::PARAM_STR); $stmt->execute(); // If this is a new task, run buildSteps() if ($this->new_task) { $this->id = $stmt->lastInsertID(); $this->new_task = false; $this->buildSteps(); } }
/** * Saves any changes to the database */ public function save() { if ($this->new_step) { $query = new \TaskerMAN\Core\DBQuery("INSERT INTO `steps`\r\n\t\t\t\t(`task_id`, `title`, `comment`)\r\n\t\t\t\tVALUES\r\n\t\t\t\t(:task_id, :title, :comment)\r\n\t\t\t"); $query->bindValue(':task_id', $this->task_id); $query->bindValue(':title', $this->title); $query->bindValue(':comment', $this->comment); $query->execute(); $this->id = $query->lastInsertID(); } else { $query = new \TaskerMAN\Core\DBQuery("UPDATE `steps` SET\r\n\t\t\t\t`title` = :title,\r\n\t\t\t\t`comment` = :comment\r\n\r\n\t\t\t\tWHERE `id` = :id\r\n\t\t\t\tLIMIT 1\r\n\t\t\t"); $query->bindValue(':id', $this->id); $query->bindValue(':title', $this->title); $query->bindValue(':comment', $this->comment); $query->execute(); } }