public static function attachmentDelete($post_id, $attach_id) { $id = DB::createQuery('post_comments')->fields('id')->where(array('attachment_id' => $attach_id, 'post_id' => $post_id))->select(); for ($i = 0; $i < count($id); $i++) { $id = $this->createQuery()->delete($id[$i]['id']); } Post_Model::clearCache(); }
/** * Add a vote in a survey * * @param int $id Id of the survey * @param array $id Ids of the answers * @param string $username User name * @return int Id of the corresponding post */ public function vote($id, $votes, $username) { if (count($votes) == 0) { throw new Exception('You should give at least one answer'); } $surveys = $this->createQuery()->select($id); if (!isset($surveys[0])) { throw new Exception('Survey not found'); } $survey = $surveys[0]; unset($surveys); if ($survey['multiple'] != '1' && count($votes) != 1) { throw new Exception('You must choose exactly one answer'); } if (strtotime($survey['date_end']) < time()) { throw new Exception('The survey is closed'); } $post_model = new Post_Model(); $post = $post_model->getRawPost((int) $survey['post_id']); $answers = DB::createQuery('survey_answers')->fields('id', 'votes')->where(array('survey_id' => $survey['id']))->select(); foreach ($answers as $answer) { $answer['votes'] = $answer['votes'] == '' ? array() : json_decode($answer['votes'], true); if (in_array($username, $answer['votes']) && !in_array((int) $answer['id'], $votes)) { array_splice($answer['votes'], array_search($username, $answer['votes']), 1); $weight = -1; } else { if (!in_array($username, $answer['votes']) && in_array((int) $answer['id'], $votes)) { $answer['votes'][] = $username; $weight = 1; } else { continue; } } DB::createQuery('survey_answers')->set(array('votes' => json_encode($answer['votes']), 'nb_votes = nb_votes' . ($weight == 1 ? '+1' : '-1')))->update((int) $answer['id']); } Post_Model::clearCache(); return (int) $post['id']; }
/** * Delete a comment * * @param int $id Id of the comment */ public function delete($id) { $id = $this->createQuery()->delete($id); Post_Model::clearCache(); }
/** * 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); } }
public function addAttachment($param) { $this->setView('iframe_add.php'); $is_logged = isset(User_Model::$auth_data); $is_admin = $is_logged && User_Model::$auth_data['admin'] == '1'; @set_time_limit(0); $uploaded_files = array(); $attachments = array(); try { if ($is_admin && isset($param['id']) && isset($_FILES['attachment_photo']) && is_array($_FILES['attachment_photo']['name'])) { foreach ($_FILES['attachment_photo']['size'] as $size) { if ($size > Config::UPLOAD_MAX_SIZE_PHOTO) { throw new Exception(__('POST_ADD_ERROR_PHOTO_SIZE', array('size' => File::humanReadableSize(Config::UPLOAD_MAX_SIZE_PHOTO)))); } } if ($filepaths = File::upload('attachment_photo')) { foreach ($filepaths as $filepath) { $uploaded_files[] = $filepath; } foreach ($filepaths as $i => $filepath) { $name = isset($_FILES['attachment_photo']['name'][$i]) ? $_FILES['attachment_photo']['name'][$i] : ''; try { $img = new Image(); $img->load($filepath); $type = $img->getType(); if ($type == IMAGETYPE_JPEG) { $ext = 'jpg'; } else { if ($type == IMAGETYPE_GIF) { $ext = 'gif'; } else { if ($type == IMAGETYPE_PNG) { $ext = 'png'; } else { throw new Exception(); } } } if ($img->getWidth() > 800) { $img->setWidth(800, true); } $img->save($filepath); // Thumb $thumbpath = $filepath . '.thumb'; $img->thumb(Config::$THUMBS_SIZES[0], Config::$THUMBS_SIZES[1]); $img->setType(IMAGETYPE_JPEG); $img->save($thumbpath); unset($img); $attachments[] = array($filepath, $name, $thumbpath); $uploaded_files[] = $thumbpath; } catch (Exception $e) { throw new Exception(__('POST_ADD_ERROR_PHOTO_FORMAT')); } } } // Attach files foreach ($attachments as $attachment) { $this->model->attachFile($param['id'], $attachment[0], $attachment[1], isset($attachment[2]) ? $attachment[2] : null); } $this->addJSCode(' parent.location = "' . Config::URL_ROOT . Routes::getPage('post', array('id' => $param['id'])) . '"; '); } Post_Model::clearCache(); } catch (Exception $e) { // Delete all uploading files in tmp foreach ($uploaded_files as $uploaded_file) { File::delete($uploaded_file); } $this->addJSCode(' with(parent){ Post.errorForm(' . json_encode($e->getMessage()) . '); } '); } }
public function isepdorPage($param) { $this->setView('isepdor.php'); $questions = $this->model->getquestions(); for ($i = 0; $i < count($questions); $i++) { $type = explode(',', $questions[$i]["type"]); $tab = array("students", "associations", "employees", "events"); $result = array_intersect($type, $tab); if (in_array("students", $result)) { $questions[$i]["students"] = 1; } else { $questions[$i]["students"] = 0; } if (in_array("events", $result)) { $questions[$i]["events"] = 1; } else { $questions[$i]["events"] = 0; } if (in_array("associations", $result)) { $questions[$i]["associations"] = 1; } else { $questions[$i]["associations"] = 0; } if (in_array("employees", $result)) { $questions[$i]["employees"] = 1; } else { $questions[$i]["employees"] = 0; } if ($questions[$i]["extra"] == null) { $questions[$i]["extra"] = " "; } } $events = $this->model->getevents(); for ($i = 0; $i < count($events); $i++) { if ($events[$i]['extra'] == "soiree") { $events[$i]['extra'] = 1; } else { $events[$i]['extra'] = 0; } } $myFile = DATA_DIR . Config::DIR_DATA_STORAGE . Config::DIR_DATA_ADMIN . "/diplome.json"; $file = fopen($myFile, 'r'); $positions = fread($file, filesize($myFile)); fclose($file); $this->addJSCode(' Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxcore.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxdata.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxbuttons.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxscrollbar.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxmenu.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxgrid.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxgrid.edit.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxgrid.selection.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxgrid.sort.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxgrid.filter.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxgrid.columnsresize.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxlistbox.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxdropdownlist.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxcheckbox.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxcombobox.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxgrid.pager.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxdragdrop.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxcalendar.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxtooltip.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxdatetimeinput.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jquery.global.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jquery.glob.fr-FR.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/jqx/jqxtabs.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/crop/jquery.Jcrop.min.js","js"); Admin.loadjscssfile("' . Config::URL_STATIC . 'js/crop/jquery.color.js","js"); jQuery(document).ready(function () { diplomeData=new Array(); Admin.loadTab(); Admin.loadCrop(); Admin.loadCatGrid(' . json_encode($questions) . '); Admin.loadEventGrid(' . json_encode($events) . '); Admin.loadEmployGrid(' . json_encode($this->model->getemployees()) . '); Admin.loadDate(' . json_encode($this->model->getDate()) . '); jQuery(".jcrop-holder").ready(function () { Admin.loadDiplome(' . $positions . '); }); jQuery("#adminIsepdorTab").removeClass("hidden"); }); '); /* Code qui met à jour le questionnaire pour les ISEP D'or * */ if (isset($_POST['categories'])) { $id = array(); $post = json_decode($_POST['categories'], true); for ($i = 0; $i < count($post); $i++) { if (is_numeric($post[$i]['id'])) { array_push($id, $post[$i]['id']); } } $toDelete = $this->model->checkIsepdorQuestions($id); if (count($toDelete) > 0) { for ($i = 0; $i < count($toDelete); $i++) { $this->model->deleteQuestions($toDelete[$i]); } } for ($i = 0; $i < count($post); $i++) { if ($post[$i]['extra'] == "") { $post[$i]['extra'] = NULL; } if ($post[$i]['id'] != "") { $this->model->updateisepdor($post[$i]['type'], $post[$i]['extra'], $post[$i]['questions'], $post[$i]['id'], $post[$i]['position']); } elseif ($post[$i]['id'] == "") { $this->model->insertisepdor($post[$i]['type'], $post[$i]['extra'], $post[$i]['questions'], $post[$i]['position']); } } } /*Code qui met à jour la table isepdor_employees * */ if (isset($_POST['employees'])) { $id = array(); $post = json_decode($_POST['employees'], true); for ($i = 0; $i < count($post); $i++) { if (is_numeric($post[$i]['id'])) { array_push($id, $post[$i]['id']); } } $toDelete = $this->model->checkIsepdorEmployees($id); if (count($toDelete) > 0) { for ($i = 0; $i < count($toDelete); $i++) { $this->model->deleteEmployees($toDelete[$i]); } } for ($i = 0; $i < count($post); $i++) { $username = $this->makeusername($post[$i]['lastname'], $post[$i]['firstname']); if ($post[$i]['id'] != "") { $this->model->updateEmployees($post[$i]['lastname'], $post[$i]['firstname'], $post[$i]['id'], $username); } elseif ($post[$i]['id'] == "") { $this->model->insertemployees($post[$i]['lastname'], $post[$i]['firstname'], $username); } } } /*Code qui met à jour la table isepdor_event * */ if (isset($_POST['events'])) { $id = array(); $post = json_decode($_POST['events'], true); for ($i = 0; $i < count($post); $i++) { if (is_numeric($post[$i]['id'])) { array_push($id, $post[$i]['id']); } } $toDelete = $this->model->checkIsepdorEvents($id); if (count($toDelete) > 0) { for ($i = 0; $i < count($toDelete); $i++) { $this->model->deleteEvents($toDelete[$i]); } } for ($i = 0; $i < count($post); $i++) { if ($post[$i]['extra'] == 1) { $post[$i]['extra'] = "soiree"; } else { $post[$i]['extra'] = NULL; } if ($post[$i]['id'] != "") { $this->model->updateEvent($post[$i]['name'], $post[$i]['id'], $post[$i]['extra']); } elseif ($post[$i]['id'] == "") { $this->model->insertEvent($post[$i]['name'], $post[$i]['extra']); } } } /*Code qui met à jour les date de vote des isep d'or * */ if (isset($_POST['dates'])) { $post = json_decode($_POST['dates'], true); $this->model->insertDate($post[0][0], $post[0][1], $post[1][0], $post[1][1], $post[2][0], $post[2][1]); } /* * Change l'image diplome */ if (isset($_FILES['diplome']) && !is_array($_FILES['diplome']['name'])) { if ($_FILES['diplome']['size'] > Config::UPLOAD_MAX_SIZE_PHOTO) { throw new FormException('size'); } if ($avatarpath = File::upload('diplome')) { $uploaded_files[] = $avatarpath; try { $img = new Image(); $img->load($avatarpath); $type = $img->getType(); if ($type == IMAGETYPE_JPEG) { $ext = 'jpg'; } else { if ($type == IMAGETYPE_GIF) { $ext = 'gif'; } else { if ($type == IMAGETYPE_PNG) { $ext = 'png'; } else { throw new Exception(); } } } if ($img->getHeight() != 794 || $img->getWidth() != 1122) { throw new FormException('width'); } $img->setType($type); $img->save($avatarpath); unset($img); if (isset($avatarpath) && File::exists($avatarpath)) { $avatar_path = DATA_DIR . Config::DIR_DATA_STORAGE . Config::DIR_DATA_ADMIN . "diplomeIsepDOr9652.png"; $avatar_dir = File::getPath($avatar_path) . "/diplomeIsepDOr9652.png"; File::rename($avatarpath, $avatar_dir); } } catch (FormException $e) { $this->set('form_error', $e->getError()); } foreach ($uploaded_files as $uploaded_file) { File::delete($uploaded_file); } } Post_Model::clearCache(); } /* * Enregistre les coordonnées */ if (isset($_POST['diplomeData'])) { $post = $_POST['diplomeData']; $file = fopen($myFile, 'w'); fwrite($file, $post); fclose($file); } /* * Envoie les diplomes */ if (isset($_GET['getDiplome'])) { $template = DATA_DIR . Config::DIR_DATA_STORAGE . Config::DIR_DATA_ADMIN . "diplomeIsepDOr9652.png"; $font = DATA_DIR . Config::DIR_DATA_STORAGE . Config::DIR_DATA_ADMIN . "font2354.ttf"; $files = array(); $positions = json_decode($positions, true); //récupere les coordonnées précédament demandées for ($i = 0; $i < count($positions); $i++) { $coord[$positions[$i]['index']] = $positions[$i]; } $questions = IsepOr_Model::fetchQuestions(); foreach ($questions as $value) { if (strpos($value['type'], ',')) { $data = array(); foreach (explode(',', $value['type']) as $type) { $data = IsepOr_Controller::__array_rePad($data, IsepOr_Model::fetchFinals($value['id'], $type, 2)); } $finalList[$value['id']] = array_slice(IsepOr_Controller::__array_orderby($data, 'cmpt', SORT_DESC), 0, 3); } else { $finalList[$value['id']] = IsepOr_Model::fetchFinals($value['id'], $value['type'], 2); } } for ($i = 0; $i < count($questions); $i++) { for ($j = 0; $j < count($finalList[$questions[$i]['id']]); $j++) { File::copy($template, DATA_DIR . Config::DIR_DATA_TMP . "diplome" . $i . $j . ".png"); array_push($files, DATA_DIR . Config::DIR_DATA_TMP . "diplome" . $i . $j . ".png"); $im = ImageCreateFromPng(DATA_DIR . Config::DIR_DATA_TMP . "diplome" . $i . $j . ".png"); // Path Images $color = ImageColorAllocate($im, 0, 0, 0); // Text Color $champs[0] = $questions[$i]['questions']; $champs[1] = $finalList[$questions[$i]['id']][$j]["name"]; $champs[2] = ""; if (!is_numeric($finalList[$questions[$i]['id']][$j]["valid"])) { $champs[2] = $this->model->getBirthDay($finalList[$questions[$i]['id']][$j]["valid"]); } for ($a = 0; $a < 3; $a++) { $pxX = round($coord[$a]['x1']); // X $pxY = round($coord[$a]['y2']); // Y ImagettfText($im, round($coord[$a]['h']), 0, $pxX, $pxY, $color, $font, $champs[$a]); } imagePng($im, DATA_DIR . Config::DIR_DATA_TMP . "diplome" . $i . $j . ".png", 9); ImageDestroy($im); if ($finalList[$questions[$i]['id']][$j]['cmpt'] != $finalList[$questions[$i]['id']][$j + 1]['cmpt']) { break; } } } if (self::create_zip($files, DATA_DIR . Config::DIR_DATA_TMP . "diplomesIsepDor.zip", true)) { foreach ($files as $file) { File::delete($file); } header($_SERVER["SERVER_PROTOCOL"] . " 200 OK"); header("Cache-Control: public"); // needed for i.e. header("Content-Type: application/zip"); header("Content-Transfer-Encoding: Binary"); header("Content-Length:" . filesize(DATA_DIR . Config::DIR_DATA_TMP . "diplomesIsepDor.zip")); header("Content-Disposition: attachment; filename=diplomesIsepDor.zip"); readfile(DATA_DIR . Config::DIR_DATA_TMP . "diplomesIsepDor.zip"); File::delete(DATA_DIR . Config::DIR_DATA_TMP . "diplomesIsepDor.zip"); die; } foreach ($files as $file) { File::delete($file); } } /*Code qui export les résultats des isep d'or * */ if (isset($_GET['export'])) { $db = $this->model->getResult(); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: filename=' . 'Résultats_Isepdor' . '.xls'); header('Pragma: no-cache'); header('Expires: 0'); print '<table border=1 > <!-- impression des titres de colonnes --> <TR> <TD bgcolor="#3366CC">Tour</TD> <TD bgcolor="#3366CC">Nom du votant</TD> <TD bgcolor="#3366CC">Catégorie</TD> <TD bgcolor="#3366CC">Réponse(student)</TD> <TD bgcolor="#3366CC">Réponse(admin)</TD> <TD bgcolor="#3366CC">Réponse(assoce)</TD> <TD bgcolor="#3366CC">Réponse(event)</TD> </TR> '; foreach ($db as $champs) { print '<TR>'; print '<TD>' . $champs['round'] . '</TD>'; print '<TD>' . $champs['username'] . '</TD>'; print '<TD>' . utf8_decode($champs['questions']) . '</TD>'; print '<TD>' . $champs['student_username'] . '</TD>'; print '<TD>' . utf8_decode($champs['admin']) . '</TD>'; print '<TD>' . utf8_decode($champs['assoce']) . '</TD>'; print '<TD>' . utf8_decode($champs['name']) . '</TD>'; print '</TR>'; } print '</table>'; exit; } /* * Ajout de la police */ if (isset($_FILES['font']) && $_FILES['font']['name'] != null) { if ($_FILES['font']['size'] > Config::UPLOAD_MAX_SIZE_FILE) { throw new Exception(__('POST_ADD_ERROR_FILE_SIZE', array('size' => File::humanReadableSize(Config::UPLOAD_MAX_SIZE_FILE)))); } if ($filepaths = File::upload('font')) { if (!preg_match('#\\.ttf$#i', $filepaths)) { throw new Exception(__('POST_ADD_ERROR_FILE_FORMAT')); } $avatar_path = DATA_DIR . Config::DIR_DATA_STORAGE . Config::DIR_DATA_ADMIN . "font2354.ttf"; $avatar_dir = File::getPath($avatar_path) . "/font2354.ttf"; File::rename($filepaths, $avatar_dir); } else { throw new Exception(__('ADMIN_UPLOAD_ERROR')); } } /*Code qui met supprime les champs de la table résultat des isep d'or * */ if (isset($_GET['delete_result'])) { $this->model->deleteresult(); header("Location: " . Config::URL_ROOT . Routes::getPage('admin', array("nav" => "isepdor"))); } }
/** * Delete a group * * @param int $id Id of the group */ public function delete($id) { $this->createQuery()->delete($id); self::clearCache(); Post_Model::clearCache(); // Delete the avatar File::delete(self::getAvatarPath($id, true)); File::delete(self::getAvatarPath($id, false)); // Delete from the search index $search_model = new Search_Model(); $search_model->delete('group', $id); }