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