Exemplo n.º 1
0
 public function register($name, $email = null, $password = null)
 {
     $params = ['email' => $email, 'password' => password_hash($password, PASSWORD_DEFAULT)];
     // Add the user.
     $userCount = 'SELECT COUNT(*) FROM `users` WHERE `name` LIKE :name';
     $userNum = $this->db->query($userCount, ['name' => "{$name}%"])->fetchColumn();
     $userName = $userNum > 0 ? $name . ' ' . ($userNum + 1) : $name;
     $params['name'] = $userName;
     $this->db->query("INSERT INTO users SET name=:name, email=:email, password=:password", $params);
     $userId = $this->db->lastInsertId();
     // Add the new user to a group of their own.
     $groupCountSql = 'SELECT COUNT(*) FROM `groups` WHERE `name` LIKE :name';
     $groupNum = $this->db->query($groupCountSql, ['name' => "{$name}%"])->fetchColumn();
     $groupName = $groupNum > 0 ? $name . ' ' . ($groupNum + 1) : $name;
     $this->db->query('INSERT INTO `groups` SET `name`=:name', ['name' => $groupName]);
     $personalGroupId = $this->db->lastInsertId();
     $groupMemberSql = 'INSERT INTO `user_groups` SET `user`=:u, `group`=:g';
     $this->db->query($groupMemberSql, ['u' => $userId, 'g' => $personalGroupId]);
     // Make it their default group.
     $defaultGroupSql = "UPDATE `users` SET `default_group` = :g WHERE `id`=:u";
     $this->db->query($defaultGroupSql, ['g' => $personalGroupId, 'u' => $userId]);
     // Also add them to the public group.
     $groupMemberSql = 'INSERT INTO `user_groups` SET `user`=:u, `group`=:g';
     $this->db->query($groupMemberSql, ['u' => $userId, 'g' => self::GROUP_PUBLIC]);
     // Reload the user's data.
     $this->load($userId);
 }
Exemplo n.º 2
0
 /**
  * Save an item's data.
  *
  * @param string[] $metadata Array of metadata pairs.
  * @param string $tagsString CSV string of tags.
  * @param string $filename The full filesystem path to a file to attach to this Item. Don't use with $fileContents.
  * @param string $fileContents A string to treat as the contents of a file. Don't use with $filename.
  * @return false
  */
 public function save($metadata, $tagsString = null, $filename = null, $fileContents = null)
 {
     if (isset($metadata['id'])) {
         $this->load($metadata['id']);
     }
     if (!$this->editable()) {
         throw new \Exception("You are not allowed to edit this item.");
     }
     if (empty($metadata['title'])) {
         $metadata['title'] = 'Untitled';
     }
     if (empty($metadata['description'])) {
         $metadata['description'] = null;
     }
     if (empty($metadata['date'])) {
         $metadata['date'] = null;
     }
     if (empty($metadata['date_granularity'])) {
         $metadata['date_granularity'] = self::DATE_GRANULARITY_DEFAULT;
     }
     if (empty($metadata['edit_group'])) {
         $metadata['edit_group'] = $this->getEditGroup()->id;
     }
     if (empty($metadata['read_group'])) {
         $metadata['read_group'] = $this->getReadGroup()->id;
     }
     $setClause = 'SET title=:title, description=:description, date=:date, ' . ' date_granularity=:date_granularity, edit_group=:edit_group, read_group=:read_group ';
     // Start a transaction. End after the key words and files have been written.
     $this->db->query('BEGIN');
     if ($this->isLoaded()) {
         // Update?
         $metadata['id'] = $this->getId();
         $sql = "UPDATE items {$setClause} WHERE id=:id";
         $this->db->query($sql, $metadata);
         $id = $metadata['id'];
     } else {
         // Or insert?
         unset($metadata['id']);
         $sql = "INSERT INTO items {$setClause}";
         $this->db->query($sql, $metadata);
         $id = $this->db->lastInsertId();
     }
     $this->load($id);
     // Save tags.
     if (!empty($tagsString)) {
         $this->db->query("DELETE FROM item_tags WHERE item=:id", ['id' => $id]);
         $tags = array_map('trim', array_unique(str_getcsv($tagsString)));
         foreach ($tags as $tag) {
             $this->db->query("INSERT IGNORE INTO tags SET title=:title", ['title' => $tag]);
             $selectTagId = "SELECT id FROM tags WHERE title LIKE :title";
             $tagId = $this->db->query($selectTagId, ['title' => $tag])->fetchColumn();
             $insertJoin = "INSERT IGNORE INTO item_tags SET item=:item, tag=:tag";
             $this->db->query($insertJoin, ['item' => $id, 'tag' => $tagId]);
         }
     }
     $newVer = $this->getVersionCount() + 1;
     // Save file contents.
     if (!empty($fileContents)) {
         $filesystem = App::getFilesystem();
         $filesystem->put("storage://" . $this->getFilePath($newVer), $fileContents);
     }
     // Save uploaded file.
     if (!empty($filename)) {
         $filesystem = App::getFilesystem();
         $stream = fopen($filename, 'r+');
         $filesystem->putStream("storage://" . $this->getFilePath($newVer), $stream);
         fclose($stream);
     }
     // End the transaction and reload the data from the DB.
     $this->db->query('COMMIT');
 }