diff --git a/app/Http/Controllers/API/v1/Nominee/AddController.php b/app/Http/Controllers/API/v1/Nominee/AddController.php index a9805eb..923d0fa 100644 --- a/app/Http/Controllers/API/v1/Nominee/AddController.php +++ b/app/Http/Controllers/API/v1/Nominee/AddController.php @@ -43,12 +43,19 @@ class AddController extends Controller ]); } - public function insertNominee ($request) + public function insertNominee ($request) { - $nominee = $request->all(); - $default_image = config('app.cloudinary_enabled') ? config('app.cloudinary_image_default') : config('app.nominee_image'); - $nominee['image'] = Util::getImagePath($request, config('app.nominee_directory'), $default_image); + $nominee = $request->except(['photo']); $nominee['election_id'] = Util::getCurrentElection(); + + if ($request->hasFile('photo')) { + $imageData = file_get_contents($request->file('photo')); + $nominee['photo'] = $imageData; + } + Nominee::create($nominee); + return response()->json(['message' => 'Photo updated successfully']); + + } } diff --git a/app/Http/Controllers/API/v1/Nominee/UpdateController.php b/app/Http/Controllers/API/v1/Nominee/UpdateController.php index f63c09e..82a0c4e 100644 --- a/app/Http/Controllers/API/v1/Nominee/UpdateController.php +++ b/app/Http/Controllers/API/v1/Nominee/UpdateController.php @@ -41,11 +41,10 @@ class UpdateController extends Controller ]); } - private function updateNominee ($request, $id) + private function updateNominee ($request, $id) { $nominee = Nominee::find($id); - $default_image = $nominee->image; - + $nominee->name = $request->name; $nominee->unit = $request->unit; $nominee->no_anggota = $request->no_anggota; @@ -53,11 +52,13 @@ class UpdateController extends Controller $nominee->partylist_id = $request->partylist_id; $nominee->motto = $request->motto; $nominee->description = $request->description; - $nominee->image = Util::getImagePath($request, config('app.nominee_directory'), $default_image); - $nominee->save(); - if (!empty($request->image)) - Util::deleteImage($default_image); + if ($request->hasFile('photo')) { + $imageData = file_get_contents($request->file('photo')); + $nominee->photo = $imageData; + } + + $nominee->save(); } } diff --git a/app/Nominee.php b/app/Nominee.php index eb89e33..fdc1cd0 100644 --- a/app/Nominee.php +++ b/app/Nominee.php @@ -8,14 +8,24 @@ class Nominee extends Model { protected $table = 'nominee'; protected $fillable = [ - 'name', - 'unit', - 'no_anggota', + 'name', + 'unit', + 'no_anggota', 'position_id', 'partylist_id', 'election_id', - 'image', 'description', - 'motto' + 'motto', + 'photo', ]; + public $timestamp = false; + + public function getPhotoAttribute($value) + { + if ($value) { + return base64_encode($value); + } + + return null; + } } diff --git a/database/migrations/2024_03_23_135849_add_photo_column_to_nominee_table.php b/database/migrations/2024_03_23_135849_add_photo_column_to_nominee_table.php new file mode 100644 index 0000000..6ed0aca --- /dev/null +++ b/database/migrations/2024_03_23_135849_add_photo_column_to_nominee_table.php @@ -0,0 +1,34 @@ +binary('photo')->nullable(); + $table->dropColumn('image'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('nominee', function (Blueprint $table) { + $table->dropColumn('photo'); + $table->string('image'); + }); + } +} diff --git a/resources/assets/js/components/demo/admin/nominee/add.vue b/resources/assets/js/components/demo/admin/nominee/add.vue index d86b311..3e71bc3 100644 --- a/resources/assets/js/components/demo/admin/nominee/add.vue +++ b/resources/assets/js/components/demo/admin/nominee/add.vue @@ -46,9 +46,12 @@ +
+ Preview +
- +
@@ -70,6 +73,7 @@ export default { data: function () { return { + imageUrl: '', loading: false } }, @@ -88,6 +92,8 @@ export default { vm.$router.push({ name: 'Maklumat Calon' }); }, error: function (error) { + alert('Nominee with the same name has already registered'); + location.reload(); $.notifyClose(); vm.loading = false; vm.util.showResult(error, 'error', 'ajax'); @@ -98,30 +104,27 @@ export default { }) }, - uploadImage(event) { + handleImageChange(event) { + const file = event.target.files[0]; // Get the selected file + const imageType = /image.*/; // RegExp to check if the file is an image - const URL = 'http://foobar.com/upload'; + // Check if the selected file is an image + if (file && file.type.match(imageType)) { + const reader = new FileReader(); // Create a FileReader object - let data = new FormData(); - data.append('name', 'my-picture'); - data.append('file', event.target.files[0]); + reader.onload = (e) => { + this.imageUrl = e.target.result; // Set the imageUrl to the data URL of the image + document.getElementById('imagePreview').style.display = 'block'; // Display the div + }; - let config = { - header: { - 'Content-Type': 'image/png' - } - } - - axios.put( - URL, - data, - config - ).then( - response => { - console.log('image upload response > ', response) - } - ) - } + reader.readAsDataURL(file); // Read the image data as a data URL + } else { + // Clear the file input and hide the div if the selected file is not an image + event.target.value = ''; + this.imageUrl = ''; + document.getElementById('imagePreview').style.display = 'none'; + } + } }, computed: { @@ -136,4 +139,19 @@ export default { } } } - \ No newline at end of file + + + diff --git a/resources/assets/js/components/demo/admin/nominee/edit.vue b/resources/assets/js/components/demo/admin/nominee/edit.vue index 101a606..c3fc5cb 100644 --- a/resources/assets/js/components/demo/admin/nominee/edit.vue +++ b/resources/assets/js/components/demo/admin/nominee/edit.vue @@ -10,12 +10,8 @@ @submit.prevent="edit()"> -
- -
-
- +
@@ -23,12 +19,12 @@
- +
- +
@@ -57,10 +53,18 @@
+
+ Preview +
+
+ + +
+
- Back @@ -75,6 +79,7 @@ export default{ data: function () { return { + imageUrl: '', loading: false } }, @@ -84,7 +89,38 @@ export default{ this.$router.push({name:'Maklumat Calon'}); }, + mounted: function () { + if(this.nominee.id) { + this.imageUrl = this.createBase64ImageUrl(this.nominee.photo); + document.getElementById('imagePreview').style.display = 'block'; + } + }, + methods: { + createBase64ImageUrl: function(base64ImageData) { + return "data:image/png;base64," + base64ImageData; + }, + handleImageChange(event) { + const file = event.target.files[0]; // Get the selected file + const imageType = /image.*/; // RegExp to check if the file is an image + + // Check if the selected file is an image + if (file && file.type.match(imageType)) { + const reader = new FileReader(); // Create a FileReader object + + reader.onload = (e) => { + this.imageUrl = e.target.result; // Set the imageUrl to the data URL of the image + document.getElementById('imagePreview').style.display = 'block'; // Display the div + }; + + reader.readAsDataURL(file); // Read the image data as a data URL + } else { + // Clear the file input and hide the div if the selected file is not an image + event.target.value = ''; + this.imageUrl = ''; + document.getElementById('imagePreview').style.display = 'none'; + } + }, edit: function () { if (this.loading) return; var vm = this; @@ -94,7 +130,7 @@ export default{ success: function (response) { $.notifyClose(); vm.loading = false; - if (vm.util.showResult(response, 'success', 'ajax')) + if (vm.util.showResult(response, 'success', 'ajax')) vm.$router.push({name: 'Maklumat Calon'}); }, error: function (error) { @@ -122,4 +158,19 @@ export default{ } } } - \ No newline at end of file + + + diff --git a/resources/assets/js/components/demo/admin/nominee/index.vue b/resources/assets/js/components/demo/admin/nominee/index.vue index c701e14..de8cf95 100644 --- a/resources/assets/js/components/demo/admin/nominee/index.vue +++ b/resources/assets/js/components/demo/admin/nominee/index.vue @@ -2,20 +2,20 @@
    - All Position - @@ -43,7 +43,7 @@ - + {{ nominee.name}} {{ nominee.no_anggota}} @@ -142,7 +142,11 @@ export default{ if (partylists[i].id == id) return partylists[i]['name']; return 'No partylists'; - } + }, + + createBase64ImageUrl: function(base64ImageData) { + return "data:image/png;base64," + base64ImageData; + } }, computed: { @@ -165,4 +169,4 @@ export default{ } } } - \ No newline at end of file + diff --git a/resources/assets/js/components/demo/voter/home/index.vue b/resources/assets/js/components/demo/voter/home/index.vue index c403564..be90a11 100644 --- a/resources/assets/js/components/demo/voter/home/index.vue +++ b/resources/assets/js/components/demo/voter/home/index.vue @@ -51,10 +51,10 @@ - {{ nominee.name }} @@ -124,7 +124,7 @@ export default{ getNominee: function (id) { let nominees = this.data.nominees; - for (var i in nominees) + for (var i in nominees) if (nominees[i]['id']==id) return nominees[i].name; return ''; }, @@ -132,7 +132,12 @@ export default{ view: function (index) { this.x = this.data.nominees[index]; this.util.showModal('#nominee-information-modal'); - } + }, + + createBase64ImageUrl: function(base64ImageData) { + return "data:image/png;base64," + base64ImageData; + } + }, computed: {