-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCompanyListPage.php
More file actions
291 lines (249 loc) · 9.86 KB
/
Copy pathCompanyListPage.php
File metadata and controls
291 lines (249 loc) · 9.86 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
require_once 'Zend/Date.php';
class CompanyListPage extends Page
{
static $db = array();
static $has_one = array();
static $has_many = array(
'Company' => 'Company'
);
static $many_many = array(
'Donors' => 'Company'
);
//sponsor type
static $many_many_extraFields = array(
'Donors' => array(
'SortOrder' => 'Int',
)
);
function getCMSFields()
{
$fields = parent::getCMSFields();
$config = GridFieldConfig_RelationEditor::create(20);
$companiesTable = new GridField('Company', 'Company', $this->Company()->sort('Name'), $config);
$fields->addFieldToTab('Root.Companies', $companiesTable);
$config = GridFieldConfig_RelationEditor::create(20);
$config->addComponent($sort = new GridFieldSortableRows('SortOrder'));
$config->removeComponentsByType('GridFieldEditButton');
$config->removeComponentsByType('GridFieldAddNewButton');
$donorsTable = new GridField('Donors', 'Donors', $this->Donors('Name'), $config);
$fields->addFieldToTab('Root.Companies', $donorsTable);
return $fields;
}
}
class CompanyListPage_Controller extends Page_Controller
{
static $allowed_actions = array(
'profile',
'edit',
'save',
'CompanyEditForm',
);
function init()
{
parent::init();
Requirements::css("themes/openstack/css/jquery.autocomplete.css");
Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::css("themes/openstack/css/supporting-organizations.css");
Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery-ui.js');
JSChosenDependencies::renderRequirements();
JQueryValidateDependencies::renderRequirements();
Requirements::combine_files('company_list.js', array(
"themes/openstack/javascript/jquery.autocomplete.min.js",
));
}
public function EditorToolbar() {
return HtmlEditorField_Toolbar::create($this, "EditorToolbar");
}
function DisplayedCompanies($type)
{
$cache = SS_Cache::factory('cache_company_list_page');
$list = unserialize($cache->load('var_cache_company_list_page_displayed_companies_' . $type));
if (!$list) {
if ($type == 'Combined') {
$list = Company::get()->filter(array('DisplayOnSite' => 1))->filterAny(array('MemberLevel' => array('Startup', 'Silver')))->sort('Name');
} else {
$list = Company::get()->filter(array('DisplayOnSite' => 1, 'MemberLevel' => $type))->sort('Name');
}
$cache->save(serialize($list), 'var_cache_company_list_page_displayed_companies_' . $type);
}
if(!$list) $list = NULL;
return $list;
}
function MostRecent()
{
$cache = SS_Cache::factory('cache_company_list_page');
$most_recent = unserialize($cache->load('var_cache_company_list_page_most_recent'));
if(!$most_recent){
$list = Company::get()->filter(array('DisplayOnSite' => 1))->sort('Name');
$list->sort('Created');
$most_recent = $list->Last();
$cache->save(serialize($most_recent), 'var_cache_company_list_page_most_recent');
}
return $most_recent;
}
function Featured()
{
$cache = SS_Cache::factory('cache_company_list_page');
$list = unserialize($cache->load('var_cache_company_list_page_featured'));
if(!$list) {
$list = Company::get()->filter('Featured', 1)->sort('Name');
$cache->save(serialize($list), 'var_cache_company_list_page_featured');
}
return $list;
}
function getDonorsOrdered()
{
$cache = SS_Cache::factory('cache_company_list_page');
$list = unserialize($cache->load('var_cache_company_list_donors_ordered'));
if(!$list) {
$list = $this->Donors()->sort('SortOrder');
$cache->save(serialize($list), 'var_cache_company_list_donors_ordered');
}
return $list;
}
//Show the Company detail page using the CompanyListPage_show.ss template
function profile()
{
if ($Company = $this->getCompanyByURLSegment()) {
$Data = array(
'Company' => $Company
);
//return our $Data to use on the page
return $this->Customise($Data);
} else {
//Company member not found
return $this->httpError(404, 'Sorry that comapny could not be found');
}
}
// EditCompanyForm
function CompanyEditForm()
{
$current_company= $this->getCompany();
if(!$current_company){
$current_company = $this->CurrentCompany();
}
if(is_null($current_company))
return $this->httpError(404, 'Sorry that company could not be found');
$CompanyEditForm = new CompanyEditForm($this, 'CompanyEditForm',$current_company);
$CompanyEditForm->disableSecurityToken();
// Fill in the form
if ($current_company) {
Session::set('CompanyID', $current_company->ID);
$CompanyEditForm->loadDataFrom($current_company, False);
return $CompanyEditForm;
} elseif ($this->request->isPost()) {
// SS is returning to the form controller to post data
return $CompanyEditForm;
} else {
// Attempted to load the edit form, but the id was missing or didn't match an id in the database
return $this->httpError(404, 'Sorry that company could not be found');
}
}
// Save an edited company
function save($data, $form)
{
$CompanyID = Session::get('CompanyID');
// Check to see if it is set and numeric
if ($CompanyID && is_numeric($CompanyID)) {
// Try to pull the company data record by ID
$Company = Company::get()->byID($CompanyID);
$MemberID = Member::currentUserID();
$allow = $Company->CompanyAdminID == $MemberID;
if (!$allow) {
//check groups
$allow = $Company->canEditProfile() || $Company->canEditLogo();
}
// Check to see if the currently logged in member is an admin for this company
if ($allow) {
// Load the data from the form and save the edits to the company
$form->saveInto($Company);
$Company->write();
$this->setMessage('Success', 'Your edits have been saved.');
Session::clear('CompanyID');
$this->redirectBack();
} else {
$this->setMessage('Error', 'You do not seem to have permission to edit this company.');
$this->redirectBack();
}
} else {
$this->setMessage('Error', 'There was an error saving your edits.');
$this->redirectBack();
}
}
public function isCompanyAdmin()
{
if (($company = $this->getCompany()) && ($MemberID = Member::currentUserID())) {
return $company->canEditProfile() || $company->canEditLogo();
} else {
return false;
}
}
// Check to see if a member is logged in and allowed to edit this company
public function canEditCompanyProfile(){
if (($Company = $this->getCompany()) && ($MemberID = Member::currentUserID())) {
return $Company->canEditProfile() || $Company->canEditLogo();
} else {
return false;
}
}
//Get the current Company from the URL, if any
public function getCompany()
{
$params = $this->getURLParams();
if (is_numeric($params['ID']) && $Company = Company::get()->byID((int)$params['ID'])) {
Session::set('CompanyID', $Company->ID);
return $Company;
}
return null;
}
//Get the current Company from the URL, if any
public function getCompanyByURLSegment()
{
$Params = $this->getURLParams();
$Segment = convert::raw2sql($Params['ID']);
if ($Params['ID'] && $Company = Company::get()->filter('URLSegment',$Segment)->first()) {
return $Company;
}
}
//Return our custom breadcrumbs
public function Breadcrumbs()
{
//Get the default breadcrumbs
$Breadcrumbs = parent::Breadcrumbs();
if ($Company = $this->getCompany()) {
//Explode them into their individual parts
$Parts = explode(SiteTree::$breadcrumbs_delimiter, $Breadcrumbs);
//Count the parts
$NumOfParts = count($Parts);
//Change the last item to a link instead of just text
$Parts[$NumOfParts - 1] = ("<a href=\"" . $this->Link() . "\">" . $this->Title . "</a>");
//Add our extra piece on the end
$Parts[$NumOfParts] = $Company->Name;
//Return the imploded array
$Breadcrumbs = implode(SiteTree::$breadcrumbs_delimiter, $Parts);
}
return $Breadcrumbs;
}
function CurrentCompany()
{
$CompanyID = Session::get('CompanyID');
if ($CompanyID && is_numeric($CompanyID)) {
$Company = Company::get()->byID((int)$CompanyID);
return $Company;
}
return null;
}
}