/** * set options * @return [type] return json contain datatable values */ public function get_options() { $opt = $this->model()->options(); debug::property('data', $opt); $this->model()->_processor(object(array("force_json" => true, "force_stop" => true))); // echo(json_encode($result, JSON_FORCE_OBJECT)); // exit(); }
/** * Upload file to project files and database * @return [type] [description] */ function sp_attachment_add() { $FOLDER_SIZE = 1000; // 1. check upload process and validate it $invalid = utility\upload::invalid('upfile'); if ($invalid) { debug::property('status', 'fail'); debug::property('error', $invalid); $this->_processor(['force_json' => true, 'not_redirect' => true]); return false; } // 2. Generate file_id, folder_id and url $qry_count = $this->sql()->table('posts')->where('post_type', 'attachment')->select()->num(); $folder_prefix = "files/"; $folder_id = ceil(($qry_count + 1) / $FOLDER_SIZE); $folder_loc = $folder_prefix . $folder_id; $file_id = $qry_count % $FOLDER_SIZE + 1; $url_full = "{$folder_loc}/{$file_id}-" . utility\upload::$fileFullName; // 3. Check for record exist in db or not $qry_count = $this->sql()->table('posts')->where('post_slug', utility\upload::$fileMd5)->select(); if ($qry_count->num()) { $id = $qry_count->assoc('id'); debug::property('status', 'fail'); $link = '<a target="_blank" href=/cp/attachments/edit=' . $id . '>' . T_('Duplicate - File exist') . '</a>'; debug::property('error', $link); $this->_processor(['force_json' => true, 'not_redirect' => true]); return false; } // 4. transfer file to project folder with new name if (!utility\upload::transfer($url_full, $folder_loc)) { debug::property('status', 'fail'); debug::property('error', T_('Fail on tranfering file')); $this->_processor(['force_json' => true, 'not_redirect' => true]); return false; } $file_ext = utility\upload::$fileExt; $url_thumb = null; $url_normal = null; switch ($file_ext) { case 'jpg': case 'jpeg': case 'png': case 'gif': $extlen = strlen(utility\upload::$fileExt); $url_file = substr($url_full, 0, -$extlen - 1); $url_thumb = $url_file . '-thumb.' . utility\upload::$fileExt; $url_normal = $url_file . '-normal.' . utility\upload::$fileExt; utility\image::load($url_full); utility\image::thumb(600, 400); utility\image::save($url_normal); utility\image::thumb(150, 150); utility\image::save($url_thumb); break; } // 5. get filemeta data $file_meta = ['mime' => utility\upload::$fileMime, 'type' => utility\upload::$fileType, 'size' => utility\upload::$fileSize, 'ext' => $file_ext, 'url' => $url_full, 'thumb' => $url_thumb, 'normal' => $url_normal]; $url_slug = utility\upload::$fileMd5; $url_body = $folder_id . "_" . $file_id; $page_url = self::sp_generateUrl($url_slug, $url_body, $file_meta['type'] . "/"); if (strpos($file_meta['mime'], 'image') !== false) { list($file_meta['width'], $file_meta['height']) = getimagesize($url_full); } $file_meta = json_encode($file_meta, JSON_UNESCAPED_UNICODE); // 6. add uploaded file record to db $qry = $this->sql(); $qry = $qry->table('posts')->set('post_title', utility\upload::$fileName)->set('post_slug', utility\upload::$fileMd5)->set('post_meta', $file_meta)->set('post_type', 'attachment')->set('post_url', $page_url)->set('user_id', $this->login('id'))->set('post_status', 'draft')->set('post_publishdate', date('Y-m-d H:i:s')); $qry = $qry->insert(); $post_new_id = $qry->LAST_INSERT_ID(); // 7. commit all changes or rollback and remove file // ====================================================== // you can manage next event with one of these variables, // commit for successfull and rollback for failed // if query run without error means commit $this->commit(function ($_id) { debug::property('status', 'ok'); $link = '<a target="_blank" href=/cp/attachments/edit=' . $_id . '>' . T_('Edit') . '</a>'; debug::property('edit', $link); }, $post_new_id); // if a query has error or any error occour in any part of codes, run roolback $this->rollback(function () { debug::property('status', 'fail'); debug::property('error', T_('Error')); // remove file if has problem }); $this->_processor(['force_json' => true, 'not_redirect' => true]); }