Exemplo n.º 1
0
 public static function handler()
 {
     $data = array('success' => false, 'title' => trans('t.contactErrorTitle'), 'message' => trans('t.contactErrorMessage'));
     $requestAllowed = true;
     if (self::$requestType == 'ajax') {
         $requestAllowed = \Request::ajax() && \Request::get('getIgnore_isAjax');
     }
     if ($requestAllowed) {
         $validator = \Validator::make(\Request::all(), self::$rules);
         if ($validator->fails()) {
             $messages = $validator->getMessageBag()->toArray();
             $finalMessages = array();
             foreach ($messages as $field => $fieldMessages) {
                 foreach ($fieldMessages as $fieldMessage) {
                     $finalMessages[] = $fieldMessage;
                 }
             }
             $message = implode("\n", $finalMessages);
             $data['message'] = $message;
             return self::returnData($data);
         } else {
             try {
                 Mailer::sendMessage(\Request::all());
                 $data['success'] = true;
                 $data['title'] = trans('t.contactSuccessTitle');
                 $data['message'] = trans('t.contactSuccessMessage');
             } catch (\Exception $e) {
                 Dbar::error("Error while sending message: " . $e->getMessage());
             }
         }
         return self::returnData($data);
     }
     \App::abort(404);
 }
Exemplo n.º 2
0
 public static function handleFile($entity, $fileAction, $fileNameField, $id)
 {
     $data = array('success' => false, 'message' => trans('gtcms.errorHasOccurred') . ". " . trans('gtcms.pleaseTryAgain') . ".");
     try {
         /** @var \App\BaseModel $entity */
         $modelConfig = AdminHelper::modelExists($entity);
         /** @var \App\BaseModel $fullEntity */
         $fullEntity = $modelConfig->myFullEntityName();
         /** @var \App\BaseModel $object */
         // "new" when adding an image/file, "new_gtcms_entry" when deleting it, before the object is saved
         if ($id == "new" || $id == "new_gtcms_entry") {
             $object = new $fullEntity();
         } else {
             $object = $fullEntity::find($id);
         }
         $field = AdminHelper::getFieldsByParam($modelConfig, 'property', $fileNameField, true);
         if (\Request::ajax() && $modelConfig && $object && $field) {
             if (in_array($fileAction, array('uploadFile', 'uploadImage'))) {
                 $fieldRules = $field->rules ? array($field->property => ModelConfig::rulesToArray($field->rules)) : array();
                 $validator = \Validator::make(\Request::all(), $fieldRules);
                 if ($validator->fails()) {
                     $messages = $validator->getMessageBag()->getMessages();
                     $message = $messages[$fileNameField][0];
                     $data['message'] = $message;
                 } else {
                     $fileData = false;
                     $action = 'add';
                     if ($object->id) {
                         $action = 'edit';
                     }
                     $input = array();
                     $parentProperty = AdminHelper::standaloneCheck($modelConfig, $action, $input, $object);
                     if ($fileAction == 'uploadFile' && ($fileFields = AdminHelper::modelConfigHasFile($modelConfig))) {
                         $fileData = FileHandler::process($modelConfig, $fileFields, $parentProperty);
                     } else {
                         if ($fileAction == 'uploadImage' && ($imageFields = AdminHelper::modelConfigHasImage($modelConfig))) {
                             $fileData = ImageHandler::process($modelConfig, $imageFields, $parentProperty);
                         }
                     }
                     if (!empty($fileData[0])) {
                         $object->{$fileNameField} = $fileData[0]['filename'];
                         $method = false;
                         if ($field->displayProperty && $field->displayProperty->method) {
                             $method = $field->displayProperty->method;
                         }
                         if ($fileAction == 'uploadImage') {
                             $method = $method ? $method : "image";
                             $fileUrl = $object->{$method}('url', $fileData[0]['returnFolder']);
                             $fileOriginalUrl = $object->{$method}('url', 'original');
                         } else {
                             $method = $method ? $method : "file";
                             $fileUrl = $fileOriginalUrl = $object->{$method}('url', $fileNameField);
                         }
                         $data = array('success' => true, 'message' => false, 'fileUrl' => $fileUrl, 'fileOriginalUrl' => $fileOriginalUrl, 'filename' => $fileData[0]['filename']);
                     }
                 }
             } else {
                 if ($fileAction == 'deleteFile') {
                     $data['success'] = true;
                     $method = "file";
                     $file = true;
                     if (\Request::get('imageFile')) {
                         $method = "image";
                         $file = false;
                     }
                     $fileNameValue = \Request::get('fileNameValue');
                     if ($file) {
                         $filePath = $object->{$method}('path', $fileNameField, $fileNameValue);
                         if (file_exists($filePath)) {
                             try {
                                 unlink($filePath);
                             } catch (\Exception $e) {
                                 Dbar::error("File couldn't be deleted: " . $e->getMessage());
                             }
                         }
                     } else {
                         $folders = array();
                         $modelImagesPath = public_path("img/modelImages/" . $entity);
                         $scannedFolders = scandir($modelImagesPath);
                         if ($scannedFolders) {
                             foreach ($scannedFolders as $scannedFolder) {
                                 if (!in_array($scannedFolder, array('.', '..'))) {
                                     $folders[] = $scannedFolder;
                                 }
                             }
                         }
                         foreach ($folders as $folder) {
                             $filePath = $object->{$method}('path', $folder, $fileNameField, $fileNameValue);
                             if (file_exists($filePath)) {
                                 try {
                                     unlink($filePath);
                                 } catch (\Exception $e) {
                                     Dbar::error("File couldn't be deleted: " . $e->getMessage());
                                 }
                             }
                         }
                     }
                     if ($object->id) {
                         $object->{$fileNameField} = null;
                         $object->save();
                     }
                 }
             }
         }
     } catch (\Exception $e) {
         $preventException = false;
         if (in_array($e->getCode(), array(ImageHandler::DIM_ERROR, FileHandler::INVALID_FILE_ERROR))) {
             $preventException = true;
         }
         AdminHelper::handleException($e, null, $preventException);
         $data['message'] = $e->getMessage();
         $data['success'] = false;
     }
     return \Response::json($data);
 }