PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Repositories/Admin/AdminRepository.php

https://gitlab.com/tetrapak07/vimm-me
PHP | 162 lines | 116 code | 28 blank | 18 comment | 8 complexity | d5da6f4ab1498b1eb142ec40499cb2ae MD5 | raw file
  1. <?php namespace App\Repositories\Admin;
  2. use App\Core\EloquentRepository;
  3. use App\Models\Admin as AdminModel;
  4. use AdminAuth;
  5. use Hash;
  6. use App\Repositories\SettingRepository;
  7. use App\Repositories\QuestionRepository;
  8. use App\Repositories\AnswerRepository;
  9. use File;
  10. /**
  11. * Admin Repository
  12. *
  13. * Repository for custom methods of Admin model
  14. *
  15. * @package Repositories
  16. * @author Den
  17. */
  18. class AdminRepository extends EloquentRepository {
  19. /**
  20. *
  21. * @var model
  22. */
  23. protected $model;
  24. public function __construct(AdminModel $model, SettingRepository $setting, QuestionRepository $question, AnswerRepository $answer
  25. ) {
  26. $this->model = $model;
  27. $this->question = $question;
  28. $this->answer = $answer;
  29. }
  30. /**
  31. * Change Admin Password
  32. *
  33. * @param AdminRequest $request change password post data
  34. * @return array
  35. */
  36. public function changePassword($request) {
  37. $input = \Request::all();
  38. $adminId = AdminAuth::user()->id;
  39. $admin = $this->getById($adminId);
  40. $hashedOldPassword = $admin->password;
  41. $oldPassword = $input['old_password'];
  42. if (Hash::check($oldPassword, $hashedOldPassword)) {
  43. $newPassword = bcrypt($input['password']);
  44. $admin->password = $newPassword;
  45. $admin->save();
  46. return ['message' => 'Your Password has been updated.'];
  47. } else {
  48. return ['error' => 'Old password does not match'];
  49. }
  50. }
  51. public function sitemapGen() {
  52. }
  53. public function sitemapGenCommand($adress) {
  54. set_time_limit(0);
  55. $domain = $adress;
  56. echo 'domain: ' . $domain;
  57. $timestamp = date('c', time());
  58. $filename = 'sitemap';
  59. $format = 'xml';
  60. $file = public_path() . DIRECTORY_SEPARATOR . $filename . '.' . $format;
  61. $contentXmlFirst = '<' . '?' . 'xml version="1.0" encoding="UTF-8"?>' . "\n";
  62. $questions = $this->question->getAllVisibleQuestions();
  63. $questionsCount = $questions->count();
  64. $answers = $this->answer->getAllVisibleAnswers();
  65. $answersCount = $answers->count();
  66. $countAll = $questionsCount + $answersCount;
  67. if ($countAll < 50000) {
  68. $size = 'small';
  69. } else {
  70. $size = 'big';
  71. }
  72. echo ' questions count: ' . $questionsCount . '; answers count: ' . $answersCount . '; all count: ' . $countAll . '; size: ' . $size;
  73. $content = '';
  74. $contentMain = '';
  75. if (true) {
  76. $contentXmlFirstRow = $contentXmlFirst . '<' . '?' . 'xml-stylesheet href="/vendor/sitemap/styles/xml.xsl" type="text/xsl"?>';
  77. $contentXmlFirst = '<' . '?' . 'xml-stylesheet href="/vendor/sitemap/styles/sitemapindex.xsl" type="text/xsl"?>';
  78. $countRows = 0;
  79. $countFilesThis = 0;
  80. $contentMainPart = '';
  81. $countFilesAll = floor($countAll / 50000);
  82. echo '; count files: ' . $countFilesAll;
  83. $contentXmlSecondRow = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  84. $contentXmlEndRow = '</sitemapindex>';
  85. for ($i = 0; $i <= $countFilesAll; $i++) {
  86. $fileThisDel = public_path() . DIRECTORY_SEPARATOR . $filename . '-' . $i . '.' . $format;
  87. File::delete($fileThisDel);
  88. $contentMain = $contentMain . '<sitemap>' . "\n" . '<loc>' . $domain . '/sitemap-' . $i . '.xml</loc>' . "\n" . '<lastmod>' . date('Y-m-d\TH:i:sP', strtotime($timestamp)) . '</lastmod>' . "\n" . '</sitemap>' . "\n";
  89. }
  90. $fileThis = public_path() . DIRECTORY_SEPARATOR . $filename . '.' . $format;
  91. $contentXmlFirstRowPart = $contentXmlFirstRow . "\n";
  92. $contentXmlFirstRow = $contentXmlFirst . "\n";
  93. $contentXmlSecondRowPart = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">' . "\n";
  94. $contentXmlEndRowPart = '</urlset>';
  95. $contentIndexFirst = $contentXmlFirstRow . $contentXmlSecondRow . $contentMain . $contentXmlEndRow;
  96. File::delete($fileThis);
  97. File::put($fileThis, $contentIndexFirst);
  98. $fileThis = public_path() . DIRECTORY_SEPARATOR . $filename . '-' . $countFilesThis . '.' . $format;
  99. $contentPartFirst = $contentXmlFirstRowPart . $contentXmlSecondRowPart;
  100. File::put($fileThis, $contentPartFirst);
  101. foreach ($questions as $question) {
  102. if ($countRows > 49999) {
  103. $contentPart = $contentXmlEndRowPart;
  104. File::append($fileThis, $contentPart);
  105. $countFilesThis++;
  106. $countRows = 0;
  107. $fileThis = public_path() . DIRECTORY_SEPARATOR . $filename . '-' . $countFilesThis . '.' . $format;
  108. File::put($fileThis, $contentPartFirst);
  109. }
  110. $url = $domain . '/question/' . $question->slug;
  111. $contentMainPart = '<url>' . "\n" . '<loc>' . $url . '</loc>' . "\n" . '<priority>0.95</priority>' . "\n" . '<lastmod>' . $timestamp . '</lastmod>' . "\n" . '<changefreq>daily</changefreq>' . "\n" . '</url>' . "\n";
  112. $countRows++;
  113. File::append($fileThis, $contentMainPart);
  114. }
  115. foreach ($answers as $answer) {
  116. if ($countRows >= 49999) {
  117. $contentPart = $contentXmlEndRowPart;
  118. File::append($fileThis, $contentPart);
  119. $countFilesThis++;
  120. $countRows = 0;
  121. $fileThis = public_path() . DIRECTORY_SEPARATOR . $filename . '-' . $countFilesThis . '.' . $format;
  122. File::put($fileThis, $contentPartFirst);
  123. }
  124. $url = $domain . '/answer/' . $answer->slug;
  125. $contentMainPart = '<url>' . "\n" . '<loc>' . $url . '</loc>' . "\n" . '<priority>0.9</priority>' . "\n" . '<lastmod>' . $timestamp . '</lastmod>' . "\n" . '<changefreq>daily</changefreq>' . "\n" . '</url>' . "\n";
  126. $countRows++;
  127. File::append($fileThis, $contentMainPart);
  128. }
  129. $contentPart = $contentXmlEndRowPart;
  130. File::append($fileThis, $contentPart);
  131. }
  132. echo 'Your Sitemap was generated.';
  133. }
  134. }