more changes related to advertisements.

added position to advertisements. with it, one can choose to either show an horizontal add only at top, bottom or both.
removed can_see_ads check from AdvertisementsHelper::print_advertisement() because it's already checked before calling it.
horizontal-bottom ads in post#index are centered.
removed "Reset hit count" checkbox in ads#edit.
Width and Height in ads form are now "number" fields.
Width and Height must now be set also for "Html" ads.
made ads#show a little nicer.
ads#create and ads#update redirect to ads#index if success.
some other related changes.

made ad#show a little nicer
This commit is contained in:
Parziphal 2013-12-05 14:56:41 -05:00
parent ff28733e94
commit 12e3a57053
11 changed files with 189 additions and 86 deletions

View File

@ -30,7 +30,7 @@ class AdvertisementsController extends ApplicationController
$this->ad = new Advertisement($this->params()->advertisement);
if ($this->ad->save()) {
$this->notice('Advertisement added');
$this->redirectTo($this->ad);
$this->redirectTo('#index');
} else {
$this->render('blank');
}
@ -46,7 +46,7 @@ class AdvertisementsController extends ApplicationController
$this->ad = Advertisement::find($this->params()->id);
if ($this->ad->updateAttributes($this->params()->advertisement)) {
$this->notice('Advertisement updated');
$this->redirectTo($this->ad);
$this->redirectTo('#index');
} else {
$this->render('blank');
}

View File

