added html option to advertisements

This commit is contained in:
Parziphal 2013-12-01 21:26:35 -05:00
parent ae5952fc8c
commit bfd9978032
7 changed files with 182 additions and 64 deletions

View File

@ -6,17 +6,21 @@ class AdvertisementsHelper extends Rails\ActionView\Helper
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,
['alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height]
if ($ad->html) {
return $ad->html;
} else {
return $this->contentTag("div",
$this->linkTo(
$this->imageTag(
$ad->image_url,
['alt' => "Advertisement", 'width' => $ad->width, 'height' => $ad->height]
),
$this->redirectAdvertisementPath($ad),
['target' => '_blank']
),
$this->redirectAdvertisementPath($ad),
['target' => '_blank']
),
['style' => "margin-bottom: 1em;"]
);
['style' => "margin-bottom: 1em;"]
);
}
}
}
}

View File

@ -7,12 +7,9 @@ class Advertisement extends Rails\ActiveRecord\Base
'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 ]
'ad_type' => [ 'presence' => true ],
'status' => [ 'presence' => true ],
'validateType'
];
}
@ -41,4 +38,33 @@ class Advertisement extends Rails\ActiveRecord\Base
{
return '0';
}
protected function validateType()
{
if ($this->html) {
$this->image_url = null;
$this->referral_url = null;
$this->width = null;
$this->height = null;
} else {
$attr = '';
if (!$this->image_url) {
$attr = 'image_url';
} elseif (!$this->referral_url) {
$attr = 'referral_url';
} elseif (!$this->width) {
$attr = 'width';
} elseif (!$this->height) {
$attr = 'height';
}
if ($attr) {
$this->errors()->add($attr, "can't be blank");
return false;
}
$this->html = null;
}
}
}

View File

@ -1,37 +1,98 @@
<?= $this->formFor($this->ad, function($f) { ?>
<?php $action = $this->request()->action() ?>
<?= $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>
<table class="form">
<tbody>
<tr>
<th width="150px;">Type</th>
<td>
<label for="type-image">Image</label>
<input type="radio" name="type" id="type-image" checked />
<label for="type-html">Html</label>
<input type="radio" name="type" id="type-html" />
<script>
jQuery(function(){
var $ = jQuery;
$('#type-image').on('click', function() {
if ($(this).is(':checked')) {
$('.type-fields').hide();
$('.type-image').show();
}
});
$('#type-html').on('click', function() {
if ($(this).is(':checked')) {
$('.type-fields').hide();
$('.type-html').show();
}
});
});
</script>
</td>
</tr>
</tbody>
<tbody>
<tr>
<th><?= $f->label('ad_type') ?></th>
<td><?= $f->select('ad_type', ['Horizontal' => 'horizontal', 'Vertical' => 'vertical']) ?></td>
</tr>
<tr>
<th><?= $f->label('status') ?></th>
<td><?= $f->select('status', ['Active' => 'active', 'Disabled' => 'disabled']) ?></td>
</tr>
<?php if ($action == 'edit') : ?>
<tr>
<th><?= $f->label('reset_hit_count') ?></th>
<td><?= $f->checkBox('reset_hit_count') ?></td>
</tr>
<?php endif ?>
</tbody>
<tbody class="type-fields type-image<?php if ($action != 'blank' || $f->object()->html) echo ' hide' ?>">
<tr>
<th><?= $f->label('image_url') ?></th>
<td><?= $f->textField('image_url') ?></td>
</tr>
<tr>
<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' ?>">
<tr>
<th><?= $f->label('html') ?></th>
<td><?= $f->textArea('html', ['style' => 'height: 300px;']) ?></td>
</tr>
</tbody>
<tbody>
<tr>
<td colspan="2"><?= $f->submit() ?></td>
</tr>
</tbody>
</table>
<?php }) ?>
<style>
table.form [type=text], table.form textarea{
width: 100%;
box-sizing: border-box;
}
table.form tbody.hide {
display:none;
}
</style>

View File

@ -2,4 +2,4 @@
<?= $this->partial('form', ['ad' => $this->ad]) ?>
<?= $this->linkTo($this->t('buttons.back'), $this->ad) ?>
<?= $this->linkTo($this->t('buttons.back'), '#index') ?>

View File

@ -7,7 +7,7 @@
<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>Image URL/Html</th>
<th><?= $this->humanize('referral_url') ?></th>
<th><?= $this->humanize('width') ?></th>
<th><?= $this->humanize('height') ?></th>
@ -23,7 +23,13 @@
<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><?php
if (!$ad->html) {
echo $this->linkTo($ad->image_url, $ad->image_url);
} else {
echo '<pre style="font-size:1.15em;margin:0px;">' . substr($this->h($ad->html), 0, 100) . '...</pre>';
}
?></td>
<td><?= $this->linkTo($ad->referral_url, $ad->referral_url) ?></td>
<td><?= $ad->width ?></td>
<td><?= $ad->height ?></td>

View File

@ -4,6 +4,26 @@
<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 ?>
@ -20,18 +40,7 @@
<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>
<?php endif ?>
<?= $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

@ -0,0 +1,12 @@
<?php
class AddHtmlToAdvertisements extends Rails\ActiveRecord\Migration\Base
{
public function up()
{
$this->addColumn('advertisements', 'html', 'text', ['null' => true]);
$this->changeColumn('advertisements', 'image_url', 'string', ['null' => true]);
$this->changeColumn('advertisements', 'referral_url', 'string', ['null' => true]);
$this->changeColumn('advertisements', 'width', 'integer', ['null' => true]);
$this->changeColumn('advertisements', 'height', 'integer', ['null' => true]);
}
}