-
Notifications
You must be signed in to change notification settings - Fork 9
/
MockChildrenController.php
181 lines (146 loc) · 5.74 KB
/
MockChildrenController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* Displays a page for creating mock children in the CMS
*
* @package silverstripe-mock-dataobjects
* @author Uncle Cheese <unclecheese@leftandmain.com>
*/
class MockChildrenController extends CMSMain
{
/**
* @var string
*/
private static $url_segment = 'pages/addmockchildren';
/**
* @var string
*/
private static $url_rule = '/$Action/$ID/$OtherID';
/**
* @var int
*/
private static $url_priority = 50;
/**
* @var string
*/
private static $menu_title = 'Add mock children';
/**
* @var string
*/
private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
/**
* @var array
*/
private static $allowed_actions = array(
'MockChildrenForm',
'doAddMockChildren',
'node'
);
/**
* The default action to show the page. Accepts the ID of the page in the ID param
*
* @param SS_HTTPRequest
* @return SSViewer
*/
public function node(SS_HTTPRequest $r)
{
return $this->getResponseNegotiator()->respond($r);
}
/**
* Builds the form for creating mock children.
*
* @return CMSForm
*/
public function MockChildrenForm()
{
$pageTypes = array();
$parentID = $this->request->param('ID') ?: $this->request->requestVar('ID');
$parentPage = SiteTree::get()->byID((int) $parentID);
if (!$parentPage) {
return false;
}
$allowed_children = $parentPage->allowedChildren();
foreach ($this->PageTypes() as $type) {
if (!empty($allowed_children) && !in_array($type->getField('ClassName'), $allowed_children)) {
continue;
}
$html = sprintf('<span class="page-icon class-%s"></span><strong class="title">%s</strong><span class="description">%s</span>',
$type->getField('ClassName'),
$type->getField('AddAction'),
$type->getField('Description')
);
$pageTypes[$type->getField('ClassName')] = $html;
}
// Ensure generic page type shows on top
if (isset($pageTypes['Page'])) {
$pageTitle = $pageTypes['Page'];
$pageTypes = array_merge(array('Page' => $pageTitle), $pageTypes);
}
$numericLabelTmpl = '<span class="step-label"><span class="flyout">%d</span><span class="arrow"></span><span class="title">%s</span></span>';
$keys = array_keys($pageTypes);
$fields = new FieldList(
$typeField = new OptionsetField(
"PageType",
sprintf($numericLabelTmpl, 1, _t('MockData.CHOOSEPAGETYPE', 'Choose the type of page to create')),
$pageTypes,
reset($keys)
),
new LiteralField('optionsheader', sprintf($numericLabelTmpl, 2, _t('MockData.CHOOSEOPTIONS', 'Choose options'))),
new NumericField('Count', _t('MockData.HOWMANYPAGES', 'How many pages do you want to create?'), 10),
new DropdownField('IncludeRelations', _t('MockData.RELATEDDATA', 'Related data'), array(
0 => _t('MockData.CREATEMOCKRELATED', 'Create mock data for relations'),
1 => _t('MockData.NATIVEONLY', 'Only populate native fields')
)),
new DropdownField('DownloadImages', _t('MockData.FILESANDIMAGES', 'Files and Images'), array(
0 => _t('MockData.USEEXISTING', 'Use existing files and images'),
1 => _t('MockData.DOWNLOADNEW', 'Download new files and images')
)),
new HiddenField('ID', '', $parentPage->ID)
);
$actions = new FieldList(
FormAction::create("doAddMockChildren", _t('CMSMain.Create', "Create"))
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
->setUseButtonTag(true)
);
$form = CMSForm::create(
$this, "MockChildrenForm", $fields, $actions
)->setHTMLID('Form_MockChildrenForm');
$form->setResponseNegotiator($this->getResponseNegotiator());
$form->addExtraClass(' stacked cms-content center cms-edit-form ' . $this->BaseCSSClasses());
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
return $form;
}
/**
* Handles the creation of mock children with {@link MockDataBuilder}
*
* @param array $data The data passed in from the form
* @param CMSForm $form The Form object that was used
* @return SSViewer
*/
public function doAddMockChildren($data, $form)
{
$parentPage = SiteTree::get()->byID((int) $data['ID']);
if (!$parentPage) {
return false;
}
$className = isset($data['PageType']) ? $data['PageType'] : "Page";
$builder = new MockDataBuilder($className);
$builder
->setCount((int) $data['Count'])
->setIncludeRelations((bool) $data['IncludeRelations'])
->setDownloadImages((bool) $data['DownloadImages'])
->setParentField("ParentID")
->setParentObj($parentPage);
try {
$ids = $builder->generate();
} catch (Exception $e) {
$form->sessionMessage($e->getMessage(), "bad");
return $this->redirectBack();
}
$this->response->addHeader(
'X-Status',
_t('MockData.CREATESUCCESS', 'Created {count} mock children under {title}', array('count' => $data['Count'], 'title' => $parentPage->Title))
);
$this->redirect(Controller::join_links(singleton('CMSPagesController')->Link()));
return $this->getResponseNegotiator()->respond($this->request);
}
}