/** * Create gallery record * * @param string $value Gallery image name * @return \samson\activerecord\gallery Gallery record */ public function &parser($value, $data) { // Try to split value using passed token foreach (explode($this->token, $value) as $photo) { // Rewrite common mistakes, trim photo name $photo = trim(str_replace(array('. png', ' png'), '.png', $photo)); $photo = str_replace(array('. jpg', ' jpg'), '.jpg', $photo); $photo = str_replace(array('. gif', ' gif'), '.gif', $photo); // Build full path to photo file $path = $this->uploadPath . $photo; // Prepare data for case insensitive file search $directoryName = dirname($path); $fileArray = glob($directoryName . '/*', GLOB_NOSORT); $fileNameLowerCase = mb_strtolower($path, 'UTF-8'); $found = false; // Iterate all location files foreach ($fileArray as $file) { // If lowercase variant matches if (mb_strtolower($file, 'UTF-8') == $fileNameLowerCase) { // Normalize photo name //$normalizedPhoto = str_replace(' ', '_', strtolower($photo)); // If scale module is configured if (isset(s()->module_stack['scale'])) { // Resize image m('scale')->resize($file, $photo, $this->uploadPath); } $gallery = new \samson\activerecord\gallery(false); $gallery->Path = $photo; $gallery->Name = $photo; $gallery->Src = $file; $gallery->Loaded = date('Y-m-d h:i:s'); $gallery->MaterialID = $this->material->result->id; $gallery->Active = 1; $gallery->save(); // Rewrite image to normal file name //file_put_contents($this->uploadPath.$normalizedPhoto, file_get_contents($file)); $found = true; break; } } // Signal if file was not found if (!$found) { e('Image file: "##" - not found', D_SAMSON_DEBUG, $path); } } return $gallery; }
function gallery_async_save() { // If we have really received form data if (isset($_POST)) { /*@var \samson\activerecord\gallery $dbItem */ $dbItem = null; $dbItem = new \samson\activerecord\gallery(false); $fileName = $_SESSION['fileName']; $uploadName = $_SESSION['uploadName']; unset($_SESSION['uploadName']); unset($_SESSION['fileName']); unset($_SESSION['filePath']); $imgsize = $_FILES["file"]["size"] / 1024; $src = 'upload/' . $fileName; // Save image name $dbItem->name = filter_var($_POST['name']); // Save image description $dbItem->description = filter_var($_POST['description']); // Store file in upload dir $dbItem->src = $src; $dbItem->imgsize = $imgsize; $dbItem->save(); } return array('status' => '1'); }
/** * Controller for image upload * @param string $materialFieldId Gallery identifier, represented as materialfield id * @return array Async response array */ public function __async_upload($materialFieldId) { $result = array('status' => false); /** @var \samsonphp\upload\Upload $upload Pointer to uploader object */ $upload = null; // Verify extension image if ($this->verifyExtensionFile()) { // Uploading file to server and path current material identifier if (uploadFile($upload, array(), $materialFieldId)) { /** @var \samson\activerecord\materialfield $materialField MaterialField object to identify gallery */ $materialField = null; // /** @var array $children List of related materials */ // $children = null; // Check if participant has not uploaded remix yet if ($this->query->entity(MaterialField::ENTITY)->where('MaterialFieldID', $materialFieldId)->where('Active', 1)->first($materialField)) { // Create empty db record $photo = new \samson\activerecord\gallery(false); $photo->Name = $upload->realName(); $photo->Src = $upload->name(); $photo->Path = $upload->path(); $photo->materialFieldId = $materialField->id; $photo->MaterialID = $materialField->MaterialID; $photo->size = $upload->size(); $photo->Active = 1; $photo->save(); // Call scale if it is loaded if (class_exists('\\samson\\scale\\ScaleController', false)) { /** @var \samson\scale\ScaleController $scale */ $scale = $this->system->module('scale'); $scale->resize($upload->fullPath(), $upload->name(), $upload->uploadDir); } $result['status'] = true; } } } else { $errorText = "Файл ( " . urldecode($_SERVER['HTTP_X_FILE_NAME']) . " ) не является картинкой!"; $result = array('status' => false, 'errorText' => $errorText); } return $result; }