Issues (80)

code/AdvertSiteTreeExtension.php (2 issues)

1
<?php
2
3
class AdvertSiteTreeExtension extends DataExtension
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...
4
{
5
    /*
6
    Any page in the site tree can have an advert category.  This is used to determine the category
7
    of adverts served in the tree of pages below it.  THe cached value is used to avoid having to
8
    recalculate it by traversing the tree each time.
9
    */
10
    public static $has_one = array(
11
        'AdvertCategory' => 'AdvertCategory',
12
        'CachedAdvertCategory' => 'AdvertCategory',
13
14
        // it is possible that a cached value may be null, so in order to avoid traversing the tree
15
        // each page load, use this flag first
16
        'IsCached' => 'Boolean',
17
    );
18
19
    /*
20
    Add a Location tab containing the map
21
    */
22
    public function updateCMSFields(FieldList $fields)
23
    {
24
        $categoryfield = new DropdownField('AdvertCategoryID', 'Category', AdvertCategory::get()->sort('Title')->map('ID', 'Title'));
25
        $categoryfield->setEmptyString('(Select one)');
26
        $fields->addFieldToTab('Root.AdvertCategory', $categoryfield);
27
    }
28
29
    /*
30
    Prior to an item being saved, check for the category having being changed.  If so we need to clear the category cache for all items in the database, and cache this one.
31
    */
32 2
    public function onBeforeWrite()
33
    {
34 2
        $savedpage = SiteTree::get()->byID($this->owner->ID);
35 2
        if ($savedpage && $savedpage->AdvertCategoryID != $this->owner->AdvertCategoryID) {
36
            // clear caching for live and stage subtree
37 2
            DB::query('update SiteTree_Live set IsCachedID = 0, CachedAdvertCategoryID = 0;');
38 2
            DB::query('update SiteTree set IsCachedID = 0, CachedAdvertCategoryID = 0;');
39 2
        }
40 2
        parent::onBeforeWrite();
41 2
    }
42
43
  /*
44
    Get the advert category.  Either use the cached advert category, or traverse the tree towards the root looking for it
45
  */
46 2
    public function CalculateAdvertCategoryID()
0 ignored issues
show
Method name "AdvertSiteTreeExtension::CalculateAdvertCategoryID" is not in camel caps format
Loading history...
47
    {
48
        // use the cached value
49 2
        $result = $this->owner->CachedAdvertCategoryID;
50
51
        // if the record is not marked as cached, see if it has a category
52 2
        if (!$this->owner->IsCached) {
53 2
            if ($this->owner->AdvertCategoryID > 0) {
54
                $this->owner->CachedAdvertCategoryID = $this->owner->AdvertCategoryID;
55
                $this->owner->IsCached = true;
56
                $this->owner->write;
57
                $result = $this->owner->CachedAdvertCategoryID;
58
            } else {
59
                // otherwise traverse each parent in turn until reaching the root of the site, i.e. no parent
60
61 2
                $parent = $this->owner->Parent();
62 2
                if ($parent) {
63 2
                    while (true) {
64 2
                        if ($parent->IsCached || $parent->AdvertCategoryID > 0) {
65 2
                            $this->owner->AdvertCategoryID = $parent->AdvertCategoryID;
66 2
                            $this->owner->IsCached = true;
67 2
                            $this->owner->write();
68 2
                            $result = $this->owner->AdvertCategoryID;
69 2
                            break;
70
                        }
71 1
                        $parent = SiteTree::get()->byId($parent->ParentID);
72 1
                        if (!$parent) {
73
                            break;
74
                        }
75 1
                    }
76 2
                }
77
            }
78 2
        }
79 2
        return $result;
80
    }
81
}
82