PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/BasesController.php

https://bitbucket.org/hfab/webservice
PHP | 665 lines | 491 code | 121 blank | 53 comment | 32 complexity | 8dbe8f94e6ca1701a390179ea44d075b MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, MIT, GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests;
  4. use App\Http\Controllers\Controller;
  5. use App\Jobs\BaseImportDestinataires;
  6. use App\Jobs\BaseImportDesinscrits;
  7. use App\Jobs\BaseImportBlacklistes;
  8. use App\Jobs\BaseImportBounces;
  9. use App\Jobs\BaseCompareDestinataires;
  10. use App\Jobs\BaseImportMindbaz;
  11. use App\Models\Base;
  12. use App\Models\Campagne;
  13. use App\Models\Destinataire;
  14. use App\Models\Fai;
  15. use App\Models\Planning;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Pagination\PaginationServiceProvider;
  18. // fab1 pour le crawldb
  19. use App\Http\Controllers\DB;
  20. // use DB;
  21. use App\Quotation;
  22. use Carbon\Carbon;
  23. use App\Jobs\BaseImportDestinatairesPerso;
  24. class BasesController extends Controller {
  25. /**
  26. * Display a listing of the resource.
  27. *
  28. * @return Response
  29. */
  30. public function index()
  31. {
  32. $bases = Base::with('ViewCountDestinataires')->get();
  33. return view('base.index_new')
  34. ->withMenu('bases')
  35. ->withBases($bases);
  36. }
  37. /**
  38. * Show the form for creating a new resource.
  39. *
  40. * @return Response
  41. */
  42. public function create()
  43. {
  44. return view('base.create_new')
  45. ->withMenu('bases')
  46. ->withTrigram('');
  47. }
  48. /**
  49. * Store a newly created resource in storage.
  50. *
  51. * @return Response
  52. */
  53. public function store()
  54. {
  55. $message = 'La variable [unsubscribelink] du lien de désinscription est manquante.';
  56. if((strlen(\Input::get('header_html')) > 0 or strlen(\Input::get('header_html')) > 0) and (stripos(\Input::get('footer_html'),'[unsubscribelink]') === false)) {
  57. return view('base.create_new')
  58. ->withMenu('bases')
  59. ->withError($message);
  60. }
  61. $base = new Base();
  62. $base->nom = \Input::get('nom');
  63. $base->code = \Input::get('code');
  64. $base->mentions_legales = \Input::get('mentions_legales');
  65. $base->mentions_header = \Input::get('mentions_header');
  66. $base->is_active = 1;
  67. //info si l'on doit rajouter les tags partenaires
  68. if(\Input::get('is_active') != 'on') {
  69. $base->is_active = 0;
  70. }
  71. $message = "La base a été mise à jour avec succès.";
  72. $base->header_html = \Input::get('header_html');
  73. $base->footer_html = \Input::get('footer_html');
  74. $base->nom_reply = \Input::get('nom_reply');
  75. $base->email_reply = \Input::get('email_reply');
  76. $base->save();
  77. $trigram = \Input::get('trigram');
  78. \DB::table('base_wizweb')->where('base_id', $base->id)->delete();
  79. if(!empty($trigram)) {
  80. \DB::table('base_wizweb')->insert(['base_id' => $base->id, 'trigram' => $trigram]);
  81. }
  82. return \redirect('base')->withMessage($message);
  83. }
  84. /**
  85. * Display the specified resource.
  86. *
  87. * @param int $id
  88. * @return Response
  89. */
  90. public function show($id)
  91. {
  92. //
  93. }
  94. /**
  95. * Show the form for editing the specified resource.
  96. *
  97. * @param int $id
  98. * @return Response
  99. */
  100. public function edit($id)
  101. {
  102. $base = Base::find($id);
  103. //Facultatif : Trigram pour wizweb
  104. $select_trigram = \DB::table('base_wizweb')
  105. ->select('trigram')
  106. ->where('base_id', $base->id)
  107. ->first();
  108. $trigram = "";
  109. if(!empty($select_trigram)){
  110. $trigram = $select_trigram->trigram;
  111. }
  112. return view('base.edit_new')
  113. ->withMenu('bases')
  114. ->withBase($base)
  115. ->withTrigram($trigram);
  116. }
  117. /**
  118. * Update the specified resource in storage.
  119. *
  120. * @param int $id
  121. * @return Response
  122. */
  123. public function update($id)
  124. {
  125. $message = "La base a été mise à jour avec succès.";
  126. $base = Base::find($id);
  127. $base->nom = \Input::get('nom');
  128. $base->code = \Input::get('code');
  129. $base->mentions_legales = \Input::get('mentions_legales');
  130. $base->mentions_header = \Input::get('mentions_header');
  131. $base->is_active = 1;
  132. //info si l'on doit rajouter les tags partenaires
  133. if(\Input::get('is_active') != 'on') {
  134. $base->is_active = 0;
  135. }
  136. if(stripos(\Input::get('header_html'),'[unsubscribelink]') === false and stripos(\Input::get('footer_html'),'[unsubscribelink]') === false) {
  137. $message = 'La variable [unsubscribelink] du lien de désinscription est manquante.';
  138. }
  139. $base->nom_reply = \Input::get('nom_reply');
  140. $base->email_reply = \Input::get('email_reply');
  141. $base->header_html = \Input::get('header_html');
  142. $base->footer_html = \Input::get('footer_html');
  143. $base->save();
  144. $trigram = \Input::get('trigram');
  145. \DB::table('base_wizweb')->where('base_id', $base->id)->delete();
  146. if(!empty($trigram)) {
  147. \DB::table('base_wizweb')->insert(['base_id' => $base->id, 'trigram' => $trigram]);
  148. }
  149. return redirect('base/'.$base->id.'/edit')
  150. ->withMenu('bases')
  151. ->withBase($base)
  152. ->withTrigram($trigram)
  153. ->withError($message);
  154. }
  155. /**
  156. * Remove the specified resource from storage.
  157. *
  158. * @param int $id
  159. * @return Response
  160. */
  161. public function destroy($id)
  162. {
  163. \DB::statement('DELETE FROM tokens where base_id ='.$id);
  164. $campagnes_ids = array_pluck(\DB::table('campagnes')->where('base_id',$id)->get(), 'id');
  165. \DB::table('campagnes_routeurs')->whereIn('campagne_id', $campagnes_ids)->delete();
  166. \DB::table('tokens_stats')->where('base_id', $id)->delete();
  167. \DB::table('ouvertures_stats_bases')->where('base_id', $id)->delete();
  168. \DB::table('campagnes')->where('base_id',$id)->delete();
  169. \DB::table('destinataires')->where('base_id',$id)->delete();
  170. \DB::table('plannings')->whereIn('campagne_id', $campagnes_ids)->delete();
  171. Base::find($id)->delete();
  172. return \redirect('base');
  173. }
  174. public function add()
  175. {
  176. return view('base.add_new')
  177. ->withMenu('bases');
  178. }
  179. public function add_destinataires($id)
  180. {
  181. $base = Base::find($id);
  182. return view('base.add_destinataires_new')
  183. ->withMenu('bases')
  184. ->withBase($base);
  185. }
  186. public function inject_file($base_id)
  187. {
  188. $name = \Input::file('file')->getClientOriginalName();
  189. \Input::file('file')->move(storage_path() . '/listes/', $name);
  190. $this->dispatch (new BaseImportDestinataires($base_id, \Input::file('file')->getClientOriginalName()));
  191. return "OK";
  192. }
  193. public function inject_file_type()
  194. {
  195. $name = \Input::file('file')->getClientOriginalName();
  196. $type = \Input::get('type');
  197. \Input::file('file')->move(storage_path() . "/$type/", $name);
  198. if($type == 'bounces') {
  199. $this->dispatch(new BaseImportBounces(\Input::file('file')->getClientOriginalName()));
  200. }
  201. elseif($type == 'blacklistes'){
  202. $this->dispatch(new BaseImportBlacklistes(\Input::file('file')->getClientOriginalName()));
  203. }
  204. elseif ($type == 'desinscrits'){
  205. $this->dispatch(new BaseImportDesinscrits(\Input::file('file')->getClientOriginalName()));
  206. }
  207. else{
  208. return false;
  209. }
  210. return "OK";
  211. }
  212. function stats($id)
  213. {
  214. $base = Base::find($id);
  215. $fais = Destinataire::where('statut', 0)
  216. ->where('base_id', $base->id)
  217. ->selectRaw('fai_id, count(*) as num')
  218. ->groupBy('fai_id')
  219. ->orderByRaw('num desc')
  220. ->get();
  221. return view('base.stats')
  222. ->withPageTitle('Statistiques sur la base ' . $base->nom)
  223. ->withMenu('bases')
  224. ->withFais($fais)
  225. ->withBase($base);
  226. }
  227. function choixBaseExtract()
  228. {
  229. $bases = Base::with('ViewCountDestinataires')->get();
  230. return view('base.extract')
  231. ->withMenu('bases')
  232. ->withBases($bases);
  233. }
  234. function extract_md5($id)
  235. {
  236. set_time_limit(0);
  237. ini_set('max_execution_time', 0);
  238. $today = Carbon::today()->format('Ymd');
  239. $filename = storage_path() . "/listes/md5-base-$id-$today.csv";
  240. $ressource_fichier = fopen($filename, 'w+');
  241. \DB::table('destinataires')
  242. ->select('hash')
  243. ->where('base_id', $id)
  244. ->where('statut', '<', 3)
  245. ->chunk(50000, function ($emailbase) use ($ressource_fichier) {
  246. foreach ($emailbase as $e) {
  247. fputs($ressource_fichier, $e->hash . "\n");
  248. }
  249. });
  250. return response()->download($filename);
  251. }
  252. function compare_upload($base_id)
  253. {
  254. $base = Base::find($base_id);
  255. return view('base.compare_upload')
  256. ->with('base',$base);
  257. }
  258. function compare_mails($base_id, $type='md5')
  259. {
  260. set_time_limit(0);
  261. ini_set('max_execution_time', 0);
  262. \DB::disableQueryLog();
  263. $name = \Input::file('file')->getClientOriginalName();
  264. $base_id = \Input::get('base_id');
  265. $type = \Input::get('type');
  266. \Input::file('file')->move(storage_path() . "/compare_$type/", $name);
  267. $this->dispatch (new BaseCompareDestinataires($base_id, $type, \Input::file('file')->getClientOriginalName()));
  268. }
  269. function extract_contacts($id)
  270. {
  271. set_time_limit(0);
  272. ini_set('max_execution_time', 0);
  273. \DB::disableQueryLog();
  274. $today = Carbon::today()->format('Ymd');
  275. $filename = storage_path() . "/listes/base-$id-$today.csv";
  276. $ressource_fichier = fopen($filename, 'w+');
  277. \DB::table('destinataires')
  278. ->select('mail')
  279. ->where('base_id', $id)
  280. ->where('statut', '<', 3)
  281. ->chunk(50000, function ($emailbase) use ($ressource_fichier) {
  282. foreach ($emailbase as $e) {
  283. fputs($ressource_fichier, $e->mail . "\n");
  284. }
  285. });
  286. return response()->download($filename);
  287. }
  288. function add_blacklist(){
  289. }
  290. function check_desabo(){
  291. $tabcheck = array();
  292. $check = array();
  293. $etat_fr = 'Actif';
  294. $checkemail = \Input::get('email');
  295. $resultatcheck = \DB::table('destinataires')->where('mail', $checkemail)->get();
  296. foreach ($resultatcheck as $v) {
  297. $labase = \DB::table('bases')->where('id', $v->base_id)->first();
  298. // var_dump($labase);
  299. if($labase == null){
  300. $message = "La base de ce destinataire n'existe plus";
  301. return view('base.checkemailstatut')->with('checkemail',$checkemail)->with('tabcheck',$tabcheck)->with('message',$message);
  302. }
  303. if($v->statut > 2){
  304. $etat_fr = 'Non actif';
  305. $datedesabo = $v->optout_at;
  306. $from_file = $v->optout_file;
  307. } else {
  308. $etat_fr = 'Actif';
  309. $datedesabo = null;
  310. $from_file = null;
  311. }
  312. $check = [
  313. 'base_id' => $labase->id,
  314. 'base_nom' => $labase->nom,
  315. 'email' => $v->mail,
  316. 'statut' => $v->statut,
  317. 'etat_fr' => $etat_fr,
  318. 'date_desabo' => $v->optout_at,
  319. 'from' => $v->optout_file
  320. ];
  321. $tabcheck[] = $check;
  322. $check = array();
  323. }
  324. $message = null;
  325. return view('base.checkemailstatut')->with('checkemail',$checkemail)->with('tabcheck',$tabcheck)->with('message',$message);
  326. }
  327. function searchall(){
  328. $fields = array_except(\Input::all(), '_token');
  329. $recherche = '';
  330. // var_dump($fields['searchall']);
  331. if(isset($fields['searchall'])){
  332. $recherche = $fields['searchall'];
  333. }
  334. if(isset($fields['recherche'])){
  335. $recherche = $fields['recherche'];
  336. }
  337. if(isset($fields['export'])){
  338. $today = Carbon::today()->format('YmdHis');
  339. $filename = storage_path() . "/listes/export-results-$today.csv";
  340. $ressource_fichier = fopen($filename, 'w+');
  341. \DB::table('destinataires')
  342. ->select('mail')
  343. ->where('mail', 'LIKE', '%'.$recherche.'%')
  344. ->chunk(50000, function ($emailbase) use ($ressource_fichier) {
  345. foreach ($emailbase as $e) {
  346. fputs($ressource_fichier, $e->mail . "\n");
  347. }
  348. });
  349. return response()->download($filename);
  350. }
  351. $desti = Destinataire::where('mail','LIKE','%'.$recherche.'%')->paginate(100);
  352. return view('base.show_desti_all_new', ['recherche' => $recherche])
  353. //->with('base',$base)
  354. //->with('lesbases',$lesbases)
  355. ->with('desti',$desti)
  356. ->with('recherche',$recherche);
  357. }
  358. function exportresults(){
  359. $fields = array_except(\Input::all(), '_token');
  360. $exportall = \Input::get('exportall');
  361. // var_dump($fields['searchall']);
  362. if(isset($fields['searchall'])){
  363. $recherche = $fields['searchall'];
  364. }
  365. if(isset($fields['recherche'])){
  366. $recherche = $fields['recherche'];
  367. }
  368. $desti = Destinataire::where('mail','LIKE','%'.$recherche.'%')->paginate(100);
  369. return view('base.show_desti_all', ['recherche' => $recherche])
  370. //->with('base',$base)
  371. //->with('lesbases',$lesbases)
  372. ->with('desti',$desti)
  373. ->with('recherche',$recherche);
  374. }
  375. function show_base_destinataires($id){
  376. $recherche = '';
  377. if(!empty(\Input::get('recherche')) ){
  378. // recup de la ref et suppression de l'espace ajouté pour affichage
  379. $recherche = \Input::get('recherche');
  380. $query->where('mail', 'LIKE', '%' . $recherche . '%');
  381. }
  382. $fai = \DB::table('fais')->get();
  383. $desti = Destinataire::where('base_id',$id)->paginate(100);
  384. $base = \DB::table('bases')->where('id',$id)->first();
  385. $lesbases = \DB::table('bases')->get();
  386. return view('base.show_desti', ['desti' => $desti])
  387. ->with('base',$base)
  388. ->with('lesbases',$lesbases)
  389. ->with('fai',$fai)
  390. ->with('recherche',$recherche);
  391. }
  392. function edit_destinataire($id,$destinataire_id){
  393. $base = \DB::table('bases')->where('id',$id)->first();
  394. $desti = \DB::table('destinataires')->where('id',$destinataire_id)->first();
  395. return view('base.edit_desti')->with('desti',$desti)->with('base',$base);
  396. }
  397. function store_destinataire(){
  398. $fields = array_except(\Input::all(), '_token');
  399. \DB::table('destinataires')
  400. ->where('id', $fields['desti_id'])
  401. ->update([
  402. 'mail' => $fields['mail'],
  403. 'nom' => $fields['nom'],
  404. 'prenom' => $fields['prenom'],
  405. 'datenaissance' => $fields['datenaissance'],
  406. 'ville' => $fields['ville'],
  407. 'departement' => $fields['departement'],
  408. 'civilite' => $fields['sexe'],
  409. ]);
  410. return \redirect('base/'.$fields['base_id'].'/destinataire');
  411. }
  412. function show_base_destinataires_crit($id,$typecrit,$valuecrit){
  413. $recherche = '';
  414. $lesbases = \DB::table('bases')->get();
  415. $fai = \DB::table('fais')->get();
  416. $base = \DB::table('bases')->where('id',$id)->first();
  417. if($typecrit == 'fai'){
  418. $desti = Destinataire::where('base_id',$id)->where('fai_id',$valuecrit)->paginate(100);
  419. return view('base.show_desti', ['desti' => $desti])
  420. ->with('base',$base)
  421. ->with('lesbases',$lesbases)
  422. ->with('fai',$fai)
  423. ->with('recherche',$recherche);
  424. }
  425. if($typecrit == 'abo'){
  426. if($valuecrit == 0){
  427. // echo 'les desinscrits';
  428. $desti = Destinataire::where('base_id',$id)->where('statut','>','0')->paginate(100);
  429. return view('base.show_desti', ['desti' => $desti])
  430. ->with('base',$base)
  431. ->with('lesbases',$lesbases)
  432. ->with('fai',$fai)
  433. ->with('recherche',$recherche);
  434. }
  435. if($valuecrit == 1){
  436. // echo 'les abonnés';
  437. $desti = Destinataire::where('base_id',$id)->where('statut','0')->paginate(100);
  438. return view('base.show_desti', ['desti' => $desti])
  439. ->with('base',$base)
  440. ->with('lesbases',$lesbases)
  441. ->with('fai',$fai)
  442. ->with('recherche',$recherche);
  443. }
  444. }
  445. }
  446. function show_search_destinataires_results($id){
  447. $recherche = '';
  448. $fields = array_except(\Input::all(), '_token');
  449. if(isset($fields['recherche'])){
  450. $recherche = $fields['recherche'];
  451. }
  452. $fai = \DB::table('fais')->get();
  453. $lesbases = \DB::table('bases')->get();
  454. $base = \DB::table('bases')->where('id',$id)->first();
  455. // renvoyer les resultats dans la blade sur la bonne base
  456. $desti = Destinataire::where('base_id',$id)->where('mail','LIKE','%'.$fields['recherche'].'%')->paginate(100);
  457. return view('base.show_desti', ['recherche' => $recherche])
  458. ->with('base',$base)
  459. ->with('lesbases',$lesbases)
  460. ->with('fai',$fai)
  461. ->with('recherche',$recherche)
  462. ->with('desti',$desti);
  463. }
  464. public function get_search_destinataires_ajax() {
  465. $base_id = \Input::get('base_id');
  466. $recherche = \Input::get('recherche');
  467. $source = \DB::table('destinataires')
  468. ->select('mail')
  469. ->where('base_id', $base_id)
  470. ->where('mail', 'like', "%$recherche%")
  471. ->orderBy('mail')
  472. ->take(20)
  473. ->lists('mail');
  474. return json_encode($source);
  475. }
  476. public function add_destinataires_perso($id)
  477. {
  478. $base = Base::find($id);
  479. return view('base.choix_champs_perso_new')
  480. ->withMenu('bases')
  481. ->withBase($base);
  482. }
  483. function import_desti_champs_perso_prepare($id){
  484. $base = Base::find($id);
  485. $champsperso = array_except(\Input::all(), '_token');
  486. $content = array();
  487. $listechamp = $champsperso['result'];
  488. // fichier not null
  489. $name = \Input::file('fichier')->getClientOriginalName();
  490. \Input::file('fichier')->move(storage_path() . '/listes/perso/', $name);
  491. $handle = fopen(storage_path('listes/perso/'. $name), 'r');
  492. // param nombre de lignes
  493. $countapercu = 3;
  494. while($ligne = fgets($handle))
  495. {
  496. if($ligne == '' || $ligne == "\n"){
  497. // on fait rien
  498. } else {
  499. if($countapercu == 0){
  500. break;
  501. } else {
  502. // echo $ligne;
  503. $content[]= $ligne;
  504. }
  505. $countapercu = $countapercu - 1;
  506. }
  507. }
  508. return view('base.apercu_champ_perso_new')
  509. ->withMenu('bases')
  510. ->withBase($base)
  511. ->with('content',$content)
  512. ->with('name', $name)
  513. ->with('listechamp',$listechamp);
  514. }
  515. function import_desti_champs_perso_run($id){
  516. $form = array_except(\Input::all(), '_token');
  517. $this->dispatch(new BaseImportDestinatairesPerso($id,$form['lefichier'], $form['champs']));
  518. return \redirect("base/".$id."/add_destinataires_perso");
  519. }
  520. public function desabo_fct_domainName($recherche){
  521. \DB::table('destinataires')->where('mail','LIKE','%'.$recherche.'%')->update(['statut' => 3]);
  522. return redirect('base/searchmailall');
  523. }
  524. }