ProfileController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace App\Http\Controllers\Api\V2;
  3. use App\Models\City;
  4. use App\Models\Country;
  5. use App\Http\Resources\V2\AddressCollection;
  6. use App\Models\Address;
  7. use App\Http\Resources\V2\CitiesCollection;
  8. use App\Http\Resources\V2\CountriesCollection;
  9. use App\Models\Order;
  10. use App\Models\Upload;
  11. use App\Models\User;
  12. use App\Models\Wishlist;
  13. use Illuminate\Http\Request;
  14. use App\Models\Cart;
  15. use Hash;
  16. use Illuminate\Support\Facades\File;
  17. use Storage;
  18. class ProfileController extends Controller
  19. {
  20. public function counters()
  21. {
  22. return response()->json([
  23. 'cart_item_count' => Cart::where('user_id', auth()->user()->id)->count(),
  24. 'wishlist_item_count' => Wishlist::where('user_id', auth()->user()->id)->count(),
  25. 'order_count' => Order::where('user_id', auth()->user()->id)->count(),
  26. ]);
  27. }
  28. public function update(Request $request)
  29. {
  30. $user = User::find(auth()->user()->id);
  31. if(!$user){
  32. return response()->json([
  33. 'result' => false,
  34. 'message' => translate("User not found.")
  35. ]);
  36. }
  37. $user->name = $request->name;
  38. if ($request->password != "") {
  39. $user->password = Hash::make($request->password);
  40. }
  41. $user->save();
  42. return response()->json([
  43. 'result' => true,
  44. 'message' => translate("Profile information updated")
  45. ]);
  46. }
  47. public function update_device_token(Request $request)
  48. {
  49. $user = User::find(auth()->user()->id);
  50. if(!$user){
  51. return response()->json([
  52. 'result' => false,
  53. 'message' => translate("User not found.")
  54. ]);
  55. }
  56. $user->device_token = $request->device_token;
  57. $user->save();
  58. return response()->json([
  59. 'result' => true,
  60. 'message' => translate("device token updated")
  61. ]);
  62. }
  63. public function updateImage(Request $request)
  64. {
  65. $user = User::find(auth()->user()->id);
  66. if(!$user){
  67. return response()->json([
  68. 'result' => false,
  69. 'message' => translate("User not found."),
  70. 'path' => ""
  71. ]);
  72. }
  73. $type = array(
  74. "jpg" => "image",
  75. "jpeg" => "image",
  76. "png" => "image",
  77. "svg" => "image",
  78. "webp" => "image",
  79. "gif" => "image",
  80. );
  81. try {
  82. $image = $request->image;
  83. $request->filename;
  84. $realImage = base64_decode($image);
  85. $dir = public_path('uploads/all');
  86. $full_path = "$dir/$request->filename";
  87. $file_put = file_put_contents($full_path, $realImage); // int or false
  88. if ($file_put == false) {
  89. return response()->json([
  90. 'result' => false,
  91. 'message' => "File uploading error",
  92. 'path' => ""
  93. ]);
  94. }
  95. $upload = new Upload;
  96. $extension = strtolower(File::extension($full_path));
  97. $size = File::size($full_path);
  98. if (!isset($type[$extension])) {
  99. unlink($full_path);
  100. return response()->json([
  101. 'result' => false,
  102. 'message' => "Only image can be uploaded",
  103. 'path' => ""
  104. ]);
  105. }
  106. $upload->file_original_name = null;
  107. $arr = explode('.', File::name($full_path));
  108. for ($i = 0; $i < count($arr) - 1; $i++) {
  109. if ($i == 0) {
  110. $upload->file_original_name .= $arr[$i];
  111. } else {
  112. $upload->file_original_name .= "." . $arr[$i];
  113. }
  114. }
  115. //unlink and upload again with new name
  116. unlink($full_path);
  117. $newFileName = rand(10000000000, 9999999999) . date("YmdHis") . "." . $extension;
  118. $newFullPath = "$dir/$newFileName";
  119. $file_put = file_put_contents($newFullPath, $realImage);
  120. if ($file_put == false) {
  121. return response()->json([
  122. 'result' => false,
  123. 'message' => "Uploading error",
  124. 'path' => ""
  125. ]);
  126. }
  127. $newPath = "uploads/all/$newFileName";
  128. if (env('FILESYSTEM_DRIVER') == 's3') {
  129. Storage::disk('s3')->put($newPath, file_get_contents(base_path('public/') . $newPath));
  130. unlink(base_path('public/') . $newPath);
  131. }
  132. $upload->extension = $extension;
  133. $upload->file_name = $newPath;
  134. $upload->user_id = $user->id;
  135. $upload->type = $type[$upload->extension];
  136. $upload->file_size = $size;
  137. $upload->save();
  138. $user->avatar_original = $upload->id;
  139. $user->save();
  140. return response()->json([
  141. 'result' => true,
  142. 'message' => translate("Image updated"),
  143. 'path' => uploaded_asset($upload->id)
  144. ]);
  145. } catch (\Exception $e) {
  146. return response()->json([
  147. 'result' => false,
  148. 'message' => $e->getMessage(),
  149. 'path' => ""
  150. ]);
  151. }
  152. }
  153. // not user profile image but any other base 64 image through uploader
  154. public function imageUpload(Request $request)
  155. {
  156. $user = User::find(auth()->user()->id);
  157. if(!$user){
  158. return response()->json([
  159. 'result' => false,
  160. 'message' => translate("User not found."),
  161. 'path' => "",
  162. 'upload_id' => 0
  163. ]);
  164. }
  165. $type = array(
  166. "jpg" => "image",
  167. "jpeg" => "image",
  168. "png" => "image",
  169. "svg" => "image",
  170. "webp" => "image",
  171. "gif" => "image",
  172. );
  173. try {
  174. $image = $request->image;
  175. $request->filename;
  176. $realImage = base64_decode($image);
  177. $dir = public_path('uploads/all');
  178. $full_path = "$dir/$request->filename";
  179. $file_put = file_put_contents($full_path, $realImage); // int or false
  180. if ($file_put == false) {
  181. return response()->json([
  182. 'result' => false,
  183. 'message' => "File uploading error",
  184. 'path' => "",
  185. 'upload_id' => 0
  186. ]);
  187. }
  188. $upload = new Upload;
  189. $extension = strtolower(File::extension($full_path));
  190. $size = File::size($full_path);
  191. if (!isset($type[$extension])) {
  192. unlink($full_path);
  193. return response()->json([
  194. 'result' => false,
  195. 'message' => "Only image can be uploaded",
  196. 'path' => "",
  197. 'upload_id' => 0
  198. ]);
  199. }
  200. $upload->file_original_name = null;
  201. $arr = explode('.', File::name($full_path));
  202. for ($i = 0; $i < count($arr) - 1; $i++) {
  203. if ($i == 0) {
  204. $upload->file_original_name .= $arr[$i];
  205. } else {
  206. $upload->file_original_name .= "." . $arr[$i];
  207. }
  208. }
  209. //unlink and upload again with new name
  210. unlink($full_path);
  211. $newFileName = rand(10000000000, 9999999999) . date("YmdHis") . "." . $extension;
  212. $newFullPath = "$dir/$newFileName";
  213. $file_put = file_put_contents($newFullPath, $realImage);
  214. if ($file_put == false) {
  215. return response()->json([
  216. 'result' => false,
  217. 'message' => "Uploading error",
  218. 'path' => "",
  219. 'upload_id' => 0
  220. ]);
  221. }
  222. $newPath = "uploads/all/$newFileName";
  223. if (env('FILESYSTEM_DRIVER') == 's3') {
  224. Storage::disk('s3')->put($newPath, file_get_contents(base_path('public/') . $newPath));
  225. unlink(base_path('public/') . $newPath);
  226. }
  227. $upload->extension = $extension;
  228. $upload->file_name = $newPath;
  229. $upload->user_id = $user->id;
  230. $upload->type = $type[$upload->extension];
  231. $upload->file_size = $size;
  232. $upload->save();
  233. return response()->json([
  234. 'result' => true,
  235. 'message' => translate("Image updated"),
  236. 'path' => uploaded_asset($upload->id),
  237. 'upload_id' => $upload->id
  238. ]);
  239. } catch (\Exception $e) {
  240. return response()->json([
  241. 'result' => false,
  242. 'message' => $e->getMessage(),
  243. 'path' => "",
  244. 'upload_id' => 0
  245. ]);
  246. }
  247. }
  248. public function checkIfPhoneAndEmailAvailable()
  249. {
  250. $phone_available = false;
  251. $email_available = false;
  252. $phone_available_message = translate("User phone number not found");
  253. $email_available_message = translate("User email not found");
  254. $user = User::find(auth()->user()->id);
  255. if ($user->phone != null || $user->phone != "") {
  256. $phone_available = true;
  257. $phone_available_message = translate("User phone number found");
  258. }
  259. if ($user->email != null || $user->email != "") {
  260. $email_available = true;
  261. $email_available_message = translate("User email found");
  262. }
  263. return response()->json(
  264. [
  265. 'phone_available' => $phone_available,
  266. 'email_available' => $email_available,
  267. 'phone_available_message' => $phone_available_message,
  268. 'email_available_message' => $email_available_message,
  269. ]
  270. );
  271. }
  272. }