Beispiel #1
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);
 }
 public static function index($loginRedirect)
 {
     $startUrl = "";
     AdminHistoryManager::clearHistory();
     if ($loginRedirect) {
         $defaultModel = AdminController::getDefaultModelForUser();
         $startUrl = "/admin/" . $defaultModel;
         AdminHistoryManager::addHistoryLink($startUrl, $defaultModel);
     } else {
         AdminHistoryManager::addHistoryLink();
     }
     $ajaxRequest = \Request::ajax() && \Request::get('getIgnore_isAjax') ? true : false;
     $getSearchResults = isset($_GET['getIgnore_getSearchResults']) && $ajaxRequest ? true : false;
     $searchIsOpen = isset($_GET['getIgnore_searchIsOpen']) && $ajaxRequest ? true : false;
     /** @var \App\BaseModel $entity */
     $entity = self::$entity;
     /** @var \App\BaseModel $fullEntity */
     $fullEntity = self::$modelConfig->myFullEntityName();
     $indexType = self::$modelConfig->index ? ucfirst(self::$modelConfig->index) : '';
     $searchParams = array();
     $searchDataWithFieldValues = array();
     $ordering = Tools::getSearchAndOrderGets(false, false, false, true);
     if ($indexType == 'Tree') {
         $objects = $fullEntity::where('depth', 0)->orderBy('position', 'asc')->get();
     } else {
         $searchDataWithFieldValues = AdminHelper::getSearchData(self::$modelConfig, true);
         $input = array();
         AdminHelper::standaloneCheck(self::$modelConfig, 'index', $input);
         $orderAndDirection = AdminHelper::getOrderParams(self::$modelConfig);
         $objects = $fullEntity::searchResultsEntities(self::$modelConfig)->where(function ($query) {
             if (self::$modelConfig->name == 'User' && !\Auth::user()->is_superadmin) {
                 $query->where('is_superadmin', 0);
             }
         })->orderBy($orderAndDirection['orderBy'], $orderAndDirection['direction'])->paginate(self::$modelConfig->perPage);
     }
     $addEntity = true;
     if ($indexType == 'Tree' && self::$modelConfig->maxFirstLevelItems && $fullEntity::where('depth', 0)->count() >= self::$modelConfig->maxFirstLevelItems) {
         $addEntity = false;
     }
     $viewData = array('active' => $entity, 'modelConfig' => self::$modelConfig, 'objects' => $objects, 'addEntity' => $addEntity, 'searchParams' => $searchParams, 'searchDataWithFieldValues' => $searchDataWithFieldValues, 'ordering' => $ordering, 'ajaxRequest' => $ajaxRequest, 'loginRedirect' => $loginRedirect, 'getSearchResults' => $getSearchResults, 'searchIsOpen' => $searchIsOpen, 'indexType' => $indexType);
     if ($ajaxRequest) {
         $view = \View::make('gtcms.admin.elements.index' . $indexType . 'Content')->with($viewData);
         if ($loginRedirect) {
             $data = array('success' => true, 'setUrl' => $startUrl, 'view' => $view->render());
             return \Response::json($data);
         }
         $data = array('success' => true, 'view' => $view->render(), 'setHistoryLinks' => false, 'modelConfigName' => self::$modelConfig->name, 'indexView' => true, 'setUrl' => "/admin/" . $entity . Tools::getGets(), 'getParams' => Tools::getGets(), 'entity' => $entity, 'searchDataWithFieldValues' => $searchDataWithFieldValues || $ordering ? true : false);
         return \Response::json($data);
     } else {
         return \View::make('gtcms.admin.elements.index')->with($viewData);
     }
 }