Commit b400ff09 authored by ZeMKI's avatar ZeMKI
Browse files

Update StudyController.php

fixed add token on update issue
parent c3ef907d
......@@ -85,7 +85,7 @@ class StudyController extends Controller
// extract method savetokens
foreach ($request->get('sorting')['tokens'] as $t) {
if((isset($t['ispreset']) && $t['ispreset']) || strpos($t['image_path'], 'presets') !== false){
if((isset($t['ispreset']) && $t['ispreset']) || (isset($t['image_path']) && strpos($t['image_path'], 'presets') !== false)){
// save only preset path
$token = new Token;
......@@ -95,12 +95,15 @@ class StudyController extends Controller
$token->save();
$token->studies()->sync($newstudy->id);
}else if (array_key_exists('base64',$t)) {
}else {
if(isset( $t['base64'])) $image = $t['base64'];
else $image = config('utilities.base64logo');
//extract method save and encrypt files
$image = $t['base64'];
$extension = $this->extension($image);
$name = $t['name'];
$arr = explode(",", $t['base64'], 2);
$arr = explode(",", $image , 2);
$base64firstpart = $arr[0];
$studypath = storage_path('app/study'.$newstudy->id.'/tokens/');
......@@ -297,20 +300,45 @@ class StudyController extends Controller
$study->sortings()->sync($request->get('sorting')['id']);
// @todo remove all tokens before updating the new ones to remove what was remove
$key = 'id';
$rtokens = $request->get('sorting')['tokens'];
$output = array_map(function($item) use ($key) {
if(isset($item[$key]))return $item[$key];
}, $rtokens);
$available_tokens = $study->available_tokens()->pluck('tokens.id')->toArray();
$result=array_diff($available_tokens,$output);
foreach ($result as $t){
$to = Token::where('id', '=', $t)->first();
File::delete($to->image_path);
$study->available_tokens()->detach($to);
$to->delete();
}
foreach ($request->get('sorting')['tokens'] as $t) {
if((isset($t['ispreset']) && $t['ispreset']) || strpos($t['image_path'], 'presets') !== false){
// save only preset path
$token = Token::where('id', '=', $t['id'])->first();
if((isset($t['ispreset']) && $t['ispreset']) || (isset($t['image_path']) && strpos($t['image_path'], 'presets') !== false)){
$token->name = $t['name'];
$token->image_path = $t['file']['dirname'];
$token->author = Auth::user()->id;
$token->save();
// save only preset path
$token = new Token;
$token->name = $t['name'];
$token->image_path = $t['file']['dirname'];
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($study->id);
$token->studies()->sync($study->id);
}else{
$studypath = storage_path('app/study'.$study->id.'/tokens/');
/// HANDLE THE UPDATE TOKENS
if(isset($t['id']) && $t['id'] != ""){
/// HANDLE THE UPDATE TOKENS with existing token
$token = Token::where('id', '=', $t['id'])->first();
$token->author = Auth::user()->id;
$token->name = $t['name'];
......@@ -328,8 +356,35 @@ class StudyController extends Controller
$arr = explode(",", $t['base64'], 2);
$base64firstpart = $arr[0];
// open file a image resource
\Image::make($image)->fit(100, 100)->save($studypath.$name);
$path = $studypath.$name;
$ext = pathinfo($path, PATHINFO_EXTENSION);
$encryptedContent = encrypt($base64firstpart.",".base64_encode(file_get_contents($path)));
$encryptedName = 'study'.$study->id.'/tokens/'.$name.'.mtoken';
// Store the encrypted Content
Storage::put($encryptedName, $encryptedContent);
File::delete($path);
$token->image_path = $encryptedName;
}
}else{
// create new token
if(isset( $t['base64'])) $image = $t['base64'];
else $image = config('utilities.base64logo');
//extract method save and encrypt files
$extension = $this->extension($image);
$name = $t['name'];
$arr = explode(",", $image , 2);
$base64firstpart = $arr[0];
$studypath = storage_path('app/study'.$study->id.'/tokens/');
File::isDirectory($studypath) or File::makeDirectory($studypath, 0775, true, true);
// open file a image resource
\Image::make($image)->fit(100, 100)->save($studypath.$name);
$path = $studypath.$name;
......@@ -341,7 +396,14 @@ class StudyController extends Controller
Storage::put($encryptedName, $encryptedContent);
File::delete($path);
$token = new Token;
$token->name = $t['name'];
$token->image_path = $encryptedName;
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($study->id);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment