Completed
Push — master ( 296567...51f629 )
by Robbie
01:56 queued 11s
created
src/Forms/GridFieldSiteTreeAddNewButton.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -16,7 +16,6 @@
 block discarded – undo
16 16
 use SilverStripe\Forms\HiddenField;
17 17
 use SilverStripe\ORM\ArrayList;
18 18
 use SilverStripe\View\ArrayData;
19
-use SilverStripe\View\Requirements;
20 19
 
21 20
 /**
22 21
  * This component creates a dropdown of possible page types and a button to create a new page.
Please login to merge, or discard this patch.
Indentation   +134 added lines, -134 removed lines patch added patch discarded remove patch
@@ -30,138 +30,138 @@
 block discarded – undo
30 30
  */
31 31
 class GridFieldSiteTreeAddNewButton extends GridFieldAddNewButton implements GridField_ActionProvider
32 32
 {
33
-    /**
34
-     * Determine the list of classnames and titles allowed for a given parent object
35
-     *
36
-     * @param SiteTree $parent
37
-     * @return boolean
38
-     */
39
-    public function getAllowedChildren(SiteTree $parent = null)
40
-    {
41
-        if (!$parent || !$parent->canAddChildren()) {
42
-            return array();
43
-        }
44
-
45
-        $nonHiddenPageTypes = SiteTree::page_type_classes();
46
-        $allowedChildren = $parent->allowedChildren();
47
-        $children = array();
48
-        foreach ($allowedChildren as $class) {
49
-            if (Config::inst()->get($class, 'show_in_sitetree') === false) {
50
-                $instance = Injector::inst()->get($class);
51
-                // Note: Second argument to SiteTree::canCreate will support inherited permissions
52
-                // post 3.1.12, and will default to the old permission model in 3.1.11 or below
53
-                // See http://docs.silverstripe.org/en/changelogs/3.1.11
54
-                if ($instance->canCreate(null, array('Parent' => $parent)) && in_array($class, $nonHiddenPageTypes)) {
55
-                    $children[$class] = $instance->i18n_singular_name();
56
-                }
57
-            }
58
-        }
59
-        return $children;
60
-    }
61
-
62
-    public function getHTMLFragments($gridField)
63
-    {
64
-        $state = $gridField->State->GridFieldSiteTreeAddNewButton;
65
-
66
-        $parent = SiteTree::get()->byId(Controller::curr()->currentPageID());
67
-
68
-        if ($parent) {
69
-            $state->currentPageID = $parent->ID;
70
-        }
71
-
72
-        $children = $this->getAllowedChildren($parent);
73
-        if (empty($children)) {
74
-            return array();
75
-        } elseif (count($children) > 1) {
76
-            $pageTypes = DropdownField::create('PageType', 'Page Type', $children, $parent->defaultChild());
77
-            $pageTypes
78
-                ->setFieldHolderTemplate(__CLASS__ . '_holder')
79
-                ->addExtraClass('gridfield-dropdown no-change-track');
80
-
81
-            $state->pageType = $parent->defaultChild();
82
-
83
-            if (!$this->buttonName) {
84
-                $this->buttonName = _t(
85
-                    __CLASS__ . '.AddMultipleOptions',
86
-                    'Add new',
87
-                    'Add button text for multiple options.'
88
-                );
89
-            }
90
-        } else {
91
-            $keys = array_keys($children);
92
-            $pageTypes = HiddenField::create('PageType', 'Page Type', $keys[0]);
93
-
94
-            $state->pageType = $keys[0];
95
-
96
-            if (!$this->buttonName) {
97
-                $this->buttonName = _t(
98
-                    __CLASS__ . '.Add',
99
-                    'Add new {name}',
100
-                    'Add button text for a single option.',
101
-                    ['name' => $children[$keys[0]]]
102
-                );
103
-            }
104
-        }
105
-
106
-        $addAction = GridField_FormAction::create($gridField, 'add', $this->buttonName, 'add', 'add');
107
-        $addAction
108
-            ->setAttribute('data-icon', 'add')
109
-            ->addExtraClass('no-ajax btn btn-primary font-icon-plus');
110
-
111
-        $forTemplate = ArrayData::create();
112
-        $forTemplate->Fields = ArrayList::create();
113
-        $forTemplate->Fields->push($pageTypes);
114
-        $forTemplate->Fields->push($addAction);
115
-
116
-        return [$this->targetFragment => $forTemplate->renderWith(__CLASS__)];
117
-    }
118
-
119
-    /**
120
-     * Provide actions to this component.
121
-     *
122
-     * @param  GridField $gridField
123
-     * @return array
124
-    **/
125
-    public function getActions($gridField)
126
-    {
127
-        return array('add');
128
-    }
129
-
130
-    /**
131
-     * Handles the add action, but only acts as a wrapper for {@link CMSPageAddController::doAdd()}
132
-     *
133
-     * @param GridField $gridField
134
-     * @param string $actionName
135
-     * @param mixed $arguments
136
-     * @param array $data
137
-     *
138
-     * @return HTTPResponse|null
139
-     */
140
-    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
141
-    {
142
-        if ($actionName == 'add') {
143
-            $tmpData = json_decode($data['ChildPages']['GridState'], true);
144
-            /** @skipUpgrade  */
145
-            $tmpData = $tmpData['GridFieldSiteTreeAddNewButton'];
146
-
147
-            $data = array(
148
-                'ParentID' => $tmpData['currentPageID'],
149
-                'PageType' => $tmpData['pageType']
150
-            );
151
-
152
-            /** @var $controller CMSPageAddController */
153
-            $controller = Injector::inst()->create(CMSPageAddController::class);
154
-
155
-            // pass current request to newly created controller
156
-            $request = Controller::curr()->getRequest();
157
-            $controller->setRequest($request);
158
-
159
-            $form = $controller->AddForm();
160
-            $form->loadDataFrom($data);
161
-
162
-            return $controller->doAdd($data, $form);
163
-        }
164
-
165
-        return null;
166
-    }
33
+	/**
34
+	 * Determine the list of classnames and titles allowed for a given parent object
35
+	 *
36
+	 * @param SiteTree $parent
37
+	 * @return boolean
38
+	 */
39
+	public function getAllowedChildren(SiteTree $parent = null)
40
+	{
41
+		if (!$parent || !$parent->canAddChildren()) {
42
+			return array();
43
+		}
44
+
45
+		$nonHiddenPageTypes = SiteTree::page_type_classes();
46
+		$allowedChildren = $parent->allowedChildren();
47
+		$children = array();
48
+		foreach ($allowedChildren as $class) {
49
+			if (Config::inst()->get($class, 'show_in_sitetree') === false) {
50
+				$instance = Injector::inst()->get($class);
51
+				// Note: Second argument to SiteTree::canCreate will support inherited permissions
52
+				// post 3.1.12, and will default to the old permission model in 3.1.11 or below
53
+				// See http://docs.silverstripe.org/en/changelogs/3.1.11
54
+				if ($instance->canCreate(null, array('Parent' => $parent)) && in_array($class, $nonHiddenPageTypes)) {
55
+					$children[$class] = $instance->i18n_singular_name();
56
+				}
57
+			}
58
+		}
59
+		return $children;
60
+	}
61
+
62
+	public function getHTMLFragments($gridField)
63
+	{
64
+		$state = $gridField->State->GridFieldSiteTreeAddNewButton;
65
+
66
+		$parent = SiteTree::get()->byId(Controller::curr()->currentPageID());
67
+
68
+		if ($parent) {
69
+			$state->currentPageID = $parent->ID;
70
+		}
71
+
72
+		$children = $this->getAllowedChildren($parent);
73
+		if (empty($children)) {
74
+			return array();
75
+		} elseif (count($children) > 1) {
76
+			$pageTypes = DropdownField::create('PageType', 'Page Type', $children, $parent->defaultChild());
77
+			$pageTypes
78
+				->setFieldHolderTemplate(__CLASS__ . '_holder')
79
+				->addExtraClass('gridfield-dropdown no-change-track');
80
+
81
+			$state->pageType = $parent->defaultChild();
82
+
83
+			if (!$this->buttonName) {
84
+				$this->buttonName = _t(
85
+					__CLASS__ . '.AddMultipleOptions',
86
+					'Add new',
87
+					'Add button text for multiple options.'
88
+				);
89
+			}
90
+		} else {
91
+			$keys = array_keys($children);
92
+			$pageTypes = HiddenField::create('PageType', 'Page Type', $keys[0]);
93
+
94
+			$state->pageType = $keys[0];
95
+
96
+			if (!$this->buttonName) {
97
+				$this->buttonName = _t(
98
+					__CLASS__ . '.Add',
99
+					'Add new {name}',
100
+					'Add button text for a single option.',
101
+					['name' => $children[$keys[0]]]
102
+				);
103
+			}
104
+		}
105
+
106
+		$addAction = GridField_FormAction::create($gridField, 'add', $this->buttonName, 'add', 'add');
107
+		$addAction
108
+			->setAttribute('data-icon', 'add')
109
+			->addExtraClass('no-ajax btn btn-primary font-icon-plus');
110
+
111
+		$forTemplate = ArrayData::create();
112
+		$forTemplate->Fields = ArrayList::create();
113
+		$forTemplate->Fields->push($pageTypes);
114
+		$forTemplate->Fields->push($addAction);
115
+
116
+		return [$this->targetFragment => $forTemplate->renderWith(__CLASS__)];
117
+	}
118
+
119
+	/**
120
+	 * Provide actions to this component.
121
+	 *
122
+	 * @param  GridField $gridField
123
+	 * @return array
124
+	 **/
125
+	public function getActions($gridField)
126
+	{
127
+		return array('add');
128
+	}
129
+
130
+	/**
131
+	 * Handles the add action, but only acts as a wrapper for {@link CMSPageAddController::doAdd()}
132
+	 *
133
+	 * @param GridField $gridField
134
+	 * @param string $actionName
135
+	 * @param mixed $arguments
136
+	 * @param array $data
137
+	 *
138
+	 * @return HTTPResponse|null
139
+	 */
140
+	public function handleAction(GridField $gridField, $actionName, $arguments, $data)
141
+	{
142
+		if ($actionName == 'add') {
143
+			$tmpData = json_decode($data['ChildPages']['GridState'], true);
144
+			/** @skipUpgrade  */
145
+			$tmpData = $tmpData['GridFieldSiteTreeAddNewButton'];
146
+
147
+			$data = array(
148
+				'ParentID' => $tmpData['currentPageID'],
149
+				'PageType' => $tmpData['pageType']
150
+			);
151
+
152
+			/** @var $controller CMSPageAddController */
153
+			$controller = Injector::inst()->create(CMSPageAddController::class);
154
+
155
+			// pass current request to newly created controller
156
+			$request = Controller::curr()->getRequest();
157
+			$controller->setRequest($request);
158
+
159
+			$form = $controller->AddForm();
160
+			$form->loadDataFrom($data);
161
+
162
+			return $controller->doAdd($data, $form);
163
+		}
164
+
165
+		return null;
166
+	}
167 167
 }
Please login to merge, or discard this patch.