ItemController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Application;
  4. use App\Item;
  5. use App\Setting;
  6. use App\User;
  7. use GrahamCampbell\GitHub\Facades\GitHub;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Storage;
  10. use App\SupportedApps;
  11. class ItemController extends Controller
  12. {
  13. public function __construct()
  14. {
  15. $this->middleware('allowed');
  16. }
  17. /**
  18. * Display a listing of the resource on the dashboard.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function dash()
  23. {
  24. $data['apps'] = Item::doesntHave('parents')->pinned()->orderBy('order', 'asc')->get();
  25. $data['all_apps'] = Item::doesntHave('parents')->get();
  26. return view('welcome', $data);
  27. }
  28. /**
  29. * Set order on the dashboard.
  30. *
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function setOrder(Request $request)
  34. {
  35. $order = array_filter($request->input('order'));
  36. foreach($order as $o => $id) {
  37. $item = Item::find($id);
  38. $item->order = $o;
  39. $item->save();
  40. }
  41. }
  42. /**
  43. * Pin item on the dashboard.
  44. *
  45. * @return \Illuminate\Http\Response
  46. */
  47. public function pin($id)
  48. {
  49. $item = Item::findOrFail($id);
  50. $item->pinned = true;
  51. $item->save();
  52. $route = route('dash', [], false);
  53. return redirect($route);
  54. }
  55. /**
  56. * Unpin item on the dashboard.
  57. *
  58. * @return \Illuminate\Http\Response
  59. */
  60. public function unpin($id)
  61. {
  62. $item = Item::findOrFail($id);
  63. $item->pinned = false;
  64. $item->save();
  65. $route = route('dash', [], false);
  66. return redirect($route);
  67. }
  68. /**
  69. * Unpin item on the dashboard.
  70. *
  71. * @return \Illuminate\Http\Response
  72. */
  73. public function pinToggle($id, $ajax=false)
  74. {
  75. $item = Item::findOrFail($id);
  76. $new = ((bool)$item->pinned === true) ? false : true;
  77. $item->pinned = $new;
  78. $item->save();
  79. if($ajax) {
  80. $data['apps'] = Item::pinned()->get();
  81. $data['ajax'] = true;
  82. return view('sortable', $data);
  83. } else {
  84. $route = route('dash', [], false);
  85. return redirect($route);
  86. }
  87. }
  88. /**
  89. * Display a listing of the resource.
  90. *
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function index(Request $request)
  94. {
  95. $trash = (bool)$request->input('trash');
  96. $data['apps'] = Item::ofType('item')->orderBy('title', 'asc')->get();
  97. $data['trash'] = Item::ofType('item')->onlyTrashed()->get();
  98. if($trash) {
  99. return view('items.trash', $data);
  100. } else {
  101. return view('items.list', $data);
  102. }
  103. }
  104. /**
  105. * Show the form for creating a new resource.
  106. *
  107. * @return \Illuminate\Http\Response
  108. */
  109. public function create()
  110. {
  111. //
  112. $data['tags'] = Item::ofType('tag')->orderBy('title', 'asc')->pluck('title', 'id');
  113. $data['current_tags'] = [];
  114. return view('items.create', $data);
  115. }
  116. /**
  117. * Store a newly created resource in storage.
  118. *
  119. * @param \Illuminate\Http\Request $request
  120. * @return \Illuminate\Http\Response
  121. */
  122. public function store(Request $request)
  123. {
  124. //
  125. $validatedData = $request->validate([
  126. 'title' => 'required|max:255',
  127. 'url' => 'required|url',
  128. ]);
  129. if($request->hasFile('file')) {
  130. $path = $request->file('file')->store('icons');
  131. $request->merge([
  132. 'icon' => $path
  133. ]);
  134. }
  135. $config = Item::checkConfig($request->input('config'));
  136. $current_user = User::currentUser();
  137. $request->merge([
  138. 'description' => $config,
  139. 'user_id' => $current_user->id
  140. ]);
  141. //die(print_r($request->input('config')));
  142. $item = Item::create($request->all());
  143. $item->parents()->sync($request->tags);
  144. $route = route('dash', [], false);
  145. return redirect($route)
  146. ->with('success', __('app.alert.success.item_created'));
  147. }
  148. /**
  149. * Display the specified resource.
  150. *
  151. * @param int $id
  152. * @return \Illuminate\Http\Response
  153. */
  154. public function show($id)
  155. {
  156. //
  157. }
  158. /**
  159. * Show the form for editing the specified resource.
  160. *
  161. * @param int $id
  162. * @return \Illuminate\Http\Response
  163. */
  164. public function edit($id)
  165. {
  166. // Get the item
  167. $data['item'] = Item::find($id);
  168. $data['tags'] = Item::ofType('tag')->orderBy('title', 'asc')->pluck('title', 'id');
  169. $data['current_tags'] = $data['item']->parents;
  170. // show the edit form and pass the nerd
  171. return view('items.edit', $data);
  172. }
  173. /**
  174. * Update the specified resource in storage.
  175. *
  176. * @param \Illuminate\Http\Request $request
  177. * @param int $id
  178. * @return \Illuminate\Http\Response
  179. */
  180. public function update(Request $request, $id)
  181. {
  182. $validatedData = $request->validate([
  183. 'title' => 'required|max:255',
  184. 'url' => 'required|url',
  185. ]);
  186. //die(print_r($request->all()));
  187. if($request->hasFile('file')) {
  188. $path = $request->file('file')->store('icons');
  189. $request->merge([
  190. 'icon' => $path
  191. ]);
  192. }
  193. $config = Item::checkConfig($request->input('config'));
  194. $current_user = User::currentUser();
  195. $request->merge([
  196. 'description' => $config,
  197. 'user_id' => $current_user->id
  198. ]);
  199. $item = Item::find($id);
  200. $item->update($request->all());
  201. $item->parents()->sync($request->tags);
  202. $route = route('dash', [], false);
  203. return redirect($route)
  204. ->with('success',__('app.alert.success.item_updated'));
  205. }
  206. /**
  207. * Remove the specified resource from storage.
  208. *
  209. * @param int $id
  210. * @return \Illuminate\Http\Response
  211. */
  212. public function destroy(Request $request, $id)
  213. {
  214. //
  215. $force = (bool)$request->input('force');
  216. if($force) {
  217. Item::withTrashed()
  218. ->where('id', $id)
  219. ->forceDelete();
  220. } else {
  221. Item::find($id)->delete();
  222. }
  223. $route = route('items.index', [], false);
  224. return redirect($route)
  225. ->with('success',__('app.alert.success.item_deleted'));
  226. }
  227. /**
  228. * Restore the specified resource from soft deletion.
  229. *
  230. * @param int $id
  231. * @return \Illuminate\Http\Response
  232. */
  233. public function restore($id)
  234. {
  235. //
  236. Item::withTrashed()
  237. ->where('id', $id)
  238. ->restore();
  239. $route = route('items.inded', [], false);
  240. return redirect($route)
  241. ->with('success',__('app.alert.success.item_restored'));
  242. }
  243. /**
  244. * Return details for supported apps
  245. *
  246. * @return Json
  247. */
  248. public function appload(Request $request)
  249. {
  250. $output = [];
  251. $appname = $request->input('app');
  252. //die($appname);
  253. $app_details = Application::where('name', $appname)->firstOrFail();
  254. $appclass = $app_details->class();
  255. $app = new $appclass;
  256. // basic details
  257. $output['icon'] = $app_details->icon();
  258. $output['iconview'] = $app_details->iconView();
  259. $output['colour'] = $app_details->defaultColour();
  260. // live details
  261. if($app instanceof \App\EnhancedApps) {
  262. $output['config'] = $app_details->name.'.config';
  263. } else {
  264. $output['config'] = null;
  265. }
  266. return json_encode($output);
  267. }
  268. public function testConfig(Request $request)
  269. {
  270. $data = $request->input('data');
  271. //$url = $data[array_search('url', array_column($data, 'name'))]['value'];
  272. $app = $data['type'];
  273. $app_details = new $app();
  274. $app_details->config = (object)$data;
  275. $app_details->testConfig();
  276. }
  277. public function getStats($id)
  278. {
  279. $item = Item::find($id);
  280. $config = $item->config();
  281. if(isset($config->type)) {
  282. $application = new $config->type;
  283. $application->config = $config;
  284. echo $application->livestats();
  285. }
  286. }
  287. public function checkAppList()
  288. {
  289. $localapps = Application::all();
  290. $list = json_decode(SupportedApps::getList()->getBody());
  291. foreach($list->apps as $app) {
  292. if(!file_exists(app_path('SupportedApps/'.$app->name))) {
  293. SupportedApps::getFiles($app);
  294. $application = new Application;
  295. SupportedApps::saveApp($app, $application);
  296. } else {
  297. // check if there has been an update for this app
  298. $localapp = $localapps->where('name', $app->name)->first();
  299. if($localapp) {
  300. if($localapp->sha !== $app->sha) {
  301. SupportedApps::getFiles($app);
  302. SupportedApps::saveApp($app, $localapp);
  303. }
  304. } else { // local folder, add to database
  305. $application = new Application;
  306. SupportedApps::saveApp($app, $application);
  307. }
  308. }
  309. }
  310. }
  311. }