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 @@ +formFor($this->ad, function($f) { ?> + partial('shared/error_messages', ['object' => $f->object()]) ?> + +
+ label('image_url') ?> + textField('image_url') ?> +
+
+ label('width') ?> + textField('width') ?> +
+
+ label('height') ?> + textField('height') ?> +
+
+ label('referral_url') ?> + textField('referral_url') ?> +
+
+ label('ad_type') ?> + select('ad_type', ['Horizontal' => 'horizontal', 'Vertical' => 'vertical']) ?> +
+
+ label('status') ?> + textField('status') ?> +
+ request()->action() == 'edit') : ?> +
+ label('reset_hit_count') ?> + checkBox('reset_hit_count') ?> +
+ +
+ submit() ?> +
+ diff --git a/app/views/advertisements/blank.php b/app/views/advertisements/blank.php new file mode 100755 index 0000000..41352e7 --- /dev/null +++ b/app/views/advertisements/blank.php @@ -0,0 +1,5 @@ +

t('advertisements.new.title') ?>

+ +partial('form', ['ad' => $this->ad]) ?> + +linkTo($this->t('buttons.back'), $this->advertisementsPath()) ?> diff --git a/app/views/advertisements/edit.php b/app/views/advertisements/edit.php new file mode 100755 index 0000000..7fc81cb --- /dev/null +++ b/app/views/advertisements/edit.php @@ -0,0 +1,5 @@ +

t('.title', ['id' => $this->ad->id]) ?>

+ +partial('form', ['ad' => $this->ad]) ?> + +linkTo($this->t('buttons.back'), $this->ad) ?> diff --git a/app/views/advertisements/index.php b/app/views/advertisements/index.php new file mode 100755 index 0000000..2b0a037 --- /dev/null +++ b/app/views/advertisements/index.php @@ -0,0 +1,53 @@ +

t('.title') ?>

+ +formTag($this->updateMultipleAdvertisementsPath(), function() { ?> + + + + + + + + + + + + + + + + + + + ads as $ad) : ?> + + + + + + + + + + + + + + + +
checkBoxTag('check_all', 'check_all', false, ['onClick' => "checkbox_toggle(this, 'advertisement_ids[]');"]) ?>#humanize('image_url') ?>humanize('referral_url') ?>humanize('width') ?>humanize('height') ?>humanize('ad_type') ?>humanize('status') ?>humanize('hit_count') ?>
checkBoxTag('advertisement_ids[]', $ad->id) ?>linkTo($ad->id, $ad) ?>linkTo($ad->image_url, $ad->image_url) ?>linkTo($ad->referral_url, $ad->referral_url) ?>width ?>height ?>ad_type ?>status ?>hit_count ?>linkTo($this->t('buttons.edit'), $this->editAdvertisementPath($ad)) ?>linkTo($this->t('buttons.delete'), $ad, ['data' => ['confirm' => $this->t('confirmations.is_sure')], 'method' => 'delete']) ?>
+ + submitTag($this->t('.reset_hit_count'), ['name' => 'do_reset_hit_count']) ?> + submitTag($this->t('buttons.delete'), ['name' => 'do_delete']) ?> + + +linkTo($this->t('buttons.add'), $this->newAdvertisementPath()) ?> +willPaginate($this->ads) ?> + + diff --git a/app/views/advertisements/show.php b/app/views/advertisements/show.php new file mode 100755 index 0000000..6d2f6ed --- /dev/null +++ b/app/views/advertisements/show.php @@ -0,0 +1,38 @@ +

Advertisement #ad->id ?>

+ +
+ + ad->id ?> +
+
+ + ad->image_url ?> +
+
+ + ad->referral_url ?> +
+
+ + ad->width ?> +
+
+ + ad->height ?> +
+
+ + ad->ad_type ?> +
+
+ + ad->status ?> +
+
+ + ad->hit_count ?> +
+ +linkTo($this->t('buttons.edit'), $this->editAdvertisementPath($this->ad)) ?> +linkTo($this->t('buttons.delete'), $this->ad, ['data' => ['confirm' => $this->t('confirmations.is_sure')], 'method' => 'delete']) ?> +linkTo($this->t('buttons.back'), $this->advertisementsPath()) ?> diff --git a/config/routes.php b/config/routes.php index 40207bc..4872ef4 100755 --- a/config/routes.php +++ b/config/routes.php @@ -8,16 +8,14 @@ MyImouto\Application::routes()->draw(function() { $this->post('admin/purge_tags'); # Advertisements - /* - resources :advertisements do - collection do - post :update_multiple - end - member do - get :redirect - end - end - */ + $this->resources('advertisements', function() { + $this->collection(function() { + $this->post('update_multiple'); + }); + $this->member(function() { + $this->get('redirect'); + }); + }); # Artist $this->match('artist(/index)(.:format)', 'artist#index', ['via' => ['get', 'post']]); @@ -273,4 +271,4 @@ MyImouto\Application::routes()->draw(function() { $this->post('wiki/create(.:format)', 'wiki#create'); $this->root('static#index'); -}); \ No newline at end of file +});