Advertisement
Guest User

Untitled

a guest
Feb 6th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. <?php
  2. /**
  3. * Admin controller
  4. *
  5. * @package Comandeer/Controller
  6. * @author Comandeer
  7. * @copyright (c) 2014 Comandeer
  8. * @license EULA
  9. */
  10.  
  11. namespace Comandeer\Controller;
  12.  
  13. use \Comandeer\Auth, \Comandeer\Model, \Comandeer\Mailer;
  14.  
  15. class Admin extends \Comandeer\Controller
  16. {
  17. /**
  18. * Generates admin's panel homepage
  19. *
  20. * @return void
  21. */
  22. public function get_index()
  23. {
  24. if(!Auth::isAdmin())
  25. $this->app->response->redirect('/', 303);
  26.  
  27. $this->app->render('partials/admin/homepage.twig');
  28. }
  29.  
  30.  
  31. //SETTINGS
  32.  
  33. /**
  34. * Shows form for editing settings
  35. *
  36. * @return void
  37. */
  38. public function get_settings()
  39. {
  40. if(!Auth::isAdmin())
  41. $this->app->response->redirect('/', 303);
  42.  
  43. $this->app->render('/partials/admin/settings.twig');
  44. }
  45.  
  46. /**
  47. * Handles updating settings
  48. *
  49. * @return void
  50. */
  51. public function post_settings()
  52. {
  53. if(!Auth::isAdmin())
  54. $this->app->response->redirect('/', 303);
  55.  
  56. file_put_contents('./config.php', '<?php return ' . var_export(array_merge($this->config, $this->app->request->post()), true) . ';');
  57.  
  58. $this->app->response->redirect('/admin');
  59. }
  60.  
  61.  
  62. //USERS
  63.  
  64. /**
  65. * Show all users
  66. *
  67. * @return void
  68. */
  69. public function get_users()
  70. {
  71. if(!Auth::isAdmin())
  72. $this->app->response->redirect('/', 303);
  73.  
  74. $users = Model::factory('User')->getUsers();
  75.  
  76. $this->app->render('partials/admin/users.twig', [
  77. 'users' => $users
  78. ]);
  79. }
  80.  
  81. /**
  82. * Show specified user
  83. *
  84. * @return void
  85. */
  86. public function get_user($id)
  87. {
  88. if(!Auth::isAdmin())
  89. $this->app->response->redirect('/', 303);
  90.  
  91. $user = Model::factory('User')->getUser($id);
  92.  
  93. $this->app->render('partials/admin/user.twig', [
  94. 'user' => $user
  95. ]);
  96. }
  97.  
  98. /**
  99. * Generates user's edit form
  100. *
  101. * @return void
  102. */
  103. public function get_userEdit($id)
  104. {
  105. if(!Auth::isAdmin())
  106. $this->app->response->redirect('/', 303);
  107.  
  108. $user = Model::factory('User')->getData($id);
  109.  
  110. $this->app->render('/partials/admin/user-edit.twig', [
  111. 'user' => $user
  112. ]);
  113. }
  114.  
  115. /**
  116. * Handles user's edit
  117. *
  118. * @return void
  119. */
  120. public function post_userEdit($id)
  121. {
  122. if(!Auth::isAdmin())
  123. $this->app->response->redirect('/', 303);
  124.  
  125. Model::factory('User')->updateUser($id, $this->app->request->post());
  126.  
  127. $this->app->response->redirect('/admin/uzytkownicy', 303);
  128. }
  129.  
  130. /**
  131. * Generates new password for user and sends it via e-mail
  132. *
  133. * @return void
  134. */
  135. public function get_userPassword($id)
  136. {
  137. if(!Auth::isAdmin())
  138. $this->app->response->redirect('/', 303);
  139.  
  140. $password = substr(uniqid(rand(0, 9999), true), 0, 8);
  141.  
  142. $user = Model::factory('User')->getData($id);
  143.  
  144. Model::factory('User')->updatePassword($id, $password);
  145.  
  146. \Comandeer\Mailer::send([$user['email']], 'Zmiana hasła', $this->view->render('mails/password.twig', [
  147. 'password' => $password
  148. ]), $this->config['admin']);
  149.  
  150. $this->app->response->redirect('/admin/uzytkownicy', 303);
  151. }
  152.  
  153. /**
  154. * Delete user
  155. *
  156. * @return void
  157. */
  158. public function get_userDelete($id)
  159. {
  160. if(!Auth::isAdmin())
  161. $this->app->response->redirect('/', 303);
  162.  
  163. Model::factory('User')->deleteUser($id);
  164.  
  165. $this->app->response->redirect('/admin/uzytkownicy', 303);
  166. }
  167.  
  168. /**
  169. * Activates user
  170. *
  171. * @return void
  172. */
  173. public function get_userActivate($id)
  174. {
  175. if(!Auth::isAdmin())
  176. $this->app->response->redirect('/', 303);
  177. $model = Model::factory('User');
  178.  
  179. $user = $model->getData($id);
  180.  
  181. $model->updatePayment($id);
  182.  
  183. \Comandeer\Mailer::send([$user['email']], 'Link aktywacyjny', $this->view->render('mails/activation.twig', [
  184. 'name' => $user['name']
  185. ,'hash' => $user['hash']
  186. ]), $this->config['admin']);
  187.  
  188. $this->app->response->redirect('/admin/uzytkownicy', 303);
  189. }
  190.  
  191.  
  192. //WIZARD
  193.  
  194. /**
  195. * Generate wizard list
  196. *
  197. * @return void
  198. */
  199. public function get_wizard()
  200. {
  201. if(!Auth::isAdmin())
  202. $this->app->response->redirect('/', 303);
  203.  
  204. $questions = Model::factory('Wizard')->generate();
  205.  
  206. $this->app->render('/partials/admin/wizard.twig', [
  207. 'questions' => $questions
  208. ]);
  209. }
  210.  
  211. /**
  212. * Generate form for adding question to wizard
  213. *
  214. * @return void
  215. */
  216. public function get_wizardAdd()
  217. {
  218. $this->app->render('/partials/admin/wizard-edit.twig');
  219. }
  220.  
  221. /**
  222. * Handles adding questions to wizard
  223. *
  224. * @return void
  225. */
  226. public function post_wizardAdd()
  227. {
  228.  
  229. Model::factory('Wizard')->addQuestion($this->app->request->post());
  230.  
  231. $this->app->response->redirect('/admin/kreator', 303);
  232. }
  233.  
  234. /**
  235. * Generate form for editing question in wizard
  236. *
  237. * @return void
  238. */
  239. public function get_wizardEdit($id)
  240. {
  241. $question = Model::factory('Wizard')->getQuestion($id);
  242.  
  243. $this->app->render('/partials/admin/wizard-edit.twig', [
  244. 'question' => $question
  245. ]);
  246. }
  247.  
  248. /**
  249. * Handling editing question in wizard
  250. *
  251. * @return void
  252. */
  253. public function post_wizardEdit($id)
  254. {
  255.  
  256. Model::factory('Wizard')->updateQuestion($id, $this->app->request->post());
  257.  
  258. $this->app->response->redirect('/admin/kreator', 303);
  259. }
  260.  
  261. /**
  262. * Deletes question
  263. *
  264. * @return void
  265. */
  266. public function get_wizardDelete($id)
  267. {
  268. if(!Auth::isAdmin())
  269. $this->app->response->redirect('/', 303);
  270.  
  271. Model::factory('Wizard')->deleteQuestion($id);
  272.  
  273. $this->app->response->redirect('/admin/kreator', 303);
  274. }
  275.  
  276.  
  277. //OFFERS
  278.  
  279. /**
  280. * Shows users' offers
  281. *
  282. * @return void
  283. */
  284. public function get_offers()
  285. {
  286. if(!Auth::isAdmin())
  287. $this->app->response->redirect('/', 303);
  288.  
  289. $offers = Model::factory('Offers')->getAll();
  290.  
  291. $this->app->render('/partials/admin/offers.twig', [
  292. 'offers' => $offers
  293. ]);
  294. }
  295.  
  296. /**
  297. * Shows offers from archive
  298. *
  299. * @return void
  300. */
  301. public function get_offersArchived()
  302. {
  303. if(!Auth::isAdmin())
  304. $this->app->response->redirect('/', 303);
  305.  
  306. $offers = Model::factory('Offers')->getArchived();
  307.  
  308. $this->app->render('/partials/admin/offers-archive.twig', [
  309. 'offers' => $offers
  310. ]);
  311. }
  312.  
  313. /**
  314. * Shows specified offer
  315. *
  316. * @param $id integer offer's id
  317. * @return void
  318. */
  319. public function get_offer($id)
  320. {
  321. if(!Auth::isAdmin())
  322. $this->app->response->redirect('/', 303);
  323.  
  324. $offer = Model::factory('Offers')->getOffer($id);
  325.  
  326. if(!$offer)
  327. $this->app->response->redirect('/admin/zapytania', 303);
  328.  
  329. $this->app->render('partials/admin/offer.twig', [
  330. 'offer' => $offer
  331. ]);
  332. }
  333.  
  334. /**
  335. * Archive specified offer
  336. *
  337. * @param $id integer offer's id
  338. * @return void
  339. */
  340. public function get_offerArchive($id)
  341. {
  342. if(!Auth::isAdmin())
  343. $this->app->response->redirect('/', 303);
  344.  
  345. Model::factory('Offers')->archiveOffer($id);
  346.  
  347. $this->app->response->redirect('/admin/zapytania', 303);
  348. }
  349.  
  350. /**
  351. * Deletes offer
  352. *
  353. * @param $id integer offer's id
  354. * @return void
  355. */
  356. public function get_offerDelete($id)
  357. {
  358. if(!Auth::isAdmin())
  359. $this->app->response->redirect('/', 303);
  360.  
  361. Model::factory('Offers')->deleteOffer($id);
  362.  
  363. $this->app->response->redirect($this->app->request->get('archive') ? '/admin/zapytania/archiwum' : '/admin/zapytania', 303);
  364. }
  365.  
  366.  
  367. //BILLS
  368.  
  369. /**
  370. * Show all current bills
  371. *
  372. * @return void
  373. */
  374. public function get_bills()
  375. {
  376. if(!Auth::isAdmin())
  377. $this->app->response->redirect('/', 303);
  378.  
  379. $bills = Model::factory('Bills')->getAll();
  380.  
  381. $this->app->render('/partials/admin/bills.twig', [
  382. 'bills' => $bills
  383. ]);
  384. }
  385.  
  386. /**
  387. * Show all archived bills
  388. *
  389. * @return void
  390. */
  391. public function get_billsArchived()
  392. {
  393. if(!Auth::isAdmin())
  394. $this->app->response->redirect('/', 303);
  395.  
  396. $bills = Model::factory('Bills')->getArchived();
  397.  
  398. $this->app->render('/partials/admin/bills-archive.twig', [
  399. 'bills' => $bills
  400. ]);
  401. }
  402.  
  403. /**
  404. * Get specified bill
  405. *
  406. * @param $id integer bill's id
  407. * @return void
  408. */
  409. public function get_bill($id)
  410. {
  411. if(!Auth::isAdmin())
  412. $this->app->response->redirect('/', 303);
  413.  
  414. $file = Model::factory('Bills')->getBill($id);
  415.  
  416. $this->app->response->headers->set('Content-Type', 'application/pdf');
  417. $this->app->response->headers->set('Content-Disposition', 'attachment; filename="wycena.pdf"');
  418. $this->app->response->setBody($file);
  419. }
  420.  
  421. /**
  422. * Generates form for generating bill
  423. *
  424. * @return void
  425. */
  426. public function get_billNew()
  427. {
  428. if(!Auth::isAdmin())
  429. $this->app->response->redirect('/', 303);
  430.  
  431. $user = $this->app->request->get('user');
  432.  
  433. if(!$user)
  434. $this->app->response->redirect('/admin/wyceny', 303);
  435.  
  436. $this->app->render('partials/admin/generate-bill.twig', [
  437. 'user' => $user
  438. ,'image' => $this->config['image']
  439. ]);
  440. }
  441.  
  442. /**
  443. * Generates new bill
  444. *
  445. * @return void
  446. */
  447. public function post_billNew()
  448. {
  449. if(!Auth::isAdmin())
  450. $this->app->response->redirect('/', 303);
  451.  
  452. $data = $this->app->request->post();
  453. $data['image'] = isset($data['image']) && filter_var($data['image'], FILTER_VALIDATE_URL) ? $data['image'] : $this->config['image'];
  454.  
  455. $filename = Model::factory('Bills')->generateBill($data, $this->view);
  456.  
  457. $email = Model::factory('User')->getData($data['user'], 'email');
  458.  
  459. \Comandeer\Mailer::send([$email], 'Wycena dostępna', $this->view->render('mails/new_bill.twig'), $this->config['admin'], [
  460. [
  461. DIR . '/files/' . $filename
  462. ,'wycena.pdf'
  463. ]
  464. ]);
  465.  
  466. $this->app->response->redirect('/admin/wyceny');
  467. }
  468.  
  469.  
  470. //BANS
  471.  
  472. /**
  473. * Shows list of all bans
  474. *
  475. * @return void
  476. */
  477. public function get_banned()
  478. {
  479. if(!Auth::isAdmin())
  480. $this->app->response->redirect('/', 303);
  481.  
  482. $banned = Model::factory('User')->getBanned();
  483.  
  484. $this->app->render('partials/admin/banned.twig', [
  485. 'banned' => $banned
  486. ]);
  487. }
  488.  
  489. /**
  490. * Shows bans' form
  491. *
  492. * @return void
  493. */
  494. public function get_banNew()
  495. {
  496. if(!Auth::isAdmin())
  497. $this->app->response->redirect('/', 303);
  498.  
  499. $this->app->render('partials/admin/ban-new.twig');
  500. }
  501.  
  502. /**
  503. * Handles ban's form
  504. *
  505. * @return void
  506. */
  507. public function post_banNew()
  508. {
  509. if(!Auth::isAdmin())
  510. $this->app->response->redirect('/', 303);
  511.  
  512. $who = $this->app->request->post('who');
  513.  
  514. switch($this->app->request->post('type'))
  515. {
  516. case 'user':
  517. $model = Model::factory('User');
  518.  
  519. $model->banUser($model->getUserBy('name', $who)['id']);
  520. break;
  521.  
  522. case 'ip':
  523. Model::factory('User')->banIp($who);
  524. break;
  525.  
  526. case 'email':
  527. Model::factory('User')->banEmail($who);
  528. break;
  529. }
  530.  
  531. $this->app->response->redirect('/admin/zbanowani', 303);
  532. }
  533.  
  534. /**
  535. * Ban user
  536. *
  537. * @param $user integer user's id
  538. * @return void
  539. */
  540. public function get_banUser($user)
  541. {
  542. if(!Auth::isAdmin())
  543. $this->app->response->redirect('/', 303);
  544.  
  545. Model::factory('User')->banUser($user);
  546.  
  547. $this->app->response->redirect('/admin/zbanowani', 303);
  548. }
  549.  
  550. /**
  551. * Unban
  552. *
  553. * @param $id integer ban's id
  554. * @return void
  555. */
  556. public function get_unban($id)
  557. {
  558. if(!Auth::isAdmin())
  559. $this->app->response->redirect('/', 303);
  560.  
  561. Model::factory('User')->unban($id);
  562.  
  563. $this->app->response->redirect('/admin/zbanowani', 303);
  564. }
  565.  
  566.  
  567. //SEARCH
  568.  
  569. /**
  570. * Search user
  571. *
  572. * @return void
  573. */
  574. public function get_search()
  575. {
  576. if(!Auth::isAdmin())
  577. $this->app->response->redirect('/', 303);
  578.  
  579. $query = $this->app->request->get('query');
  580.  
  581. if(!$query)
  582. $this->app->response->redirect('/admin/uzytkownicy', 303);
  583.  
  584. $users = Model::factory('User')->search($query);
  585.  
  586. $this->app->render('partials/admin/users.twig', [
  587. 'users' => $users
  588. ,'query' => $query
  589. ]);
  590. }
  591. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement