public function update(Request $request, $id) { $app = App::find($id); $app->name = Input::get('name'); $app->category_id = Category::where('name', '=', Input::get('class_name'))->pluck('id'); $app->slogan = Input::get('slogan'); $app->introduce = Input::get('introduce'); $app->android = Input::get('android'); $app->ios = Input::get('ios'); $app->download = Input::get('download'); $app->is_check = 1; //图片处理 $tmp_name = $_FILES['logo']['tmp_name']; if ($tmp_name) { $result_pic = $data['logo'] = FileHandler::uploadPic($tmp_name); if ($result_pic['success']) { $app->logo = $result_pic['name']; } else { return Redirect::back()->withInput()->withErrors($result_pic['msg']); } } if ($app->save()) { return Redirect::back(); } else { return Redirect::back()->withInput()->withErrors('保存失败!'); } }
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); }