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

View File

@ -1,23 +1,28 @@
<?php <?php
class AdvertisementsHelper extends Rails\ActionView\Helper 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, substr($position, 0, 1));
$ad = Advertisement::random($ad_type);
if ($ad) { if ($ad) {
if ($ad->html) { if ($ad->html) {
return $ad->html; $contents = $ad->html;
} else { } else {
return $this->linkTo( $contents = $this->linkTo(
$this->imageTag( $this->imageTag(
$ad->image_url, $ad->image_url,
['alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height] ['alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height]
), ),
$this->redirectAdvertisementPath($ad), $this->redirectAdvertisementPath($ad),
['target' => '_blank'] ['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 <?php
class Advertisement extends Rails\ActiveRecord\Base class Advertisement extends Rails\ActiveRecord\Base
{ {
# Valid positions for horizontal advertisements: any, top, bottom.
static protected $POSITIONS = ['a', 't', 'b'];
protected function validations() protected function validations()
{ {
return [ return [
@ -9,13 +12,18 @@ class Advertisement extends Rails\ActiveRecord\Base
], ],
'ad_type' => [ 'presence' => true ], 'ad_type' => [ 'presence' => true ],
'status' => [ '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) static public function reset_hit_count($ids)
@ -39,13 +47,53 @@ class Advertisement extends Rails\ActiveRecord\Base
return '0'; 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() 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) { if ($this->html) {
$this->image_url = null; $this->image_url = null;
$this->referral_url = null; $this->referral_url = null;
$this->width = null;
$this->height = null;
} else { } else {
$attr = ''; $attr = '';
@ -53,10 +101,6 @@ class Advertisement extends Rails\ActiveRecord\Base
$attr = 'image_url'; $attr = 'image_url';
} elseif (!$this->referral_url) { } elseif (!$this->referral_url) {
$attr = 'referral_url'; $attr = 'referral_url';
} elseif (!$this->width) {
$attr = 'width';
} elseif (!$this->height) {
$attr = 'height';
} }
if ($attr) { if ($attr) {

View File

@ -40,20 +40,52 @@
<th><?= $f->label('ad_type') ?></th> <th><?= $f->label('ad_type') ?></th>
<td><?= $f->select('ad_type', ['Horizontal' => 'horizontal', 'Vertical' => 'vertical']) ?></td> <td><?= $f->select('ad_type', ['Horizontal' => 'horizontal', 'Vertical' => 'vertical']) ?></td>
</tr> </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> <tr>
<th><?= $f->label('status') ?></th> <th><?= $f->label('status') ?></th>
<td><?= $f->select('status', ['Active' => 'active', 'Disabled' => 'disabled']) ?></td> <td><?= $f->select('status', ['Active' => 'active', 'Disabled' => 'disabled']) ?></td>
</tr> </tr>
<?php if ($action == 'edit') : ?> <?php if (false && $action == 'edit') : // Why is this here? ?>
<tr> <tr>
<th><?= $f->label('reset_hit_count') ?></th> <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> </tr>
<?php endif ?> <?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>
<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> <tr>
<th><?= $f->label('image_url') ?></th> <th><?= $f->label('image_url') ?></th>
<td><?= $f->textField('image_url') ?></td> <td><?= $f->textField('image_url') ?></td>
@ -62,17 +94,9 @@
<th><?= $f->label('referral_url') ?></th> <th><?= $f->label('referral_url') ?></th>
<td><?= $f->textField('referral_url') ?></td> <td><?= $f->textField('referral_url') ?></td>
</tr> </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>
<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> <tr>
<th><?= $f->label('html') ?></th> <th><?= $f->label('html') ?></th>
<td><?= $f->textArea('html', ['style' => 'height: 300px;']) ?></td> <td><?= $f->textArea('html', ['style' => 'height: 300px;']) ?></td>
@ -92,7 +116,7 @@ table.form [type=text], table.form textarea{
width: 100%; width: 100%;
box-sizing: border-box; box-sizing: border-box;
} }
table.form tbody.hide { .hide-box {
display:none; display:none;
} }
</style> </style>

View File

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

View File

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

View File

@ -1,3 +1,8 @@
<div style="margin-bottom: 1em;"> <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> </div>

View File

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

View File

@ -26,7 +26,7 @@
</div> </div>
<div class="content" id="right-col"> <div class="content" id="right-col">
<?php if (CONFIG()->can_show_ad('post#show-top', current_user())) : ?> <?php if (CONFIG()->can_show_ad('post#show-top', current_user())) : ?>
<?= $this->partial('horizontal') ?> <?= $this->partial('horizontal', ['position' => 'top']) ?>
<?php endif ?> <?php endif ?>
<?= $this->partial('post/show_partials/image') ?> <?= $this->partial('post/show_partials/image') ?>
@ -35,7 +35,7 @@
<?= $this->partial('post/show_partials/comments') ?> <?= $this->partial('post/show_partials/comments') ?>
<?php if (CONFIG()->can_show_ad('post#show-bottom', current_user())) : ?> <?php if (CONFIG()->can_show_ad('post#show-bottom', current_user())) : ?>
<?= $this->partial('horizontal') ?> <?= $this->partial('horizontal', ['position' => 'bottom']) ?>
<?php endif ?> <?php endif ?>
</div> </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', 'type' => 'text',
'default' => NULL, 'default' => NULL,
), ),
'position' =>
array (
'type' => 'char(255)',
'default' => NULL,
),
), ),
1 => 1 =>
array ( array (