davide-casiraghi /
laravel-testimonials
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DavideCasiraghi\LaravelTestimonials; |
||
| 4 | |||
| 5 | use DavideCasiraghi\LaravelTestimonials\Models\Testimonial; |
||
| 6 | use DavideCasiraghi\LaravelTestimonials\Models\TestimonialGroup; |
||
| 7 | |||
| 8 | class LaravelTestimonials |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Provide the testimonial data array. |
||
| 12 | * |
||
| 13 | * @param int $testimonialId |
||
| 14 | * @return \DavideCasiraghi\LaravelTestimonials\Models\Testimonial $ret |
||
| 15 | **/ |
||
| 16 | 1 | public static function getTestimonial($testimonialId) |
|
| 17 | { |
||
| 18 | 1 | $ret = Testimonial::where('id', $testimonialId)->first(); |
|
| 19 | |||
| 20 | 1 | return $ret; |
|
| 21 | } |
||
| 22 | |||
| 23 | /**************************************************************************/ |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Provide the testimonials of a specfied group. |
||
| 27 | * |
||
| 28 | * @param int $testimonialId |
||
| 29 | * @return \DavideCasiraghi\LaravelTestimonials\Models\Testimonial $ret |
||
| 30 | **/ |
||
| 31 | 1 | public static function getTestimonialsByGroup($testimonialGroupId) |
|
| 32 | { |
||
| 33 | 1 | $ret = Testimonial::where('testimonials_group', $testimonialGroupId)->get(); |
|
| 34 | |||
| 35 | 1 | return $ret; |
|
| 36 | } |
||
| 37 | |||
| 38 | /**************************************************************************/ |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Provide the testimonial group data array. |
||
| 42 | * |
||
| 43 | * @param int $testimonialGroupId |
||
| 44 | * @return \DavideCasiraghi\LaravelTestimonials\Models\TestimonialGroup $ret |
||
| 45 | **/ |
||
| 46 | 1 | public static function getTestimonialGroup($testimonialGroupId) |
|
| 47 | { |
||
| 48 | 1 | $ret = TestimonialGroup::where('id', $testimonialGroupId)->first(); |
|
| 49 | |||
| 50 | 1 | return $ret; |
|
| 51 | } |
||
| 52 | |||
| 53 | /**************************************************************************/ |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Find the testimonial snippet occurances in the text. |
||
| 57 | * eg. {# testimonial_group testimonial_group_id=[1] #}. |
||
| 58 | * |
||
| 59 | * @param string $text |
||
| 60 | * @return array $matches |
||
| 61 | **/ |
||
| 62 | public static function getTestimonialGroupSnippetOccurrences($text) |
||
| 63 | { |
||
| 64 | $re = '/{\# |
||
| 65 | \h+testimonial_group |
||
| 66 | \h+(testimonial_group_id)=\[([^]]*)] |
||
| 67 | \h*\#}/x'; |
||
| 68 | |||
| 69 | if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) { |
||
| 70 | return $matches; |
||
| 71 | } else { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /**************************************************************************/ |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns the plugin parameters. |
||
| 80 | * |
||
| 81 | * @param array $matches |
||
| 82 | * @return array $ret |
||
| 83 | **/ |
||
| 84 | public static function getSnippetParameters($matches) |
||
| 85 | { |
||
| 86 | $ret = []; |
||
| 87 | |||
| 88 | // Get activation string parameters (from article) |
||
| 89 | $ret['token'] = $matches[0]; |
||
| 90 | //dump($matches); |
||
| 91 | |||
| 92 | $ret['testimonial_group_id'] = $matches[2]; |
||
| 93 | |||
| 94 | return $ret; |
||
| 95 | } |
||
| 96 | |||
| 97 | /**************************************************************************/ |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Return the same text with the testimonials HTML replaced |
||
| 101 | * where the token strings has been found. |
||
| 102 | * |
||
| 103 | * @param string $text |
||
| 104 | * @return string $ret |
||
| 105 | **/ |
||
| 106 | public function replace_testimonial_group_snippets_with_template($text) |
||
| 107 | { |
||
| 108 | $matches = self::getTestimonialGroupSnippetOccurrences($text); |
||
| 109 | // aaaaaa |
||
| 110 | |||
| 111 | if (! empty($matches)) { |
||
| 112 | foreach ($matches as $key => $single_gallery_matches) { |
||
| 113 | $snippetParameters = self::getSnippetParameters($single_gallery_matches); |
||
| 114 | |||
| 115 | $testimonialGroupId = $snippetParameters['testimonial_group_id']; |
||
| 116 | |||
| 117 | $testimonialGroup = self::getTestimonialGroup($testimonialGroupId); |
||
| 118 | $testimonialGroupParameters = ($testimonialGroup) ? (self::getParametersArray($testimonialGroup)) : null; |
||
| 119 | $testimonials = self::getTestimonialsByGroup($testimonialGroupId); |
||
| 120 | |||
| 121 | $testimonialView = self::showTestimonialGroup($testimonialGroup, $testimonialGroupParameters, $testimonials); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 122 | $testimonialHtml = $testimonialView->render(); |
||
| 123 | |||
| 124 | // Substitute the testimonial html to the token that has been found |
||
| 125 | $text = str_replace($snippetParameters['token'], $testimonialHtml, $text); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | $ret = $text; |
||
| 130 | |||
| 131 | return $ret; |
||
| 132 | } |
||
| 133 | |||
| 134 | /***************************************************************************/ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Show a Testimonial group. |
||
| 138 | * |
||
| 139 | * @param \DavideCasiraghi\LaravelTestimonials\Models\TestimonialGroup $testimonialGroup |
||
| 140 | * @param array $testimonialGroupParameters |
||
| 141 | * @param \DavideCasiraghi\LaravelTestimonials\Models\Testimonial $testimonials |
||
| 142 | * @return \Illuminate\Http\Response |
||
| 143 | */ |
||
| 144 | public function showTestimonialGroup($testimonialGroup, $testimonialGroupParameters, $testimonials) |
||
| 145 | { |
||
| 146 | return view('laravel-testimonials::show-testimonial-group', compact('testimonialGroup')) |
||
| 147 | ->with('testimonialGroupParameters', $testimonialGroupParameters) |
||
| 148 | ->with('testimonials', $testimonials); |
||
| 149 | } |
||
| 150 | |||
| 151 | /***************************************************************************/ |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Return an array with the parameters for the testimonial. |
||
| 155 | * @param \DavideCasiraghi\LaravelTestimonials\Models\TestimonialGroup $testimonialGroup |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 2 | public static function getParametersArray($testimonialGroup) |
|
| 159 | { |
||
| 160 | 2 | if ($testimonialGroup->show_title) { |
|
| 161 | 1 | $title_style = 'display: block; '; |
|
| 162 | } else { |
||
| 163 | 1 | $title_style = 'display: none; '; |
|
| 164 | } |
||
| 165 | |||
| 166 | $ret = [ |
||
| 167 | 2 | 'title_style' => $title_style, |
|
| 168 | ]; |
||
| 169 | |||
| 170 | 2 | return $ret; |
|
| 171 | } |
||
| 172 | } |
||
| 173 |