StockistPage_Controller   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 16
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A IsStockistPage() 0 4 1
1
<?php
2
3
class StockistPage extends Page
4
{
5
6
7
    /**
8
     * @inherited
9
     */
10
    private static $icon = 'mysite/images/treeicons/StockistPage';
11
12
    /**
13
     * @inherited
14
     */
15
    private static $allowed_children = 'none';
16
17
    /**
18
     * remove from Site Tree as were using lumberjack
19
     * @var boolean
20
     */
21
    private static $show_in_sitetree = false;
22
23
    /**
24
     * @inherited
25
     */
26
    private static $can_be_root = false;
27
28
    /**
29
     * @inherited
30
     */
31
    private static $default_parent = 'StockistCountryPage';
32
33
34
35
    /**
36
     * Standard SS variable.
37
     */
38
    private static $singular_name = "Stockist Page";
39
    public function i18n_singular_name()
40
    {
41
        return "Stockist Page";
42
    }
43
44
    /**
45
     * Standard SS variable.
46
     */
47
    private static $plural_name = "Stockist Pages";
48
    public function i18n_plural_name()
49
    {
50
        return "Stockist Pages";
51
    }
52
53
    /**
54
     * @inherited
55
     */
56
    private static $description = 'Individual Stockist Page';
57
58
    /**
59
     * @inherited
60
     */
61
    private static $db = array(
62
        'Address' => 'Varchar(255)',
63
        'StreetAddress' => 'Varchar(255)',
64
        'City' => 'Varchar(255)',
65
        'WebAddress' => 'Varchar(255)',
66
        'Email' => 'Varchar(255)',
67
        'Phone' => 'Varchar(255)',
68
        'Fax' => 'Varchar(255)',
69
        'HasPhysicalStore' => 'Boolean',
70
        'HasWebStore' => 'Boolean',
71
        'DefaultZoom' => 'Int'
72
    );
73
74
    /**
75
     * @inherited
76
     */
77
    private static $has_one = array(
78
        "Image" => "Image",
79
        "Logo" => "Image"
80
    );
81
82
    /**
83
     * @inherited
84
     */
85
    private static $many_many = array(
86
        "Types" => "StockistPage_Type"
87
    );
88
89
    /**
90
     * @inherited
91
     */
92
    private static $defaults = array(
93
        'HasGeoInfo' => true,
94
        'HasPhysicalStore' => true,
95
        'HasLighting' => true,
96
        'HasJewellery' => true,
97
        'DefaultZoom' => 15
98
    );
99
100
    /**
101
     * @inherited
102
     */
103
    private static $casting = array(
104
        'CountryName' => "Varchar",
105
        'CountryCode' => "Varchar",
106
        'DistributorName' => "Varchar",
107
        'PhoneWithoutSpaces' => 'Varchar'
108
    );
109
110
    /**
111
     * @inherited
112
     */
113
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
114
    {
115
        $fields = parent::getCMSFields();
116
117
        $fields->addFieldsToTab('Root.Images', array(
118
            $imageField = new UploadField("Image", "Photo"),
119
            $logoField = new UploadField("Logo", "Logo")
120
        ));
121
        $imageField->setRightTitle("
122
            Upload 1400px wide x 630px high, at around 66% compression rate, images display up to 700px wide.
123
            This can typically be a picture of the store.
124
        ");
125
        $imageField->setFolderName("StockistImages");
126
        $logoField->setRightTitle("
127
            These are used primarily for webstores, but you can upload one for each stockist.
128
            Upload 400px wide x 185px high, at as much compression as looks good.
129
            Logos display up to 200px wide x 92.5px high.
130
            Logos should be uploaded as GIFs or PNGs (GIFs are smaller), with transparent background.
131
        ");
132
        $logoField->setFolderName("StokistLogos");
133
134
        //types
135
        $typeField = new CheckboxSetField(
136
            'Types',
137
            'Types',
138
            StockistPage_Type::get()->map()
139
        );
140
141
        $typeField->setRightTitle("What sort of customers does this shop serve?");
142
143
        $fields->addFieldsToTab('Root.Map', array(
144
            $fullAddressField = new TextField('Address'),
145
            $defaultZoomField = new NumericField('DefaultZoom'),
146
            $streetAddressField = new TextField('StreetAddress'),
147
            $cityField = new TextField('City'),
148
            $webAddress = new TextField('WebAddress', 'Web Address'),
149
            new EmailField('Email'),
150
            new TextField('Phone')
151
        ));
152
153
        $fullAddressField->setRightTitle('Full Address (including city and country)');
154
        $defaultZoomField->setRightTitle('Set between 1 and 20.  One is the whole world and twenty is highest zoom level for map.');
155
        $streetAddressField->setRightTitle('Number and Street');
156
        $webAddress->setRightTitle(' e.g. http://www.shop.com');
157
        $cityField->setRightTitle('Suburb and/or City and/or State');
158
        $fields->addFieldToTab('Root.Map', new ReadonlyField("CountryName"));
159
        $fields->addFieldToTab('Root.Map', new ReadonlyField("CountryCode"));
160
        $fields->addFieldToTab('Root.Map', new ReadonlyField("DistributorName"));
161
        if ($distributor = $this->getDistributor()) {
162
            $fields->addFieldToTab(
163
                'Root.Map',
164
                new LiteralField("MyDistributorLink", "<h5><a href=\"".$distributor->CMSEditLink()."\">edit my " . _t('Distributor.SINGULAR_NAME', 'Distributor') . ' </a></h5>')
165
            );
166
        }
167
        return $fields;
168
    }
169
170
    /**
171
     * checks the map point
172
     * @inherited
173
     */
174
    public function onBeforeWrite()
175
    {
176
        $this->createMapPoint();
177
        parent::onBeforeWrite();
178
    }
179
180
    /**
181
     * checks the map details if it has a map...
182
     */
183
    public function createMapPoint()
184
    {
185
        if ($this->HasPhysicalStore && $this->Address) {
186
            if ($map = GoogleMapLocationsObject::get()
187
                ->filter(array("ParentID" => $this->ID))
188
                ->First()
189
            ) {
190
                //do nothing;
191
            } else {
192
                $map = new GoogleMapLocationsObject();
193
            }
194
            $map->PointType = "point";
195
            $map->ParentID = $this->ID;
196
            $map->Address = $this->Address;
197
            if ($map->findGooglePointsAndWriteIfFound()) {
198
                $this->HasGeoInfo = true;
199
            } else {
200
                $this->HasGeoInfo = false;
201
            }
202
        } else {
203
            $this->HasGeoInfo = false;
204
        }
205
    }
206
207
    /**
208
     *
209
     * @return String
210
     */
211
    public function CustomAjaxInfoWindow()
212
    {
213
        return $this->renderWith("StockistAddressOnMap");
214
    }
215
216
    /**
217
     * provides a links to Google Maps to search for directions
218
     * @return String
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
219
     */
220
    public function DirectionsLink()
221
    {
222
        if ($this->Address) {
223
            return "https://www.google.com/maps/dir//".urlencode($this->Address);
224
        }
225
    }
226
227
    /**
228
     * Obscure all email links in StringField.
229
     * Matches mailto:[email protected] as well as [email protected]
230
     *
231
     * @return string
232
     */
233
    public function EncodedEmailLink()
234
    {
235
        $obj = HideMailto::convert_email($this->Email, "Enquiry from www.davidtrubridge.com");
236
        return $obj->MailTo;
237
    }
238
239
    /**
240
     * Obscure all email links in StringField.
241
     * Matches mailto:[email protected] as well as [email protected]
242
     *
243
     * @return string
244
     */
245
    public function EncodedEmailText()
246
    {
247
        $obj = HideMailto::convert_email($this->Email, "Enquiry from www.davidtrubridge.com");
248
        return $obj->Text;
249
    }
250
251
    /**
252
     * @return Distributor
253
     */
254
    public function Distributor()
255
    {
256
        return $this->getDistributor();
257
    }
258
    public function getDistributor()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
259
    {
260
        return Distributor::get_one_for_country($this->getCountryCode());
261
    }
262
263
    /**
264
     * @return Distributor
265
     */
266
    public function DistributorName()
267
    {
268
        return $this->getDistributorName();
269
    }
270
    public function getDistributorName()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
271
    {
272
        if ($distributor = $this->Distributor()) {
273
            return $distributor->Name;
274
        }
275
    }
276
277
    public function CountryName()
278
    {
279
        return $this->getCountryName();
280
    }
281
    public function getCountryName()
282
    {
283
        return EcommerceCountry::find_title($this->getCountryCode());
284
    }
285
    /**
286
     * alias for getPointValues
287
     * @return String
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
288
     */
289
    public function PointValues($fieldNameArray = 'LocalityName')
290
    {
291
        return $this->getPointValues($fieldNameArray);
292
    }
293
294
    /**
295
     * returns, for example, an array for all the cities
296
     * for a stockist (based on their Geo Locations)
297
     * NB... values are cached...
298
     *
299
     * @param string $fieldName
0 ignored issues
show
Documentation introduced by
There is no parameter named $fieldName. Did you maybe mean $fieldNameArray?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
300
     * @return array
301
     */
302
    public function getPointValues($fieldNameArray)
303
    {
304
        $safeFieldNameArray = array();
305
        foreach ($fieldNameArray as $fieldName) {
306
            $safeFieldName = Convert::raw2sql($fieldName);
307
            array_push($safeFieldNameArray, $safeFieldName);
308
        };
309
310
        $fieldNameArray = $safeFieldNameArray;
311
312
        $cachekey = "getPointField".'_'.$this->ID.'_'.implode('_', $fieldNameArray).'_'.preg_replace('/[^a-z\d]/i', '_', $this->LastEdited);
313
        $cache = SS_Cache::factory($cachekey);
314
        if (!($result = $cache->load($cachekey))) {
315
            $array = array();
316
            if ($this->HasGeoInfo) {
317
                $points = GoogleMapLocationsObject::get()
318
                    ->filter(array("ParentID" => $this->ID));
319
320
                if ($points->count()) {
321
                    foreach ($points as $point) {
322
                        $tempArray = array();
323
                        foreach ($fieldNameArray as $tempField) {
324
                            if (trim($point->$tempField) && !in_array($point->$tempField, $tempArray)) {
325
                                $tempArray[] = $point->$tempField;
326
                            }
327
                        }
328
329
                        $string = implode(', ', $tempArray);
330
                        if ($string) {
331
                            $array[$string] = $string;
332
                        }
333
                    }
334
                }
335
            }
336
            $cache->save(serialize($array), $cachekey);
337
            return $array;
338
        }
339
340
        return unserialize($result);
341
    }
342
343
    /**
344
     * @return String
345
     */
346
    public function CountryCode()
347
    {
348
        return $this->getCountryCode();
349
    }
350
    public function getCountryCode()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
351
    {
352
        $parent = StockistCountryPage::get()->byID($this->ParentID);
353
        $x = 0;
354
        while ($parent && !$parent->CountryCode && $x < 10) {
355
            $parent = StockistCountryPage::get()->byID($parent->ParentID);
356
            $x++;
357
        }
358
        if (!$parent || !$parent->CountryCode) {
359
            return EcommerceConfig::get('EcommerceCountry', 'default_country_code');
360
        }
361
        return $parent->CountryCode;
362
    }
363
364
    public function getPhoneWithoutSpaces()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
365
    {
366
        return preg_replace("/[^0-9+]/", "", $this->Phone);
367
    }
368
369
    public function types()
370
    {
371
        return "Retailer";
372
    }
373
}
374
375
class StockistPage_Controller extends Page_Controller
376
{
377
    public function init()
378
    {
379
        parent::init();
380
        $zoom = $this->DefaultZoom ? $this->DefaultZoom : 15;
381
        Config::inst()->update("GoogleMap", "default_zoom", $zoom);
382
        Config::inst()->update("GoogleMap", "title_div_id", "");
383
        $this->addMap("showpagepointsmapxml");
384
    }
385
386
    public function IsStockistPage()
387
    {
388
        return true;
389
    }
390
}
391