Issues (80)

code/Advert.php (13 issues)

1
<?php
2
3
/**
4
 * Only show a page with login when not logged in.
5
 */
6
class Advert extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    public static $searchable_fields = array(
9
        'WebsiteLink',
10
     );
11
12
    public static $db = array(
13
        // title to show in model admin
14
        'Title' => 'Varchar(255)',
15
16
        // for non adbroker ads, link to this URL
17
        'WebsiteLink' => 'Varchar(255)',
18
19
        // digital signature of some of the fields used as a means of uniquely identifying clicks
20
        // See http://www.sitepoint.com/forums/showthread.php?477567-SHA512-hash-in-php for reason of lenght 128
21
        'DigitalSignature' => 'Varchar(128)',
22
23
        // for adbroker ads
24
        'AdbrokerJavascript' => 'Text',
25
26
        // the source of the advert, either an image or adbroker JS
27
        'AdvertSource' => "Enum('UploadedImage,AdbrokerJavascript')",
28
29
        // date range valid
30
        'StartDate' => 'Datetime',
31
        'FinishDate' => 'Datetime',
32
33
        // stats
34
        'Impressions' => 'Int',
35
        'Clickthroughs' => 'Int',
36
     );
37
38
    public static $has_one = array(
39
        'AdvertImage' => 'Image',
40
        'AdvertCategory' => 'AdvertCategory',
41
        'AdvertLayoutType' => 'AdvertLayoutType',
42
    );
43
44
    public static $summary_fields = array(
45
        'Title' => 'Title',
46
        'AdvertCategory.Title',
47
        'AdvertLayoutType.Title',
48
        'StartDate' => 'StartDate',
49
        'FinishDate' => 'FinishDate',
50
    );
51
52
    // add an index in the db for the digital signature and date ranges
53
    public static $indexes = array(
54
        'DigitalSignature' => true,
55
        'StartDate' => '(StartDate)',
56
        'FinishDate' => '(FinishDate)',
57
        'AdvertCategories' => '(AdvertCategoryID)',
58
    );
59
60
    public function getCMSFields()
61
    {
62
        Requirements::javascript('adverts/javascript/advertedit.js');
63
64
        // throw away the scaffolding and start afresh
65
        $fields = new FieldList();
66
67
        // add a main tab
68
        $fields->push(new TabSet('Root', $mainTab = new Tab('Main')));
69
        $mainTab->setTitle(_t('SiteTree.TABMAIN', 'Main'));
70
71
        // human readable title
72
        $fields->addFieldToTab('Root.Main',  new TextField('Title',
0 ignored issues
show
Expected 1 space instead of 2 after comma in function call.
Loading history...
73
                                    'Human readable title for the advert'));
74
75
        // a Javascript toggle on this field displays either the adbroker text field, or an image with URL
76
        $fields->addFieldToTab('Root.Main', new DropdownField('AdvertSource', 'The type of advert',
77
            singleton('Advert')->dbObject('AdvertSource')->enumValues()
78
       ));
0 ignored issues
show
It seems like the identation of this line is off (expected at least 8 spaces, but found 7).
Loading history...
79
80
        if ($this->ID == 0) {
81
            $html = '<div class="field text">An image can be uploaded after the advert is saved for the first time</div>';
82
            $fields->addFieldToTab('Root.Main', new LiteralField('ImageInfo', $html));
83
        } else {
84
            $fields->addFieldToTab('Root.Main', $imageUploader = new UploadField('AdvertImage',
85
            'Image that will be shown as the actual advert'));
86
            Folder::find_or_make('ads');
87
            $imageUploader->setFolderName('ads'
88
            );
89
        }
90
91
        // quick tags, faster than the grid editor - these are processed prior to save to create/assign tags
92
        $fields->addFieldToTab('Root.Main',  new TextField('WebsiteLink',
0 ignored issues
show
Expected 1 space instead of 2 after comma in function call.
Loading history...
93
                                    'The URL that will be shown when the advert image is clicked'));
94
95
        $fields->addFieldToTab('Root.Main',  new TextareaField('AdbrokerJavascript',
0 ignored issues
show
Expected 1 space instead of 2 after comma in function call.
Loading history...
96
                                'JavaScript provided by the adbroker'));
97
98
        $fields->addFieldToTab('Root.Main', $sdf = new DateField('StartDate', 'The date the advert becomes active'));
99
        $fields->addFieldToTab('Root.Main', $fdf = new DateField('FinishDate', 'The date the advert becomes inactive'));
100
        $sdf->setConfig('showcalendar', true);
101
        $fdf->setConfig('showcalendar', true);
102
103
        $categoryfield = new DropdownField('AdvertCategoryID', 'Category', AdvertCategory::get()->sort('Title')->map('ID', 'Title'));
104
        $categoryfield->setEmptyString('-- Select one --');
105
        $mapping = AdvertLayoutType::get()->sort('Title')->map('ID', 'Title')->toArray();
106
        error_log(print_r($mapping,1));
107
        $layoutfield = new DropdownField('AdvertLayoutTypeID', 'Layout Type', $mapping);
108
        $layoutfield->setEmptyString('-- Select one --');
109
110
        $fields->addFieldToTab('Root.Main', $categoryfield);
111
        $fields->addFieldToTab('Root.Main', $layoutfield);
112
113
        return $fields;
114
    }
115
116 2
    public function onBeforeWrite()
117
    {
118 2
        $this->DigitalSignature = $this->CalculateDigitalSignature();
119 2
        parent::onBeforeWrite();
120 2
    }
121
122
  /*
123
    Calculate a digital signature from several of the fields
124
  */
125 2
  public function CalculateDigitalSignature()
0 ignored issues
show
Method name "Advert::CalculateDigitalSignature" is not in camel caps format
Loading history...
Line indented incorrectly; expected 4 spaces, found 2
Loading history...
126
  {
0 ignored issues
show
It seems like the identation of this line is off (expected at least 4 spaces, but found 2).
Loading history...
127
    /* because we save the impression counter on every rendition this cannot include
128
    - number of impressions
129
    - last edited
130
    Otherwise the clickthrough will fail
131
    */
132 2
    $data = $this->ID.'_'.$this->AdvertCategory()->Title.'_'.$this->AdvertLayoutType()->Title.'_'.$this->AdbrokerJavascript;
0 ignored issues
show
It seems like the identation of this line is off (expected at least 8 spaces, but found 4).
Loading history...
133 2
    $data .= '_'.$this->StartDate.'_'.$this->FinishDate.'_'.$this->ClickThroughs.'_advert';
0 ignored issues
show
It seems like the identation of this line is off (expected at least 8 spaces, but found 4).
Loading history...
134 2
    $hashed = hash('sha512', $data);
0 ignored issues
show
It seems like the identation of this line is off (expected at least 8 spaces, but found 4).
Loading history...
135
    //error_log("HASH CREATED:".$hashed);
136 2
    return $hashed;
0 ignored issues
show
It seems like the identation of this line is off (expected at least 8 spaces, but found 4).
Loading history...
137
  }
0 ignored issues
show
Line indented incorrectly; expected 4 spaces, found 2
Loading history...
138
}
139