PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Application/routes/entreprise.php

https://gitlab.com/Erdrix/overviewCompanies
PHP | 431 lines | 315 code | 53 blank | 63 comment | 34 complexity | c2d33c953ee3aed9273be0b8bf8d36d0 MD5 | raw file
  1. <?php
  2. /**
  3. * Configuration du router
  4. * @template : Template à utiliser lors des rendus
  5. * @variables : Variable pré-définis pour les vues
  6. */
  7. Router::config([
  8. 'variables' => ['navigation' => 'entreprise'],
  9. 'template' => 'global',
  10. 'middleware' => ['authorization'],
  11. ]);
  12. /**
  13. * Interface de listing des entreprises
  14. * @route : get:entreprise/
  15. */
  16. Router::respond('GET', '/', function (Auth $auth) {
  17. return View::make('entreprise.index', ['title' => 'Recherche d\'entreprise', 'admin' => $auth->logged()]);
  18. });
  19. /**
  20. * Interface d'information sur une entreprise
  21. * @route : get:entreprise/view
  22. */
  23. Router::respond('GET', '/view/{id}', function ($id) {
  24. // Récupération des informations
  25. $entreprise = Entreprise::find($id);
  26. $stages = Stage::byEntreprise($id);
  27. $filieres = Filiere::identifiants(['name', 'color']);
  28. $taxes = Taxe::byEntreprise($id);
  29. // Protection des variables
  30. $entreprise = array_escape($entreprise);
  31. // Création des variables de stockage
  32. $data = [];
  33. $date = [];
  34. $annees = ['All'];
  35. // Traitement des informations
  36. $temp = null;
  37. foreach ($stages as $stage) {
  38. $temp = substr($stage->startAt, 0, 4);
  39. $date[] = $temp;
  40. $data[] = [$filieres[$stage->idField]->name, $temp, $stage->study];
  41. $annees[] = $stage->study;
  42. }
  43. // Récupération des taxes
  44. $list_taxe = [];
  45. foreach ($taxes as $taxe) {
  46. $list_taxe[$taxe->year] = $taxe->amount;
  47. }
  48. ksort($list_taxe);
  49. // Gestion des couleurs
  50. $colors = [];
  51. foreach ($filieres as $filiere) {
  52. $colors[$filiere->name] = '#' . $filiere->color;
  53. }
  54. // Gestion du nom des filieres
  55. $fili = [];
  56. foreach ($data as $value) {
  57. $fili[] = $value[0];
  58. }
  59. // Nombre de stage moyen
  60. $fili = array_unique($fili);
  61. $average = count($stages);
  62. $average = round($average / count(array_unique($date)), 1);
  63. $params = [
  64. 'title' => 'Entreprise ' . $entreprise->name,
  65. 'entreprise' => $entreprise,
  66. 'filieres' => $filieres,
  67. 'fili' => $fili,
  68. 'colors' => $colors,
  69. 'annees' => array_keys(array_count_values($annees)),
  70. 'date' => array_keys(array_count_values($date)),
  71. 'average' => $average,
  72. 'data' => $data,
  73. 'taxes' => $list_taxe,
  74. ];
  75. return View::make('entreprise.view', $params);
  76. });
  77. /**
  78. * Interface d'information sur une entreprise
  79. * @route : get:entreprise/view
  80. */
  81. Router::respond('GET', '/overview', function () {
  82. // Récupération des informations
  83. $entreprise = Entreprise::all();
  84. $secteur = Sector::all();
  85. $entreprise = array_escape($entreprise);
  86. $dataCompanies = [];
  87. $date_temp = [];
  88. $list_stage = [];
  89. $list_taxe = [];
  90. $temp = null;
  91. $temp2 = null;
  92. $nbstages1 = [];
  93. $nbstages2 = [];
  94. $entreprise1 = [];
  95. $entreprise2 = [];
  96. $entreprise3 = [];
  97. $montant = [];
  98. $top = [];
  99. $cpt = 0;
  100. foreach ($entreprise as $key1 => $value1) {
  101. $cpt++;
  102. /*Liste des stages et taxes de l'entreprise*/
  103. $stages = Stage::byEntreprise($value1->id);
  104. $taxes = Taxe::byEntreprise($value1->id);
  105. /*Raz du tableau de dates*/
  106. unset($list_stage);
  107. unset($list_taxe);
  108. unset($date_temp);
  109. /*Traitement du nombre de stage par année*/
  110. foreach ($stages as $stage) {
  111. $temp = substr($stage->startAt, 0, 4);
  112. /*Si c'est la premère fois que l'on recontre une année*/
  113. if (!array_key_exists($temp, $list_stage)) {
  114. $list_stage[$temp] = 1;
  115. } else {
  116. $list_stage[$temp] += 1;
  117. } // Sinon on ajoute un stage.
  118. }
  119. ksort($list_stage);
  120. /*Traitement des taxes versée par l'entreprise*/
  121. foreach ($taxes as $taxe) {
  122. $list_taxe[$taxe->year] = $taxe->amount;
  123. }
  124. ksort($list_taxe);
  125. /*On mémorise toutes les dates*/
  126. foreach ($list_taxe as $key => $value) {
  127. $date_temp[] = $key;
  128. }
  129. foreach ($list_stage as $key => $value) {
  130. if (!in_array($key, $date_temp)) {
  131. $date_temp[] = $key;
  132. }
  133. }
  134. ksort($date_temp);
  135. /*Création de dataCompanies*/
  136. foreach ($date_temp as $key2) {
  137. $temp = (array_key_exists($key2, $list_stage)) ? $list_stage[$key2] : 0;
  138. $temp2 = (array_key_exists($key2, $list_taxe)) ? $list_taxe[$key2] : 0;
  139. $dataCompanies[$key2][] =
  140. [
  141. 'idCompanies' => $value1->id,
  142. 'name' => $value1->name,
  143. 'nbstages' => $temp,
  144. 'amount' => $temp2,
  145. 'sector' => $value1->sectors_name,
  146. ];
  147. }
  148. }
  149. ksort($dataCompanies);
  150. /*Initialisation du Top 5*/
  151. foreach ($dataCompanies as $key => $value) {
  152. /*Initialisation du TOP*/
  153. for ($i = 0; $i < 5; $i++) {
  154. $top[$key][$i] = [
  155. 'name' => '?',
  156. 'amount' => 0
  157. ];
  158. }
  159. foreach ($value as $key2 => $value2) {
  160. /*Calcul des sommes total versées chaque année*/
  161. if (!array_key_exists($key, $montant)) {
  162. $montant[$key]['All'] = $value2['amount'];
  163. $montant[$key][$value2['sector']] = $value2['amount'];
  164. } elseif (!array_key_exists($value2['sector'], $montant[$key])) {
  165. $montant[$key][$value2['sector']] = $value2['amount'];
  166. $montant[$key]['All'] += $value2['amount'];
  167. } else {
  168. $montant[$key][$value2['sector']] += $value2['amount'];
  169. $montant[$key]['All'] += $value2['amount'];
  170. }
  171. /*Création du Top 5*/
  172. $i = 0;
  173. while ($i < 5) {
  174. if ($value2['amount'] > $top[$key][$i]['amount']) {
  175. $switch_value = [
  176. 'name' => $top[$key][$i]['name'],
  177. 'amount' => $top[$key][$i]['amount']
  178. ];
  179. $top[$key][$i] = [
  180. 'name' => $value2['name'],
  181. 'amount' => $value2['amount'],
  182. ];
  183. for ($j = $i + 1; $j < 5; $j++) {
  184. $switch_temp = [
  185. 'name' => $top[$key][$j]['name'],
  186. 'amount' => $top[$key][$j]['amount']
  187. ];
  188. $top[$key][$j] = [
  189. 'name' => $switch_value['name'],
  190. 'amount' => $switch_value['amount'],
  191. ];
  192. $switch_value = $switch_temp;
  193. }
  194. $i = 5;
  195. }
  196. $i++;
  197. }
  198. /*Calcul du nombre de stage total et d'entreprise par année trié selon 3 catégories*/
  199. if (($value2['amount'] == 0) && ($value2['nbstages'] != 0)) // Entreprise avec stage sans Taxe.
  200. {
  201. $entreprise1[$key]['All'] = (array_key_exists($key, $entreprise1)) ?
  202. $entreprise1[$key]['All'] + 1 : 1;
  203. $nbstages1[$key]['All'] = (array_key_exists($key, $nbstages1)) ?
  204. $nbstages1[$key]['All'] + $value2['nbstages'] : $value2['nbstages'];
  205. $entreprise1[$key][$value2['sector']] = (array_key_exists($value2['sector'], $entreprise1[$key])) ?
  206. $entreprise1[$key][$value2['sector']] + 1 : 1;
  207. $nbstages1[$key][$value2['sector']] = (array_key_exists($value2['sector'], $nbstages1[$key])) ?
  208. $nbstages1[$key][$value2['sector']] + $value2['nbstages'] : $value2['nbstages'];
  209. } elseif (($value2['amount'] != 0) && ($value2['nbstages'] == 0)) // Entreprise avec Taxe sans stage.
  210. {
  211. $entreprise2[$key]['All'] = (array_key_exists($key, $entreprise2)) ?
  212. $entreprise2[$key]['All'] + 1 : 1;
  213. $entreprise2[$key][$value2['sector']] = (array_key_exists($value2['sector'], $entreprise2[$key])) ?
  214. $entreprise2[$key][$value2['sector']] + 1 : 1;
  215. } elseif (($value2['amount'] != 0) && ($value2['nbstages'] != 0)) // Entreprise avec Taxe et avec stage.
  216. {
  217. $entreprise3[$key]['All'] = (array_key_exists($key, $entreprise3)) ?
  218. $entreprise3[$key]['All'] + 1 : 1;
  219. $nbstages2[$key]['All'] = (array_key_exists($key, $nbstages2)) ?
  220. $nbstages2[$key]['All'] + $value2['nbstages'] : $value2['nbstages'];
  221. $entreprise3[$key][$value2['sector']] = (array_key_exists($value2['sector'], $entreprise3[$key])) ?
  222. $entreprise3[$key][$value2['sector']] + 1 : 1;
  223. $nbstages2[$key][$value2['sector']] = (array_key_exists($value2['sector'], $nbstages2[$key])) ?
  224. $nbstages2[$key][$value2['sector']] + $value2['nbstages'] : $value2['nbstages'];
  225. }
  226. }
  227. }
  228. //dd($top);
  229. $params = [
  230. 'title' => 'OverView',
  231. 'versement' => $montant,
  232. 'top' => $top,
  233. 'type_ent1' => $entreprise1, // avec stage et sans Taxe.
  234. 'type_stage1' => $nbstages1,
  235. 'type_ent2' => $entreprise2, // avec taxe et sans stage.
  236. 'type_ent3' => $entreprise3, // avec stage et taxe.
  237. 'type_stage3' => $nbstages2,
  238. 'secteur' => $secteur,
  239. 'navigation' => 'overview',
  240. ];
  241. return View::make('entreprise.overview', $params);
  242. });
  243. /**
  244. * Interface de création d'entreprise
  245. * @route : get:entreprise/add
  246. */
  247. Router::respond('GET', '/add', function () {
  248. $sector = Sector::identifiants(['name']);
  249. return View::make('entreprise.update', [
  250. 'title' => 'Ajout d\'une entreprise',
  251. 'sector' => $sector,
  252. 'form' => new FormBuilder()
  253. ]);
  254. });
  255. /**
  256. * Interface de mise à jour d'une entreprise
  257. * @route : get:entreprise/modify
  258. */
  259. Router::respond('GET', '/modify/{id}', function ($id) {
  260. $entreprise = Entreprise::find($id);
  261. $sector = Sector::identifiants(['name']);
  262. $params = [
  263. 'title' => 'Modification de l\'entreprise ' . $entreprise->name,
  264. 'sector' => $sector,
  265. 'form' => new FormBuilder($entreprise),
  266. ];
  267. return View::make('entreprise.update', $params);
  268. });
  269. /**
  270. * Interface de suppression d'une entreprise
  271. * @root : get:entreprise/askdelete
  272. */
  273. Router::respond('GET', '/askdelete/{id}', function ($id) {
  274. $entreprise = Entreprise::find($id);
  275. $params = [
  276. 'title' => 'Suppression de l\'entreprise ' . $entreprise->name,
  277. 'entreprise' => $entreprise,
  278. ];
  279. return View::make('entreprise.askdelete', $params);
  280. });
  281. /**
  282. * Suppression d'une entreprise
  283. * @root : post:entreprise/delete
  284. */
  285. Router::respond('GET', '/delete/{id}', function ($id) {
  286. $valide = Entreprise::delete($id) !== false;
  287. if (!$valide) {
  288. Flash::set('modify', 'Impossible de supprimer l\'entreprise numéro ' . $id, 'danger');
  289. } else {
  290. Flash::set('modify', 'L\'entreprise numéro ' . $id . ' vient d\'être supprimée', 'success');
  291. }
  292. return Redirect::make('entreprise');
  293. });
  294. /**
  295. * Récupération de la liste des entreprises
  296. * @route : post:entreprise/request
  297. */
  298. Router::respond('POST', '/request', function (Auth $auth) {
  299. $data = false;
  300. if (isset($_POST['data']) && '*' != $_POST['data']) {
  301. $link = [
  302. 't' => 'phone',
  303. 'p' => 'country',
  304. 'a' => 'adress',
  305. 's' => 'ovc_sectors.name',
  306. ];
  307. $temp = explode('@', $_POST['data']);
  308. $data = [];
  309. foreach ($temp as $part) {
  310. $part = trim($part);
  311. $temp = explode(':', $part);
  312. if (isset($temp[1]) && isset($link[$temp[0]])) {
  313. $data[$link[$temp[0]]] = '%' . $temp[1] . '%';
  314. } else {
  315. $data['name'] = '%' . $part . '%';
  316. }
  317. }
  318. }
  319. $data = $data ? Entreprise::finds($data) : Entreprise::all();
  320. $data = array_escape($data);
  321. $result = '';
  322. $admin = $auth->logged();
  323. foreach ($data as $entreprise) {
  324. $result .= '<tr class = "tbl-item">';
  325. $result .= ' <th scope="row" class = "nameEnt"><a href="?url=entreprise/view/' . $entreprise->id . '/">' . $entreprise->name .
  326. '</a></th>';
  327. $result .= '<td class = "sectorEnt">' . $entreprise->sectors_name . '</td>';
  328. $result .= '<td class = "phoneEnt">' . $entreprise->phone . '</td>';
  329. $result .= '<td class = "citiesEnt">' . $entreprise->adress . '<br>' . $entreprise->cities_name . '<br>' . $entreprise->cities_zip .
  330. '</td>';
  331. $result .= '<td class = "countryEnt">' . $entreprise->country . '</td>';
  332. if ($admin) {
  333. $result .= '<td>';
  334. $result .= ' <a href="?url=entreprise/modify/' . $entreprise->id .
  335. '/" class="btn btn-primary">Modifier</a>';
  336. $result .= ' <a href="?url=entreprise/askdelete/' . $entreprise->id .
  337. '/" class="btn btn-danger">Supprimer</a>';
  338. $result .= '</td>';
  339. }
  340. $result .= '</tr>';
  341. }
  342. return $result;
  343. });
  344. /**
  345. * Mise à jour d'une entreprise
  346. * @route : post:entreprise/update
  347. */
  348. Router::respond('POST', '/modify', function (Validator $validator) {
  349. $valide = false;
  350. $id = isset($_POST['id']) ? $_POST['id'] : false;
  351. unset($_POST['id']);
  352. $validator->check($_POST, [
  353. 'name' => ['required' => true],
  354. 'idSector' => ['required' => true],
  355. 'idCity' => ['default' => 1],
  356. 'phone' => ['default' => '0000000000'],
  357. 'mail' => ['default' => '?'],
  358. 'adress' => ['default' => '?'],
  359. 'country' => ['default' => '?'],
  360. ]);
  361. if ($validator->errors()) {
  362. Flash::set('errors', implode('<br>', $validator->errors()), 'danger');
  363. return Redirect::back();
  364. }
  365. if (isset($_POST['name'])) {
  366. if ($id !== false) {
  367. $valide = Entreprise::update($id, $_POST) !== false;
  368. } else {
  369. $valide = Entreprise::create($_POST) !== false;
  370. }
  371. }
  372. if ($valide) {
  373. Flash::set('modify', "L'opération s'est déroulée avec succès", 'success');
  374. } else {
  375. Flash::set('modify', "Une erreur est survenue", 'danger');
  376. }
  377. return Redirect::make('entreprise/');
  378. });