summaryrefslogtreecommitdiff
path: root/public/app/controllers/admin
diff options
context:
space:
mode:
authorGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-22 13:15:14 +0300
committerGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-04-22 13:15:14 +0300
commit74f1c04022328280b996dde0b7089e13d96acf70 (patch)
tree5488a13587f6a86aa631ecbbb370efb143a9610f /public/app/controllers/admin
parenta647a6180e6f2b0a3c3aaddc40bc2dcf5f396429 (diff)
downloadclassroom-74f1c04022328280b996dde0b7089e13d96acf70.tar.gz
classroom-74f1c04022328280b996dde0b7089e13d96acf70.tar.bz2
classroom-74f1c04022328280b996dde0b7089e13d96acf70.zip
edit lesson: media handling
Diffstat (limited to 'public/app/controllers/admin')
-rw-r--r--public/app/controllers/admin/Course_admin.php136
1 files changed, 136 insertions, 0 deletions
diff --git a/public/app/controllers/admin/Course_admin.php b/public/app/controllers/admin/Course_admin.php
index 0e34e00..ef9ef0e 100644
--- a/public/app/controllers/admin/Course_admin.php
+++ b/public/app/controllers/admin/Course_admin.php
@@ -9,6 +9,13 @@ use app\models\cms\Course_model;
class Course_admin {
+ ## -------------------------------------------------------------------------
+ ##
+ ## COURSE (category) METHODS
+ ##
+ ## -------------------------------------------------------------------------
+
+
/** breadcrumbs
* ---
* responds to ajax GET: /admin/api/categories
@@ -90,4 +97,133 @@ class Course_admin {
];
}
+
+
+
+ ## -------------------------------------------------------------------------
+ ##
+ ## FILE METHODS
+ ##
+ ## -------------------------------------------------------------------------
+
+
+ /** upload_file
+ * upload the file to the file system
+ *
+ * the method reads the POST and FILES array
+ * to retrieve all needed parametres
+ *
+ * + $_FILES[file]
+ * + $_POST[folder] : lesson's ID
+ * + $_POST[reference] : reference type
+ */
+ public static function upload_file()
+ {
+ $post = Registry::get('REQUEST')->POST;
+ $files = Registry::get('REQUEST')->FILES;
+
+
+ // Checks before uploading the file
+ ////////////////////////////////////////////////////////////////////////
+
+ // ** 1: file is upladed to temporary folder ---------------------------
+ if (! is_uploaded_file($files['file']['tmp_name'])) {
+ return ['success' => false]; // bye!
+ }
+
+ // ** 2: File belongs to the allowed MIME types ------------------------
+ $allowed_file_types = array(
+ 'application/pdf',
+ 'image/png', 'image/jpeg',
+ 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
+ );
+ // Recomended MIME type checking via mime_content_type():
+ $mime_type = mime_content_type($files['file']['tmp_name']);
+ if (! in_array($mime_type, $allowed_file_types)) { // File type NOT allowed ...
+ return ['success' => false]; // bye!
+ }
+
+ $file_name = $files['file']['name'];
+ $file_type = $files['file']['type']; // do not take it for granted
+ $file_size = $files['file']['size'];
+ $file_tmp = $files['file']['tmp_name'];
+
+ $bare_name = pathinfo($file_name, PATHINFO_FILENAME);
+ $file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
+
+
+ // ** 3: filename or size checks may be added --------------------------
+ if ($file_name == "") {
+ return ['success' => false]; // bye!
+ }
+
+
+ // READY to finaly save/upload the file to CDN /////////////////////////
+
+ $folder = MEDIA_STORAGE_ROOT . $post['folder'];
+ if (!file_exists($folder)) { // create folder if not exists
+ mkdir($folder, 0757, true);
+ }
+
+ // print_r([
+ // 'dir' => $folder,
+ // 'file' => $bare_name,
+ // 'ext' => $file_ext,
+ // 'type' => $file_type
+ // ]); die();
+
+ $relative_filename = $post['folder']
+ .'/'
+ . strtolower(self::clear_file_name($bare_name) .'.'. $file_ext);
+ $store_filename = MEDIA_STORAGE_ROOT . $relative_filename;
+
+ if (move_uploaded_file($files["file"]["tmp_name"], $store_filename)) {
+ return [
+ 'success' => true,
+ 'path' => $store_filename,
+ 'type' => $mime_type
+ ];
+
+ } else { return ['success' => false]; }
+
+ }
+
+
+ /** clear_file_name
+ * replace greek characters and strip symbols
+ */
+ private static function clear_file_name($str)
+ {
+ $el = mb_split( "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωάέήίόύώϊϋς ", "");
+ $en = str_split("ABGDEZHUIKLMNJOPRSTYFXCVabgdezhuiklmnjoprstyfxcvaehioyviys-");
+ $strip = str_split("!@#$%^&*()+~`[]{};'/<>?=\"");
+
+ return str_replace($strip, '', str_replace($el, $en, $str));
+ }
+
+
+ /** define_media
+ *
+ * create a record in media table
+ *
+ * @param $data
+ * @return id (int): id of created media record
+ */
+ public static function define_media($data)
+ {
+ $sql = "INSERT INTO wiki_media (`title`, `type`, `path`)
+ VALUES (:title, :mimetype, :filepath)";
+ $stmt = $this->pdo->prepare($sql);
+ $stmt->execute([
+ 'title' => $data['title'],
+ 'mimetype' => $data['type'],
+ 'filepath' => $data['path']
+ ]);
+ $inserted_id = $this->pdo->lastInsertId();
+
+ return $inserted_id;
+ }
+
+
} \ No newline at end of file