WebPortfolioPageTimeLine_Controller::JSONLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author Nicolaas [at] sunnysideup.co.nz
5
 * @package webportfolio
6
 * @sub-packages webportfolio
7
 *
8
 *
9
 *
10
 */
11
12
class WebPortfolioPageTimeLine extends Page
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...
13
{
14
    private static $icon = "webportfolio/images/treeicons/WebPortfolioPageTimeLine";
0 ignored issues
show
Unused Code introduced by
The property $icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
        "TimeLineHeader" => "Varchar",
18
        "TimeLineIntro" => "HTMLText",
19
        "JSON" => "Text"
20
    );
21
22
    private static $many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
        "WebPortfolioItems" => "WebPortfolioItem"
24
    );
25
26
27
    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...
28
    {
29
        $fields = parent::getCMSFields();
30
        $fields->addFieldToTab("Root.Portfolio", new TextField("TimeLineHeader", "Time line header"));
31
        $fields->addFieldToTab("Root.Portfolio", new HtmlEditorField("TimeLineIntro", "Time line intro"));
32
        $itemOptionSet = WebPortfolioItem::get();
33
        $itemOptionSetMap = ($itemOptionSet->count()) ? $itemOptionSet->map('ID', 'Title')->toArray() : array();
34
        $fields->addFieldsToTab(
35
            "Root.Portfolio",
36
            array(
37
                new LiteralField("UpdatePortfolio", "<h3>Update Portfolio</h3>"),
38
                new LiteralField("EditPortfolio", "<p><a href=\"/admin/webportfolio\" target=\"_blank\">edit portfolio</a></p>"),
39
                new LiteralField("RefreshPortfolio", "<p><a href=\"".$this->Link("json/?flush=json")."\" target=\"_blank\">clear portfolio cache</a> (portfolio data is cached to incrase loading speed)</p>"),
40
                new LiteralField("SelectPortfolio", "<h3>Select Portfolio</h3>"),
41
                new CheckboxSetField(
42
                    $name = "WebPortfolioItems",
43
                    $title = "Items shown",
44
                    $source = $itemOptionSetMap
45
                )
46
            )
47
        );
48
        return $fields;
49
    }
50
51
    public function createJSON()
52
    {
53
        SSViewer::set_source_file_comments(false);
0 ignored issues
show
Deprecated Code introduced by
The method SSViewer::set_source_file_comments() has been deprecated with message: 4.0 Use the "SSViewer.source_file_comments" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
        $json = '
55
{
56
		"timeline":
57
		{
58
				"headline":'.$this->html2json($this->TimeLineHeader).',
59
				"type":"default",
60
				"text": '.$this->html2json($this->TimeLineIntro).',
61
				"date": [';
62
        //'.$this->html2json($this->TimeLineIntro).'';//
63
        //"asset": {
64
        //    "media":"http://yourdomain_or_socialmedialink_goes_here.jpg",
65
        //    "credit":"Credit Name Goes Here",
66
        //    "caption":"Caption text goes here"
67
        //},
68
69
        $data = $this->WebPortfolioItems();
70
        if ($data && $data->count()) {
71
            $dayExistsArray = array();
0 ignored issues
show
Unused Code introduced by
$dayExistsArray is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
            foreach ($data as $site) {
73
                if ($site->StartDate) {
74
                    $startDateRaw = $site->StartDate;
75
                } else {
76
                    $startDateRaw = $this->Created;
77
                }
78
                $startDateArray = explode("-", $startDateRaw);
79
                $startDate = intval($startDateArray[0]). ",".intval($startDateArray[1]). ",".intval($startDateArray[2]);
80
                $headLine = $this->html2json($site->getHeadLine());
81
                $text = $this->html2json($site->renderWith("WebPortfolioPageOneItemTimeline")); // //
82
                $json .= '
83
						{
84
								"startDate":"'.$startDate.'",
85
								"headline": '.$headLine.',
86
								"text": '.$text.'
87
						}
88
				';
89
                if (!$site->Last()) {
90
                    $json .= ",";
91
                }
92
            }
93
        }
94
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
                        "era": [
96
                                {
97
                                        "startDate":"2011,12,10",
98
                                        "endDate":"2011,12,11",
99
                                        "headline":"Headline Goes Here",
100
                                        "text":"<p>Body text goes here, some HTML is OK</p>",
101
                                        "tag":"This is Optional"
102
                                }
103
104
                        ]
105
                        */
106
        $json .='
107
			]
108
		}
109
}';
110
        if ($json) {
111
            $this->JSON = $json;
112
            $this->writeToStage('Stage');
113
            $this->Publish('Stage', 'Live');
114
            $this->Status = "Published";
115
            $this->flushCache();
116
        }
117
        return $json;
118
    }
119
120
    protected function html2json($html)
121
    {
122
        $html = preg_replace('!\s+!', ' ', $html);
123
        if (!trim($html)) {
124
            $html = "&nbsp;";
125
        }
126
        $json = Convert::raw2json($html);
127
        return $json;
128
    }
129
}
130
131
class WebPortfolioPageTimeLine_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
132
{
133
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
134
        "json"
135
    );
136
137
    private static $ajax_file_location = "webportfolio/javascript/timeline-executive.js";
0 ignored issues
show
Unused Code introduced by
The property $ajax_file_location is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
138
139 View Code Duplication
    public function init()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        parent::init();
142
        Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
143
        if (class_exists("PrettyPhoto")) {
144
            PrettyPhoto::include_code();
145
        } else {
146
            user_error("It is recommended that you include the PrettyPhoto Module", E_USER_NOTICE);
147
        }
148
        Requirements::javascript($this->Config()->get("ajax_file_location"));
149
        Requirements::javascript("webportfolio/thirdparty/TimelineJS/compiled/js/storyjs-embed.js");
150
        Requirements::themedCSS("WebPortfolioPageTimeLine", "webportfolio");
151
    }
152
153
    public function json($request)
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...
Coding Style introduced by
json uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
154
    {
155
        SSViewer::set_source_file_comments(false);
0 ignored issues
show
Deprecated Code introduced by
The method SSViewer::set_source_file_comments() has been deprecated with message: 4.0 Use the "SSViewer.source_file_comments" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
156
        if (isset($_GET['flush']) || !$this->JSON) {
157
            return $this->createJSON();
158
        }
159
        return $this->JSON;
160
    }
161
162
    public function JSONLink()
163
    {
164
        return Director::absoluteURL($this->Link("json/"));
165
    }
166
}
167