@ -1,23 +1,28 @@
<?php
class AdvertisementsHelper extends Rails\ActionView\Helper
{
public function print_advertisement($ad_type)
public function print_advertisement($ad_type, $position = null, $center = false)
{
if (CONFIG()->can_see_ads(current_user())) {
$ad = Advertisement::random($ad_type);
if ($ad) {
if ($ad->html) {
return $ad->html;
} else {
return $this->linkTo(
$this->imageTag(
$ad->image_url,
['alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height]
),
$this->redirectAdvertisementPath($ad),
['target' => '_blank']
);
}
$ad = Advertisement::random($ad_type, substr($position, 0, 1));
if ($ad) {
if ($ad->html) {
$contents = $ad->html;
} else {
$contents = $this->linkTo(
$this->imageTag(
$ad->image_url,
['alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height]
),
$this->redirectAdvertisementPath($ad),
['target' => '_blank']
);
}
if ($center) {
return $this->contentTag('div', $contents, ['style' => 'margin:0 auto;width:' . $ad->width . 'px;height:' . $ad->height . 'px;']);
} else {
return $contents;
}
}
}

View File

@ -1,6 +1,9 @@
<?php
class Advertisement extends Rails\ActiveRecord\Base
{
# Valid positions for horizontal advertisements: any, top, bottom.
static protected $POSITIONS = ['a', 't', 'b'];
protected function validations()
{
return [
@ -9,13 +12,18 @@ class Advertisement extends Rails\ActiveRecord\Base
],
'ad_type' => [ 'presence' => true ],
'status' => [ 'presence' => true ],
'validateType'
'validateType',
'validatePosition',
];
}
static public function random($type = 'vertical')
static public function random($type = 'vertical', $position = null)
{
return self::where(['ad_type' => $type, 'status' => 'active'])->order('RAND()')->first();
$sql = self::where(['ad_type' => $type, 'status' => 'active'])->order('RAND()');
if ($position) {
$sql->where('position IN (?)', ['a', $position]);
}
return $sql->first();
}
static public function reset_hit_count($ids)
@ -39,13 +47,53 @@ class Advertisement extends Rails\ActiveRecord\Base
return '0';
}
public function prettyPosition()
{
switch ($this->position) {
case 'a':
return 'Any';
case 't':
return 'Top';
case 'b':
return 'Bottom';
default:
return 'Unknown';
}
}
protected function validatePosition()
{
if ($this->ad_type == 'vertical') {
$this->position = null;
} else {
if (!in_array($this->position, self::$POSITIONS)) {
$this->errors()->add('position', "is invalid");
return false;
}
}
}
protected function validateType()
{
# Common needed attributes, width and height.
$attr = null;
if (!$this->width) {
$attr = 'width';
} elseif (!$this->height) {
$attr = 'height';
}
if ($attr) {
$this->errors()->add($attr, "can't be blank");
return false;
}
if ($this->html) {
$this->image_url = null;
$this->referral_url = null;
$this->width = null;
$this->height = null;
} else {
$attr = '';
@ -53,10 +101,6 @@ class Advertisement extends Rails\ActiveRecord\Base
$attr = 'image_url';
} elseif (!$this->referral_url) {
$attr = 'referral_url';
} elseif (!$this->width) {
$attr = 'width';
} elseif (!$this->height) {
$attr = 'height';
}
if ($attr) {

View File

@ -40,20 +40,52 @@
<th><?= $f->label('ad_type') ?></th>
<td><?= $f->select('ad_type', ['Horizontal' => 'horizontal', 'Vertical' => 'vertical']) ?></td>
</tr>
<script>
jQuery(function(){
var $ = jQuery;
var p = $('tbody.ad_position');
$('#advertisement_ad_type').on('change', function(){
if ($(this).val() == 'horizontal') {
p.show();
} else {
p.hide();
}
});
});
</script>
</tbody>
<tbody class="ad_position<?php if ($f->object()->ad_type == 'vertical') echo ' hide-box' ?>">
<tr>
<th><?= $f->label('position') ?></th>
<td><?= $f->select('position', ['Any' => 'a', 'Top' => 't', 'Bottom' => 'b']) ?></td>
</tr>
</tbody>
<tbody>
<tr>
<th><?= $f->label('status') ?></th>
<td><?= $f->select('status', ['Active' => 'active', 'Disabled' => 'disabled']) ?></td>
</tr>
<?php if ($action == 'edit') : ?>
<?php if (false && $action == 'edit') : // Why is this here? ?>
<tr>
<th><?= $f->label('reset_hit_count') ?></th>
<td><?= $f->checkBox('reset_hit_count') ?></td>
<td><?= $this->checkBoxTag('reset_hit_count', 1, false, ['id' => 'advertisement_reset_hit_count']) ?></td>
</tr>
<?php endif ?>
<tr>
<th><?= $f->label('width') ?></th>
<td><?= $f->field('number', 'width', ['value' => $f->object()->width ?: 0, 'min' => 0]) ?></td>
</tr>
<tr>
<th><?= $f->label('height') ?></th>
<td><?= $f->field('number', 'height', ['value' => $f->object()->height ?: 0, 'min' => 0]) ?></td>
</tr>
</tbody>
<tbody class="type-fields type-image<?php if ($action != 'blank' && $f->object()->html) echo ' hide' ?>">
<tbody class="type-fields type-image<?php if ($action != 'blank' && $f->object()->html) echo ' hide-box' ?>">
<tr>
<th><?= $f->label('image_url') ?></th>
<td><?= $f->textField('image_url') ?></td>
@ -62,17 +94,9 @@
<th><?= $f->label('referral_url') ?></th>
<td><?= $f->textField('referral_url') ?></td>
</tr>
<tr>
<th><?= $f->label('width') ?></th>
<td><?= $f->textField('width') ?></td>
</tr>
<tr>
<th><?= $f->label('height') ?></th>
<td><?= $f->textField('height') ?></td>
</tr>
</tbody>
<tbody class="type-fields type-html<?php if ($action == 'blank' || !$f->object()->html) echo ' hide' ?>">
<tbody class="type-fields type-html<?php if ($action == 'blank' || !$f->object()->html) echo ' hide-box' ?>">
<tr>
<th><?= $f->label('html') ?></th>
<td><?= $f->textArea('html', ['style' => 'height: 300px;']) ?></td>
@ -92,7 +116,7 @@ table.form [type=text], table.form textarea{
width: 100%;
box-sizing: border-box;
}
table.form tbody.hide {
.hide-box {
display:none;
}
</style>

View File

@ -12,6 +12,7 @@
<th><?= $this->humanize('width') ?></th>
<th><?= $this->humanize('height') ?></th>
<th><?= $this->humanize('ad_type') ?></th>
<th><?= $this->humanize('position') ?></th>
<th><?= $this->humanize('status') ?></th>
<th><?= $this->humanize('hit_count') ?></th>
<th></th>
@ -34,6 +35,7 @@
<td><?= $ad->width ?></td>
<td><?= $ad->height ?></td>
<td><?= $ad->ad_type ?></td>
<td><?= $ad->ad_type == 'vertical' ? '&ndash;' : $ad->prettyPosition() ?></td>
<td><?= $ad->status ?></td>
<td><?= $ad->hit_count ?></td>
<td><?= $this->linkTo($this->t('buttons.edit'), $this->editAdvertisementPath($ad)) ?></td>

View File

@ -1,46 +1,56 @@
<h4>Advertisement #<?= $this->ad->id ?></h4>
<div>
<label><?= $this->humanize('id') ?></label>
<?= $this->ad->id ?>
</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>
<?php if ($this->ad->html) : ?>
<div>
<label>Html</label>
<pre style="font-size:1.15em;margin:0px;">
<?= $this->h($this->ad->html) ?>
</pre>
</div>
<?php else: ?>
<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>
<?php endif ?>
<table>
<tbody>
<tr>
<th><label><?= $this->humanize('id') ?></label></th>
<td><?= $this->ad->id ?></td>
</tr>
<tr>
<th><label><?= $this->humanize('ad_type') ?></label></th>
<td><?= $this->ad->ad_type ?></td>
</tr>
<?php if ($this->ad->ad_type == 'horizontal') : ?>
<tr>
<th><label><?= $this->humanize('position') ?></label></th>
<td><?= $this->ad->prettyPosition() ?></td>
</tr>
<?php endif ?>
<tr>
<th><label><?= $this->humanize('status') ?></label></th>
<td><?= $this->ad->status ?></td>
</tr>
<tr>
<th><label><?= $this->humanize('hit_count') ?></label></th>
<td><?= $this->ad->hit_count ?></td>
</tr>
<tr>
<th><label><?= $this->humanize('width') ?></label></th>
<td><?= $this->ad->width ?></td>
</tr>
<tr>
<th><label><?= $this->humanize('height') ?></label></th>
<td><?= $this->ad->height ?></td>
</tr>
<?php if ($this->ad->html) : ?>
<tr>
<th><label>Html</label></th>
<pre style="font-size:1.15em;margin:0px;">
<td><?= $this->h($this->ad->html) ?></td>
</pre>
</tr>
<?php else: ?>
<tr>
<th><label><?= $this->humanize('image_url') ?></label></th>
<td><?= $this->ad->image_url ?></td>
</tr>
<tr>
<th><label><?= $this->humanize('referral_url') ?></label></th>
<td><?= $this->ad->referral_url ?></td>
</tr>
<?php endif ?>
</tbody>
</table>
<?= $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']) ?>

View File

@ -1,3 +1,8 @@
<div style="margin-bottom: 1em;">
<?= $this->print_advertisement("horizontal") ?>
<?= $this->print_advertisement(
"horizontal",
!$this->localExists('position') ? null : $this->position,
!$this->localExists('center') ? false : $this->center
)
?>
</div>

View File

@ -84,7 +84,7 @@
</div>
<?php endif ?>
<?php if (CONFIG()->can_show_ad('post#index-top', current_user())) : ?>
<?= $this->partial('horizontal') ?>
<?= $this->partial('horizontal', ['position' => 'top']) ?>
<?php endif ?>
<div id="quick-edit" style="display: none;" class="top-corner-float">
@ -104,7 +104,7 @@
</div>
<?php if (CONFIG()->can_show_ad('post#index-bottom', current_user())) : ?>
<?= $this->partial('horizontal') ?>
<?= $this->partial('horizontal', ['position' => 'bottom', 'center' => true]) ?>
<?php endif ?>
</div>
</div>

View File

@ -26,7 +26,7 @@
</div>
<div class="content" id="right-col">
<?php if (CONFIG()->can_show_ad('post#show-top', current_user())) : ?>
<?= $this->partial('horizontal') ?>
<?= $this->partial('horizontal', ['position' => 'top']) ?>
<?php endif ?>
<?= $this->partial('post/show_partials/image') ?>
@ -35,7 +35,7 @@
<?= $this->partial('post/show_partials/comments') ?>
<?php if (CONFIG()->can_show_ad('post#show-bottom', current_user())) : ?>
<?= $this->partial('horizontal') ?>
<?= $this->partial('horizontal', ['position' => 'bottom']) ?>
<?php endif ?>
</div>

View File

@ -0,0 +1,8 @@
<?php
class AddPositionToAds extends Rails\ActiveRecord\Migration\Base
{
public function up()
{
$this->addColumn('advertisements', 'position', 'char', ['null' => true, 'limit' => 1]);
}
}

View File

@ -52,6 +52,11 @@ return array (
'type' => 'text',
'default' => NULL,
),
'position' =>
array (
'type' => 'char(255)',
'default' => NULL,
),
),
1 =>
array (