/** * Add a new post * * @param int $user_id User's id (relative to the users table) * @param string $message Message * @param int $category_id Category's id (relative to the categories table) * @param int $group_id Group's id (relative to the groups table) * @param boolean $official If true, the message is official in a group * @param boolean $private If true, the message will be visible only to the students * @return int Id of the new post */ public function addPost($user_id, $message, $category_id, $group_id, $official, $private, $dislike) { $id = $this->createQuery()->set(array('user_id' => $user_id, 'message' => $message, 'time' => time(), 'category_id' => $category_id, 'group_id' => $group_id, 'official' => $official ? 1 : 0, 'private' => $private ? 1 : 0, 'dislike' => $dislike ? 1 : 0))->insert(); self::clearCache(); // Add to the search index $search_model = new Search_Model(); $search_model->index(array('message' => Search_Model::sanitize($message), 'official' => $official, 'private' => $private), 'post', $id); return $id; }
* @example /usr/bin/php -f searchindex.php */ define('CLI_MODE', true); define('APP_DIR', realpath(dirname(__FILE__) . '/../') . '/'); define('CF_DIR', realpath(dirname(__FILE__) . '/../../confeature/') . '/'); define('DATA_DIR', realpath(dirname(__FILE__) . '/../../data/') . '/'); try { // Loading Confeature require_once CF_DIR . 'init.php'; $search_model = new Search_Model(); // Students indexing $search_model->delete('student'); $students = DB::createQuery('students')->fields('username', 'firstname', 'lastname')->select(); foreach ($students as $student) { $search_model->index(array('username' => $student['username'], 'firstname' => Search_Model::sanitize($student['firstname']), 'lastname' => Search_Model::sanitize($student['lastname'])), 'student', $student['username']); } // Posts indexing $search_model->delete('post'); $posts = DB::createQuery('posts')->fields('id', 'message', 'private', 'official')->select(); foreach ($posts as $post) { $search_model->index(array('message' => Search_Model::sanitize($post['message']), 'official' => $post['official'] == '1', 'private' => $post['private'] == '1'), 'post', $post['id']); } // Groups indexing $search_model->delete('group'); $groups = DB::createQuery('groups')->fields('id', 'name', 'url_name', 'description')->select(); foreach ($groups as $group) { $search_model->index(array('name' => Search_Model::sanitize($group['name']), 'url_name' => $group['url_name'], 'description' => Search_Model::sanitize($group['description'])), 'group', $group['id']); } } catch (Exception $e) { echo $e->getMessage(); }
/** * Save the data of a student * * @param string $username student's username * @param array $data student's data */ public function save($username, $data) { $student_data = array(); $old_data = DB::createQuery('students')->fields('firstname', 'lastname', 'student_number')->where(array('username' => $username))->select(); if (!$old_data[0]) { throw new Exception('Student not found'); } $old_data = $old_data[0]; $change_name = false; // Firstname if (isset($data['firstname']) && $old_data['firstname'] != trim($data['firstname'])) { if (trim($data['firstname']) == '') { throw new FormException('firstname'); } $student_data['firstname'] = trim($data['firstname']); $change_name = true; } // Lastname if (isset($data['lastname']) && $old_data['lastname'] != trim($data['lastname'])) { if (trim($data['lastname']) == '') { throw new FormException('lastname'); } $student_data['lastname'] = trim($data['lastname']); $change_name = true; } // Student number if (isset($data['student_number'])) { if (!ctype_digit(trim($data['student_number']))) { throw new FormException('student_number'); } $student_data['student_number'] = (int) trim($data['student_number']); // Moving the avatar if ($student_data['student_number'] != $old_data['student_number']) { // Thumb $avatar_path = self::getAvatarPath($student_data['student_number'], true); $avatar_dir = File::getPath($avatar_path); if (!is_dir($avatar_dir)) { File::makeDir($avatar_dir, 0777, true); } File::rename(self::getAvatarPath($old_data['student_number'], true), $avatar_path); // Big $avatar_path = self::getAvatarPath($student_data['student_number'], false); $avatar_dir = File::getPath($avatar_path); if (!is_dir($avatar_dir)) { File::makeDir($avatar_dir, 0777, true); } File::rename(self::getAvatarPath($old_data['student_number'], false), $avatar_path); } } // Promo if (isset($data['promo'])) { if (!ctype_digit(trim($data['promo'])) || (int) $data['promo'] < 2000) { throw new FormException('promo'); } $student_data['promo'] = (int) trim($data['promo']); } // Cesure if (isset($data['cesure'])) { $student_data['cesure'] = $data['cesure'] ? 1 : 0; } // Avatar if (isset($data['avatar_path']) && isset($data['student_number']) && File::exists($data['avatar_path'])) { $avatar_path = self::getAvatarPath((int) $data['student_number'], true); $avatar_dir = File::getPath($avatar_path); if (!is_dir($avatar_dir)) { File::makeDir($avatar_dir, 0777, true); } File::rename($data['avatar_path'], $avatar_path); } if (isset($data['avatar_big_path']) && isset($data['student_number']) && File::exists($data['avatar_big_path'])) { $avatar_path = self::getAvatarPath((int) $data['student_number'], false); $avatar_dir = File::getPath($avatar_path); if (!is_dir($avatar_dir)) { File::makeDir($avatar_dir, 0777, true); } File::rename($data['avatar_big_path'], $avatar_path); } // Update the DB $this->createQuery()->set($student_data)->where(array('username' => $username))->update(); if ($change_name) { Post_Model::clearCache(); // Update the search index $search_model = new Search_Model(); $search_model->index(array('username' => $username, 'firstname' => Search_Model::sanitize(isset($student_data['firstname']) ? $student_data['firstname'] : $old_data['firstname']), 'lastname' => Search_Model::sanitize(isset($student_data['lastname']) ? $student_data['lastname'] : $old_data['lastname'])), 'student', $username); } }
/** * Create a group * * @param array $data Group's data * @return string URL name */ public function create($data) { $group_data = array(); // Name $change_name = false; if (!isset($data['name'])) { throw new FormException('invalid_name'); } $name = trim($data['name']); $group_data['name'] = $name; // URL name $url_name = Text::forURL($name); if ($url_name == '') { throw new FormException('invalid_name'); } $i = ''; while (self::urlExists($url_name . $i)) { $i = $i == '' ? 1 : $i + 1; } $url_name .= $i; $group_data['url_name'] = $url_name; // Creation date if (!isset($data['creation_date']) || !($creation_date = strptime($data['creation_date'], __('GROUP_EDIT_FORM_CREATION_DATE_FORMAT_PARSE')))) { throw new FormException('invalid_creation_date'); } $group_data['creation_date'] = $creation_date['tm_year'] + 1900 . '-' . ($creation_date['tm_mon'] + 1) . '-' . $creation_date['tm_mday']; // Email if (isset($data['mail'])) { if ($data['mail'] != '' && !Validation::isEmail($data['mail'])) { throw new FormException('invalid_mail'); } $group_data['mail'] = $data['mail']; } // Description if (isset($data['description'])) { $group_data['description'] = $data['description']; } // Avatar if (!isset($data['avatar_path']) || !File::exists($data['avatar_path']) || !isset($data['avatar_big_path']) && !File::exists($data['avatar_big_path'])) { throw new FormException('avatar'); } // Insertion in the DB $id = $this->createQuery()->set($group_data)->insert(); // Avatar $avatar_path = self::getAvatarPath($id, true); $avatar_dir = File::getPath($avatar_path); if (!is_dir($avatar_dir)) { File::makeDir($avatar_dir, 0777, true); } File::rename($data['avatar_path'], $avatar_path); $avatar_path = self::getAvatarPath($id, false); $avatar_dir = File::getPath($avatar_path); if (!is_dir($avatar_dir)) { File::makeDir($avatar_dir, 0777, true); } File::rename($data['avatar_big_path'], $avatar_path); // Members if (isset($data['members']) && is_array($data['members'])) { $i = 0; foreach ($data['members'] as &$member) { $member['order'] = $i++; } if (count($data['members']) != 0) { $users = DB::createQuery('users')->fields('id')->where('id IN (' . implode(',', array_keys($data['members'])) . ')')->select(); foreach ($users as $user) { DB::createQuery('groups_users')->set(array('group_id' => $id, 'user_id' => $user['id'], 'title' => $data['members'][(int) $user['id']]['title'], 'admin' => $data['members'][(int) $user['id']]['admin'] ? '1' : '0', 'order' => $data['members'][(int) $user['id']]['order']))->insert(); } } } // Add to the search index $search_model = new Search_Model(); $search_model->index(array('name' => Search_Model::sanitize($group_data['name']), 'url_name' => $group_data['url_name'], 'description' => Search_Model::sanitize($group_data['description'])), 'group', $id); self::clearCache(); return $url_name; }