diff --git a/app/controllers/AdvertisementsController.php b/app/controllers/AdvertisementsController.php new file mode 100755 index 0000000..3a6cd95 --- /dev/null +++ b/app/controllers/AdvertisementsController.php @@ -0,0 +1,85 @@ + ['except' => ['redirect']] + ]; + } + + public function index() + { + $this->ads = Advertisement::paginate($this->page_number(), 100); + } + + public function show() + { + $this->ad = Advertisement::find($this->params()->id); + } + + public function blank() + { + $this->ad = new Advertisement(); + } + + public function create() + { + $this->ad = new Advertisement($this->params()->advertisement); + if ($this->ad->save()) { + $this->notice('Advertisement added'); + $this->redirectTo(['#show', 'id' => $this->ad->id]); + } else { + $this->render('blank'); + } + } + + public function edit() + { + $this->ad = Advertisement::find($this->params()->id); + } + + public function update() + { + $this->ad = Advertisement::find($this->params()->id); + if ($this->ad->updateAttributes($this->params()->advertisement)) { + $this->notice('Advertisement updated'); + $this->redirectTo(['#show', 'id' => $this->ad]); + } else { + $this->render('blank'); + } + } + + public function updateMultiple() + { + if ($this->params()->advertisement_ids) { + $ids = array_map(function($a) { return (int)$a; }, $this->params()->advertisement_ids); + } else { + $this->notice('No advertisement selected'); + $this->redirectTo($this->advertisementsPath()); + return; + } + if ($this->params()->do_delete) { + Advertisement::destroyAll(['id' => $ids]); + } elseif ($this->params()->do_reset_hit_count) { + Advertisement::reset_hit_count($ids); + } + $this->notice('Advertisements updated'); + $this->redirectTo($this->advertisementsPath()); + } + + public function destroy() + { + $ad = Advertisement::find($this->params()->id); + $ad->destroy(); + $this->notice('Deleted advertisement ' . $ad->id); + $this->redirectTo($this->advertisementsPath()); + } + + public function redirect() + { + $ad = Advertisement::find($this->params()->id); + $ad->increment('hit_count'); + $this->redirectTo($ad->referral_url); + } +} diff --git a/app/helpers/AdvertisementsHelper.php b/app/helpers/AdvertisementsHelper.php index 7a29396..4cba6be 100755 --- a/app/helpers/AdvertisementsHelper.php +++ b/app/helpers/AdvertisementsHelper.php @@ -4,9 +4,20 @@ class AdvertisementsHelper extends Rails\ActionView\Helper public function print_advertisement($ad_type) { if (CONFIG()->can_see_ads(current_user())) { - // $ad = Advertisement::random($ad_type); - // if ($ad) - // return $this->contentTag("div", $this->linkTo($this->imageTag($ad->image_url, array('alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height), redirect_advertisement_path($ad)), 'style' => "margin-bottom: 1em;")); + $ad = Advertisement::random($ad_type); + if ($ad) { + return $this->contentTag("div", + $this->linkTo( + $this->imageTag( + $ad->image_url, + ['alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height] + ), + $this->redirectAdvertisementPath($ad), + ['target' => '_blank'] + ), + ['style' => "margin-bottom: 1em;"] + ); + } } } } \ No newline at end of file diff --git a/app/models/Advertisement.php b/app/models/Advertisement.php old mode 100644 new mode 100755 index f38080a..0a055f7 --- a/app/models/Advertisement.php +++ b/app/models/Advertisement.php @@ -1,5 +1,44 @@ [ + 'inclusion' => ['in' => ['horizontal', 'vertical']] + ], + 'image_url' => [ 'presence' => true ], + 'referral_url' => [ 'presence' => true ], + 'ad_type' => [ 'presence' => true ], + 'status' => [ 'presence' => true ], + 'width' => [ 'presence' => true ], + 'height' => [ 'presence' => true ] + ]; + } + static public function random($type = 'vertical') + { + return self::where(['ad_type' => $type, 'status' => 'active'])->order('RAND()')->first(); + } + + static public function reset_hit_count($ids) + { + foreach (self::where('id IN (?)', $ids)->take() as $ad) { + $ad->updateAttribute('hit_count', 0); + } + } + + # virtual method for resetting hit count in view + public function setResetHitCount($is_reset) + { + if ($is_reset) { + $this->hit_count = 0; + } + } + + # virtual method for no-reset default in view's form + public function getResetHitCount() + { + return '0'; + } } diff --git a/app/views/advertisements/_form.php b/app/views/advertisements/_form.php new file mode 100755 index 0000000..6e6e005 --- /dev/null +++ b/app/views/advertisements/_form.php @@ -0,0 +1,37 @@ += $this->formFor($this->ad, function($f) { ?> + = $this->partial('shared/error_messages', ['object' => $f->object()]) ?> + +
= $this->checkBoxTag('check_all', 'check_all', false, ['onClick' => "checkbox_toggle(this, 'advertisement_ids[]');"]) ?> | +# | += $this->humanize('image_url') ?> | += $this->humanize('referral_url') ?> | += $this->humanize('width') ?> | += $this->humanize('height') ?> | += $this->humanize('ad_type') ?> | += $this->humanize('status') ?> | += $this->humanize('hit_count') ?> | ++ | + |
---|---|---|---|---|---|---|---|---|---|---|
= $this->checkBoxTag('advertisement_ids[]', $ad->id) ?> | += $this->linkTo($ad->id, $ad) ?> | += $this->linkTo($ad->image_url, $ad->image_url) ?> | += $this->linkTo($ad->referral_url, $ad->referral_url) ?> | += $ad->width ?> | += $ad->height ?> | += $ad->ad_type ?> | += $ad->status ?> | += $ad->hit_count ?> | += $this->linkTo($this->t('buttons.edit'), $this->editAdvertisementPath($ad)) ?> | += $this->linkTo($this->t('buttons.delete'), $ad, ['data' => ['confirm' => $this->t('confirmations.is_sure')], 'method' => 'delete']) ?> | +