enabled Advertisements
This commit is contained in:
parent
248d95a4a0
commit
92494e80d5
85
app/controllers/AdvertisementsController.php
Executable file
85
app/controllers/AdvertisementsController.php
Executable file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
class AdvertisementsController extends ApplicationController
|
||||
{
|
||||
protected function filters()
|
||||
{
|
||||
return [
|
||||
'admin_only' => ['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);
|
||||
}
|
||||
}
|
@ -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;"]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
app/models/Advertisement.php
Normal file → Executable file
39
app/models/Advertisement.php
Normal file → Executable file
@ -1,5 +1,44 @@
|
||||
<?php
|
||||
class Advertisement extends Rails\ActiveRecord\Base
|
||||
{
|
||||
protected function validations()
|
||||
{
|
||||
return [
|
||||
'ad_type' => [
|
||||
'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';
|
||||
}
|
||||
}
|
||||
|
37
app/views/advertisements/_form.php
Executable file
37
app/views/advertisements/_form.php
Executable file
@ -0,0 +1,37 @@
|
||||
<?= $this->formFor($this->ad, function($f) { ?>
|
||||
<?= $this->partial('shared/error_messages', ['object' => $f->object()]) ?>
|
||||
|
||||
<div>
|
||||
<?= $f->label('image_url') ?>
|
||||
<?= $f->textField('image_url') ?>
|
||||
</div>
|
||||
<div>
|
||||
<?= $f->label('width') ?>
|
||||
<?= $f->textField('width') ?>
|
||||
</div>
|
||||
<div>
|
||||
<?= $f->label('height') ?>
|
||||
<?= $f->textField('height') ?>
|
||||
</div>
|
||||
<div>
|
||||
<?= $f->label('referral_url') ?>
|
||||
<?= $f->textField('referral_url') ?>
|
||||
</div>
|
||||
<div>
|
||||
<?= $f->label('ad_type') ?>
|
||||
<?= $f->select('ad_type', ['Horizontal' => 'horizontal', 'Vertical' => 'vertical']) ?>
|
||||
</div>
|
||||
<div>
|
||||
<?= $f->label('status') ?>
|
||||
<?= $f->textField('status') ?>
|
||||
</div>
|
||||
<?php if ($this->request()->action() == 'edit') : ?>
|
||||
<div>
|
||||
<?= $f->label('reset_hit_count') ?>
|
||||
<?= $f->checkBox('reset_hit_count') ?>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<div>
|
||||
<?= $f->submit() ?>
|
||||
</div>
|
||||
<?php }) ?>
|
5
app/views/advertisements/blank.php
Executable file
5
app/views/advertisements/blank.php
Executable file
@ -0,0 +1,5 @@
|
||||
<h4><?= $this->t('advertisements.new.title') ?></h4>
|
||||
|
||||
<?= $this->partial('form', ['ad' => $this->ad]) ?>
|
||||
|
||||
<?= $this->linkTo($this->t('buttons.back'), $this->advertisementsPath()) ?>
|
5
app/views/advertisements/edit.php
Executable file
5
app/views/advertisements/edit.php
Executable file
@ -0,0 +1,5 @@
|
||||
<h4><?= $this->t('.title', ['id' => $this->ad->id]) ?></h4>
|
||||
|
||||
<?= $this->partial('form', ['ad' => $this->ad]) ?>
|
||||
|
||||
<?= $this->linkTo($this->t('buttons.back'), $this->ad) ?>
|
53
app/views/advertisements/index.php
Executable file
53
app/views/advertisements/index.php
Executable file
@ -0,0 +1,53 @@
|
||||
<h4><?= $this->t('.title') ?></h4>
|
||||
|
||||
<?= $this->formTag($this->updateMultipleAdvertisementsPath(), function() { ?>
|
||||
<?php $ads = [] ?>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="center"><?= $this->checkBoxTag('check_all', 'check_all', false, ['onClick' => "checkbox_toggle(this, 'advertisement_ids[]');"]) ?></th>
|
||||
<th>#</th>
|
||||
<th><?= $this->humanize('image_url') ?></th>
|
||||
<th><?= $this->humanize('referral_url') ?></th>
|
||||
<th><?= $this->humanize('width') ?></th>
|
||||
<th><?= $this->humanize('height') ?></th>
|
||||
<th><?= $this->humanize('ad_type') ?></th>
|
||||
<th><?= $this->humanize('status') ?></th>
|
||||
<th><?= $this->humanize('hit_count') ?></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($this->ads as $ad) : ?>
|
||||
<tr>
|
||||
<td class="center"><?= $this->checkBoxTag('advertisement_ids[]', $ad->id) ?></td>
|
||||
<td><?= $this->linkTo($ad->id, $ad) ?></td>
|
||||
<td><?= $this->linkTo($ad->image_url, $ad->image_url) ?></td>
|
||||
<td><?= $this->linkTo($ad->referral_url, $ad->referral_url) ?></td>
|
||||
<td><?= $ad->width ?></td>
|
||||
<td><?= $ad->height ?></td>
|
||||
<td><?= $ad->ad_type ?></td>
|
||||
<td><?= $ad->status ?></td>
|
||||
<td><?= $ad->hit_count ?></td>
|
||||
<td><?= $this->linkTo($this->t('buttons.edit'), $this->editAdvertisementPath($ad)) ?></td>
|
||||
<td><?= $this->linkTo($this->t('buttons.delete'), $ad, ['data' => ['confirm' => $this->t('confirmations.is_sure')], 'method' => 'delete']) ?></td>
|
||||
</tr>
|
||||
<?php endforeach ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<?= $this->submitTag($this->t('.reset_hit_count'), ['name' => 'do_reset_hit_count']) ?>
|
||||
<?= $this->submitTag($this->t('buttons.delete'), ['name' => 'do_delete']) ?>
|
||||
<?php }) ?>
|
||||
|
||||
<?= $this->linkTo($this->t('buttons.add'), $this->newAdvertisementPath()) ?>
|
||||
<?= $this->willPaginate($this->ads) ?>
|
||||
|
||||
<script type="text/javascript">
|
||||
function checkbox_toggle(source, name) {
|
||||
checkboxes = document.getElementsByName(name);
|
||||
for(var i in checkboxes)
|
||||
checkboxes[i].checked = source.checked;
|
||||
}
|
||||
</script>
|
38
app/views/advertisements/show.php
Executable file
38
app/views/advertisements/show.php
Executable file
@ -0,0 +1,38 @@
|
||||
<h4>Advertisement #<?= $this->ad->id ?></h4>
|
||||
|
||||
<div>
|
||||
<label><?= $this->humanize('id') ?></label>
|
||||
<?= $this->ad->id ?>
|
||||
</div>
|
||||
<div>
|
||||
<label><?= $this->humanize('image_url') ?></label>
|
||||
<?= $this->ad->image_url ?>
|
||||
</div>
|
||||
<div>
|
||||
<label><?= $this->humanize('referral_url') ?></label>
|
||||
<?= $this->ad->referral_url ?>
|
||||
</div>
|
||||
<div>
|
||||
<label><?= $this->humanize('width') ?></label>
|
||||
<?= $this->ad->width ?>
|
||||
</div>
|
||||
<div>
|
||||
<label><?= $this->humanize('height') ?></label>
|
||||
<?= $this->ad->height ?>
|
||||
</div>
|
||||
<div>
|
||||
<label><?= $this->humanize('ad_type') ?></label>
|
||||
<?= $this->ad->ad_type ?>
|
||||
</div>
|
||||
<div>
|
||||
<label><?= $this->humanize('status') ?></label>
|
||||
<?= $this->ad->status ?>
|
||||
</div>
|
||||
<div>
|
||||
<label><?= $this->humanize('hit_count') ?></label>
|
||||
<?= $this->ad->hit_count ?>
|
||||
</div>
|
||||
|
||||
<?= $this->linkTo($this->t('buttons.edit'), $this->editAdvertisementPath($this->ad)) ?>
|
||||
<?= $this->linkTo($this->t('buttons.delete'), $this->ad, ['data' => ['confirm' => $this->t('confirmations.is_sure')], 'method' => 'delete']) ?>
|
||||
<?= $this->linkTo($this->t('buttons.back'), $this->advertisementsPath()) ?>
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user