Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1307 lines (1081 sloc) 42.444 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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
<?php
/**
* The main "content" area of the CMS.
*
* This class creates a 2-frame layout - left-tree and right-form - to sit beneath the main
* admin menu.
*
* @package cms
* @subpackage controller
* @todo Create some base classes to contain the generic functionality that will be replicated.
*/
class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionProvider {

static $url_segment = 'pages';

static $url_rule = '/$Action/$ID/$OtherID';

// Maintain a lower priority than other administration sections
// so that Director does not think they are actions of CMSMain
static $url_priority = 39;

static $menu_title = 'Edit Page';

static $menu_priority = 10;

static $tree_class = "SiteTree";

static $subitem_class = "Member";

static $allowed_actions = array(
'buildbrokenlinks',
'deleteitems',
'DeleteItemsForm',
'dialog',
'duplicate',
'duplicatewithchildren',
'publishall',
'publishitems',
'PublishItemsForm',
'submit',
'EditForm',
'SearchForm',
'SiteTreeAsUL',
'getshowdeletedsubtree',
'batchactions',
'treeview',
'listview',
'ListViewForm',
);

public function init() {
// set reading lang
if(Object::has_extension('SiteTree', 'Translatable') && !$this->request->isAjax()) {
Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages('SiteTree')));
}

parent::init();

Requirements::css(CMS_DIR . '/css/screen.css');
Requirements::customCSS($this->generatePageIconsCss());

Requirements::combine_files(
'cmsmain.js',
array_merge(
array(
CMS_DIR . '/javascript/CMSMain.js',
CMS_DIR . '/javascript/CMSMain.EditForm.js',
CMS_DIR . '/javascript/CMSMain.AddForm.js',
CMS_DIR . '/javascript/CMSPageHistoryController.js',
CMS_DIR . '/javascript/CMSMain.Tree.js',
CMS_DIR . '/javascript/SilverStripeNavigator.js',
CMS_DIR . '/javascript/SiteTreeURLSegmentField.js'
),
Requirements::add_i18n_javascript(CMS_DIR . '/javascript/lang', true, true)
)
);

CMSBatchActionHandler::register('publish', 'CMSBatchAction_Publish');
CMSBatchActionHandler::register('unpublish', 'CMSBatchAction_Unpublish');
CMSBatchActionHandler::register('delete', 'CMSBatchAction_Delete');
CMSBatchActionHandler::register('deletefromlive', 'CMSBatchAction_DeleteFromLive');
}

function index($request) {
// In case we're not showing a specific record, explicitly remove any session state,
// to avoid it being highlighted in the tree, and causing an edit form to show.
if(!$request->param('Action')) $this->setCurrentPageId(null);

return parent::index($request);
}

public function getResponseNegotiator() {
$negotiator = parent::getResponseNegotiator();
$controller = $this;
$negotiator->setCallback('ListViewForm', function() use(&$controller) {
return $controller->ListViewForm()->forTemplate();
});
return $negotiator;
}

/**
* If this is set to true, the "switchView" context in the
* template is shown, with links to the staging and publish site.
*
* @return boolean
*/
function ShowSwitchView() {
return true;
}

/**
* Overloads the LeftAndMain::ShowView. Allows to pass a page as a parameter, so we are able
* to switch view also for archived versions.
*/
function SwitchView($page = null) {
if(!$page) {
$page = $this->currentPage();
}

if($page) {
$nav = SilverStripeNavigator::get_for_record($page);
return $nav['items'];
}
}

//------------------------------------------------------------------------------------------//
// Main controllers

//------------------------------------------------------------------------------------------//
// Main UI components

/**
* Override {@link LeftAndMain} Link to allow blank URL segment for CMSMain.
*
* @return string
*/
public function Link($action = null) {
$link = Controller::join_links(
$this->stat('url_base', true),
$this->stat('url_segment', true), // in case we want to change the segment
'/', // trailing slash needed if $action is null!
"$action"
);
$this->extend('updateLink', $link);
return $link;
}

public function LinkPages() {
return singleton('CMSPagesController')->Link();
}

public function LinkPagesWithSearch() {
return $this->LinkWithSearch($this->LinkPages());
}

public function LinkTreeView() {
return $this->LinkWithSearch(singleton('CMSMain')->Link('treeview'));
}

public function LinkListView() {
return $this->LinkWithSearch(singleton('CMSMain')->Link('listview'));
}

public function LinkGalleryView() {
return $this->LinkWithSearch(singleton('CMSMain')->Link('galleryview'));
}

public function LinkPageEdit($id = null) {
if(!$id) $id = $this->currentPageID();
return $this->LinkWithSearch(
Controller::join_links(singleton('CMSPageEditController')->Link('show'), $id)
);
}

public function LinkPageSettings() {
if($id = $this->currentPageID()) {
return $this->LinkWithSearch(
Controller::join_links(singleton('CMSPageSettingsController')->Link('show'), $id)
);
}
}

public function LinkPageHistory() {
if($id = $this->currentPageID()) {
return $this->LinkWithSearch(
Controller::join_links(singleton('CMSPageHistoryController')->Link('show'), $id)
);
}
}

protected function LinkWithSearch($link) {
// Whitelist to avoid side effects
$params = array(
'q' => (array)$this->request->getVar('q'),
'ParentID' => $this->request->getVar('ParentID')
);
$link = Controller::join_links(
$link,
array_filter(array_values($params)) ? '?' . http_build_query($params) : null
);
$this->extend('updateLinkWithSearch', $link);
return $link;
}

function LinkPageAdd($extraArguments = null) {
$link = singleton("CMSPageAddController")->Link();
$this->extend('updateLinkPageAdd', $link);
if($extraArguments) $link = Controller::join_links ($link, $extraArguments);
return $link;
}

/**
* @return string
*/
public function LinkPreview() {
$record = $this->getRecord($this->currentPageID());
$baseLink = ($record && $record instanceof Page) ? $record->Link('?stage=Stage') : Director::absoluteBaseURL();
return $baseLink;
}

/**
* Return the entire site tree as a nested set of ULs
*/
public function SiteTreeAsUL() {
// Pre-cache sitetree version numbers for querying efficiency
Versioned::prepopulate_versionnumber_cache("SiteTree", "Stage");
Versioned::prepopulate_versionnumber_cache("SiteTree", "Live");
$html = $this->getSiteTreeFor($this->stat('tree_class'));

$this->extend('updateSiteTreeAsUL', $html);

return $html;
}

/**
* @return boolean
*/
public function TreeIsFiltered() {
return $this->request->getVar('q');
}

public function ExtraTreeTools() {
$html = '';
$this->extend('updateExtraTreeTools', $html);
return $html;
}

function SearchForm() {
// get all page types in a dropdown-compatible format
$pageTypeClasses = SiteTree::page_type_classes();
$pageTypes = array();
foreach ($pageTypeClasses as $pageTypeClass) {
$pageTypes[$pageTypeClass] = _t($pageTypeClass.'.SINGULARNAME', $pageTypeClass);
}
asort($pageTypes);

// get all filter instances
$filters = ClassInfo::subclassesFor('CMSSiteTreeFilter');
$filterMap = array();
// remove base class
array_shift($filters);
// add filters to map
foreach($filters as $filter) {
$filterMap[$filter] = call_user_func(array($filter, 'title'));
}
// ensure that 'all pages' filter is on top position
uasort($filterMap,
create_function('$a,$b', 'return ($a == "CMSSiteTreeFilter_Search") ? 1 : -1;')
);

$fields = new FieldList(
new TextField('q[Term]', _t('CMSSearch.FILTERLABELTEXT', 'Content')),
$dateGroup = new FieldGroup(
new HeaderField('q[Date]', _t('CMSSearch.FILTERDATEHEADING', 'Date'), 4),
$dateFrom = new DateField('q[LastEditedFrom]', _t('CMSSearch.FILTERDATEFROM', 'From')),
$dateTo = new DateField('q[LastEditedTo]', _t('CMSSearch.FILTERDATETO', 'To'))
),
new DropdownField(
'q[FilterClass]',
_t('CMSMain.PAGES', 'Pages'),
$filterMap
),
$classDropdown = new DropdownField(
'q[ClassName]',
_t('CMSMain.PAGETYPEOPT','Page Type', 'Dropdown for limiting search to a page type'),
$pageTypes
)
// new TextField('MetaTags', _t('CMSMain.SearchMetaTags', 'Meta tags'))
);
$dateGroup->setFieldHolderTemplate('FieldGroup_DefaultFieldHolder')->addExtraClass('stacked');
$dateFrom->setConfig('showcalendar', true);
$dateTo->setConfig('showcalendar', true);
$classDropdown->setEmptyString(_t('CMSMain.PAGETYPEANYOPT','Any'));

$actions = new FieldList(
FormAction::create('doSearch', _t('CMSMain_left.ss.APPLY FILTER', 'Apply Filter'))
->addExtraClass('ss-ui-action-constructive'),
Object::create('ResetFormAction', 'clear', _t('CMSMain_left.ss.RESET', 'Reset'))
);

// Use <button> to allow full jQuery UI styling
foreach($actions->dataFields() as $action) $action->setUseButtonTag(true);

$form = Form::create($this, 'SearchForm', $fields, $actions)
->addExtraClass('cms-search-form')
->setFormMethod('GET')
->setFormAction($this->Link())
->disableSecurityToken()
->unsetValidator();
$form->loadDataFrom($this->request->getVars());

$this->extend('updateSearchForm', $form);
return $form;
}

function doSearch($data, $form) {
return $this->getsubtree($this->request);
}

/**
* @return ArrayList
*/
public function Breadcrumbs($unlinked = false) {
$items = parent::Breadcrumbs($unlinked);

// The root element should point to the pages tree view,
// rather than the actual controller (which would just show an empty edit form)
$defaultTitle = self::menu_title_for_class('CMSPagesController');
$items[0]->Title = _t("{$this->class}.MENUTITLE", $defaultTitle);
$items[0]->Link = singleton('CMSPagesController')->Link();

return $items;
}

/**
* Create serialized JSON string with site tree hints data to be injected into
* 'data-hints' attribute of root node of jsTree.
*
* @return String Serialized JSON
*/
public function SiteTreeHints() {
$json = '';

$classes = ClassInfo::subclassesFor( $this->stat('tree_class') );

$cacheCanCreate = array();
foreach($classes as $class) $cacheCanCreate[$class] = singleton($class)->canCreate();

// Generate basic cache key. Too complex to encompass all variations
$cache = SS_Cache::factory('CMSMain_SiteTreeHints');
$cacheKey = md5(implode('_', array(Member::currentUserID(), implode(',', $cacheCanCreate), implode(',', $classes))));
if($this->request->getVar('flush')) $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
$json = $cache->load($cacheKey);
if(!$json) {
$def['Root'] = array();
$def['Root']['disallowedParents'] = array();

foreach($classes as $class) {
$obj = singleton($class);
if($obj instanceof HiddenClass) continue;

$allowedChildren = $obj->allowedChildren();

// SiteTree::allowedChildren() returns null rather than an empty array if SiteTree::allowed_chldren == 'none'
if($allowedChildren == null) $allowedChildren = array();

// Exclude SiteTree from possible Children
$possibleChildren = array_diff($allowedChildren, array("SiteTree"));

// Find i18n - names and build allowed children array
foreach($possibleChildren as $child) {
$instance = singleton($child);

if($instance instanceof HiddenClass) continue;

if(!$cacheCanCreate[$child]) continue;

// skip this type if it is restricted
if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue;

$title = $instance->i18n_singular_name();

$def[$class]['allowedChildren'][] = array("ssclass" => $child, "ssname" => $title);
}

$allowedChildren = array_keys(array_diff($classes, $allowedChildren));
if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren;
$defaultChild = $obj->defaultChild();
if($defaultChild != 'Page' && $defaultChild != null) $def[$class]['defaultChild'] = $defaultChild;
$defaultParent = $obj->defaultParent();
$parent = SiteTree::get_by_link($defaultParent);
$id = $parent ? $parent->id : null;
if ($defaultParent != 1 && $defaultParent != null) $def[$class]['defaultParent'] = $defaultParent;
if(isset($def[$class]['disallowedChildren'])) {
foreach($def[$class]['disallowedChildren'] as $disallowedChild) {
$def[$disallowedChild]['disallowedParents'][] = $class;
}
}

// Are any classes allowed to be parents of root?
$def['Root']['disallowedParents'][] = $class;
}

$json = Convert::raw2xml(Convert::raw2json($def));
$cache->save($json, $cacheKey);
}
return $json;
}

/**
* Include CSS for page icons. We're not using the JSTree 'types' option
* because it causes too much performance overhead just to add some icons.
*
* @return String CSS
*/
public function generatePageIconsCss() {
$css = '';

$classes = ClassInfo::subclassesFor('SiteTree');
foreach($classes as $class) {
$obj = singleton($class);
$iconSpec = $obj->stat('icon');

if(!$iconSpec) continue;

// Legacy support: We no longer need separate icon definitions for folders etc.
$iconFile = (is_array($iconSpec)) ? $iconSpec[0] : $iconSpec;

// Legacy support: Add file extension if none exists
if(!pathinfo($iconFile, PATHINFO_EXTENSION)) $iconFile .= '-file.gif';

$iconPathInfo = pathinfo($iconFile);

// Base filename
$baseFilename = $iconPathInfo['dirname'] . '/' . $iconPathInfo['filename'];
$fileExtension = $iconPathInfo['extension'];

$selector = ".page-icon.class-$class, li.class-$class > a .jstree-pageicon";

if(Director::fileExists($iconFile)) {
$css .= "$selector { background: transparent url('$iconFile') 0 0 no-repeat; }\n";
} else {
// Support for more sophisticated rules, e.g. sprited icons
$css .= "$selector { $iconFile }\n";
}
}

return $css;
}

/**
* Populates an array of classes in the CMS
* which allows the user to change the page type.
*
* @return SS_List
*/
public function PageTypes() {
$classes = SiteTree::page_type_classes();

$result = new ArrayList();

foreach($classes as $class) {
$instance = singleton($class);

if($instance instanceof HiddenClass) continue;

if(!$instance->canCreate()) continue;

// skip this type if it is restricted
if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue;

$addAction = $instance->i18n_singular_name();

// Get description (convert 'Page' to 'SiteTree' for correct localization lookups)
$description = _t((($class == 'Page') ? 'SiteTree' : $class) . '.DESCRIPTION');

if(!$description) {
$description = $instance->uninherited('description');
}

if($class == 'Page' && !$description) {
$description = singleton('SiteTree')->uninherited('description');
}

$result->push(new ArrayData(array(
'ClassName' => $class,
'AddAction' => $addAction,
'Description' => $description,
// TODO Sprite support
'IconURL' => $instance->stat('icon'),
'Title' => singleton($class)->i18n_singular_name(),
)));
}

$result = $result->sort('AddAction');

return $result;
}

/**
* Get a database record to be managed by the CMS.
*
* @param int $id Record ID
* @param int $versionID optional Version id of the given record
*/
  public function getRecord($id, $versionID = null) {
$treeClass = $this->stat('tree_class');

if($id instanceof $treeClass) {
return $id;
}
else if($id && is_numeric($id)) {
if($this->request->getVar('Version')) {
$versionID = (int) $this->request->getVar('Version');
}

if($versionID) {
$record = Versioned::get_version($treeClass, $id, $versionID);
} else {
$record = DataObject::get_one($treeClass, "\"$treeClass\".\"ID\" = $id");
}

// Then, try getting a record from the live site
if(!$record) {
// $record = Versioned::get_one_by_stage($treeClass, "Live", "\"$treeClass\".\"ID\" = $id");
Versioned::reading_stage('Live');
singleton($treeClass)->flushCache();

$record = DataObject::get_one( $treeClass, "\"$treeClass\".\"ID\" = $id");
if($record) Versioned::set_reading_mode('');
}

// Then, try getting a deleted record
if(!$record) {
$record = Versioned::get_latest_version($treeClass, $id);
}

// Don't open a page from a different locale
/** The record's Locale is saved in database in 2.4, and not related with Session,
* we should not check their locale matches the Translatable::get_current_locale,
* here as long as we all the HTTPRequest is init with right locale.
* This bit breaks the all FileIFrameField functions if the field is used in CMS
* and its relevent ajax calles, like loading the tree dropdown for TreeSelectorField.
*/
/* if($record && Object::has_extension('SiteTree', 'Translatable') && $record->Locale && $record->Locale != Translatable::get_current_locale()) {
$record = null;
}*/

return $record;

} else if(substr($id,0,3) == 'new') {
return $this->getNewItem($id);
}
}

/**
* @param Int $id
* @param FieldList $fields
* @return Form
*/
public function getEditForm($id = null, $fields = null) {

if(!$id) $id = $this->currentPageID();
$form = parent::getEditForm($id);

// TODO Duplicate record fetching (see parent implementation)
$record = $this->getRecord($id);
if($record && !$record->canView()) return Security::permissionFailure($this);

if(!$fields) $fields = $form->Fields();
$actions = $form->Actions();

if($record) {
$deletedFromStage = $record->IsDeletedFromStage;
$deleteFromLive = !$record->ExistsOnLive;

$fields->push($idField = new HiddenField("ID", false, $id));
// Necessary for different subsites
$fields->push($liveLinkField = new HiddenField("AbsoluteLink", false, $record->AbsoluteLink()));
$fields->push($liveLinkField = new HiddenField("LiveLink"));
$fields->push($stageLinkField = new HiddenField("StageLink"));
$fields->push(new HiddenField("TreeTitle", false, $record->TreeTitle));

if($record->ID && is_numeric( $record->ID ) ) {
$liveLink = $record->getAbsoluteLiveLink();
if($liveLink) $liveLinkField->setValue($liveLink);
if(!$deletedFromStage) {
$stageLink = Controller::join_links($record->AbsoluteLink(), '?stage=Stage');
if($stageLink) $stageLinkField->setValue($stageLink);
}
}

// Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load
if(in_array('CMSPreviewable', class_implements($record)) && !$fields->fieldByName('SilverStripeNavigator')) {
$navField = new LiteralField('SilverStripeNavigator', $this->getSilverStripeNavigator());
$navField->setAllowHTML(true);
$fields->push($navField);
}

// getAllCMSActions can be used to completely redefine the action list
if($record->hasMethod('getAllCMSActions')) {
$actions = $record->getAllCMSActions();
} else {
$actions = $record->getCMSActions();
}

// Use <button> to allow full jQuery UI styling
$actionsFlattened = $actions->dataFields();
if($actionsFlattened) foreach($actionsFlattened as $action) $action->setUseButtonTag(true);

if($record->hasMethod('getCMSValidator')) {
$validator = $record->getCMSValidator();
} else {
$validator = new RequiredFields();
}

$form = new Form($this, "EditForm", $fields, $actions, $validator);
$form->loadDataFrom($record);
$form->disableDefaultAction();
$form->addExtraClass('cms-edit-form');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// TODO Can't merge $FormAttributes in template at the moment
$form->addExtraClass('center ' . $this->BaseCSSClasses());
// if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
$form->setAttribute('data-pjax-fragment', 'CurrentForm');

if(!$record->canEdit() || $deletedFromStage) {
$readonlyFields = $form->Fields()->makeReadonly();
$form->setFields($readonlyFields);
}

$this->extend('updateEditForm', $form);
return $form;
} else if($id) {
return new Form($this, "EditForm", new FieldList(
new LabelField('PageDoesntExistLabel',_t('CMSMain.PAGENOTEXISTS',"This page doesn't exist"))), new FieldList()
);
}
}

/**
* @return String HTML
*/
public function treeview($request) {
return $this->renderWith($this->getTemplatesWithSuffix('_TreeView'));
}

public function listview($request) {
return $this->renderWith($this->getTemplatesWithSuffix('_ListView'));
}

/**
* Returns the pages meet a certain criteria as {@see CMSSiteTreeFilter} or the subpages of a parent page
* defaulting to no filter and show all pages in first level.
* Doubles as search results, if any search parameters are set through {@link SearchForm()}.
*
* @param Array Search filter criteria
* @param Int Optional parent node to filter on (can't be combined with other search criteria)
* @return SS_List
*/
public function getList($params, $parentID = 0) {
$list = new DataList($this->stat('tree_class'));
$filter = null;
$ids = array();
if(isset($params['FilterClass']) && $filterClass = $params['FilterClass']){
if(!is_subclass_of($filterClass, 'CMSSiteTreeFilter')) {
throw new Exception(sprintf('Invalid filter class passed: %s', $filterClass));
}
$filter = new $filterClass($params);
$filterOn = true;
foreach($pages=$filter->pagesIncluded() as $pageMap){
$ids[] = $pageMap['ID'];
}
if(count($ids)) $list = $list->where('"'.$this->stat('tree_class').'"."ID" IN ('.implode(",", $ids).')');
} else {
$list = $list->filter("ParentID", is_numeric($parentID) ? $parentID : 0);
}

return $list;
}

public function ListViewForm() {
$params = $this->request->requestVar('q');
$list = $this->getList($params, $parentID = $this->request->requestVar('ParentID'));
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(15)
);
if($parentID){
$gridFieldConfig->addComponent(
GridFieldLevelup::create($parentID)
->setLinkSpec('?ParentID=%d&view=list')
->setAttributes(array('data-pjax' => 'ListViewForm,Breadcrumbs'))
);
}
$gridField = new GridField('Page','Pages', $list, $gridFieldConfig);
$columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns');

// Don't allow navigating into children nodes on filtered lists
$fields = array(
'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'),
'Created' => _t('SiteTree.CREATED', 'Date Created'),
'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'),
);
$gridField->getConfig()->getComponentByType('GridFieldSortableHeader')->setFieldSorting(array('getTreeTitle' => 'Title'));

if(!$params) {
$fields = array_merge(array('listChildrenLink' => ''), $fields);
}

$columns->setDisplayFields($fields);
$columns->setFieldCasting(array(
'Created' => 'Datetime->Ago',
'LastEdited' => 'Datetime->Ago',
'getTreeTitle' => 'HTMLText'
));

$controller = $this;
$columns->setFieldFormatting(array(
'listChildrenLink' => function($value, &$item) use($controller) {
$num = $item ? $item->numChildren() : null;
if($num) {
return sprintf(
'<a class="cms-panel-link list-children-link" data-pjax-target="ListViewForm,Breadcrumbs" href="%s">%s</a>',
Controller::join_links($controller->Link(), "?ParentID={$item->ID}&view=list"),
$num
);
}
},
'getTreeTitle' => function($value, &$item) use($controller) {
return '<a class="action-detail" href="' . singleton('CMSPageEditController')->Link('show') . '/' . $item->ID . '">' . $item->TreeTitle . '</a>';
}
));

$listview = new Form(
$this,
'ListViewForm',
new FieldList($gridField),
new FieldList()
);
$listview->setAttribute('data-pjax-fragment', 'ListViewForm');

$this->extend('updateListView', $listview);

$listview->disableSecurityToken();
return $listview;
}

public function currentPageID() {
$id = parent::currentPageID();

// Fall back to homepage record
if(!$id) {
$homepageSegment = RootURLController::get_homepage_link();
$homepageRecord = DataObject::get_one('SiteTree', sprintf('"URLSegment" = \'%s\'', $homepageSegment));
if($homepageRecord) $id = $homepageRecord->ID;
}

return $id;
}

//------------------------------------------------------------------------------------------//
// Data saving handlers

/**
* Save and Publish page handler
*/
public function save($data, $form) {
$className = $this->stat('tree_class');

// Existing or new record?
$SQL_id = Convert::raw2sql($data['ID']);
if(substr($SQL_id,0,3) != 'new') {
$record = DataObject::get_by_id($className, $SQL_id);
if($record && !$record->canEdit()) return Security::permissionFailure($this);
if(!$record || !$record->ID) throw new SS_HTTPResponse_Exception("Bad record ID #$SQL_id", 404);
} else {
if(!singleton($this->stat('tree_class'))->canCreate()) return Security::permissionFailure($this);
$record = $this->getNewItem($SQL_id, false);
}

// TODO Coupling to SiteTree
$record->HasBrokenLink = 0;
$record->HasBrokenFile = 0;

if (!$record->ObsoleteClassName) $record->writeWithoutVersion();

// Update the class instance if necessary
if(isset($data['ClassName']) && $data['ClassName'] != $record->ClassName) {
$newClassName = $record->ClassName;
// The records originally saved attribute was overwritten by $form->saveInto($record) before.
// This is necessary for newClassInstance() to work as expected, and trigger change detection
// on the ClassName attribute
$record->setClassName($data['ClassName']);
// Replace $record with a new instance
$record = $record->newClassInstance($newClassName);
}

// save form data into record
$form->saveInto($record);
$record->write();

// If the 'Save & Publish' button was clicked, also publish the page
if (isset($data['publish']) && $data['publish'] == 1) {
$record->doPublish();

// Update classname with original and get new instance (see above for explanation)
if(isset($data['ClassName'])) {
$record->setClassName($data['ClassName']);
$publishedRecord = $record->newClassInstance($record->ClassName);
}

$this->response->addHeader(
'X-Status',
rawurlencode(_t(
'LeftAndMain.STATUSPUBLISHEDSUCCESS',
"Published '{title}' successfully",
'Status message after publishing a page, showing the page title',
array('title' => $record->Title)
))
);
} else {
$this->response->addHeader('X-Status', rawurlencode(_t('LeftAndMain.SAVEDUP', 'Saved.')));
}

return $this->getResponseNegotiator()->respond($this->request);
}

/**
* @uses LeftAndMainExtension->augmentNewSiteTreeItem()
*/
public function getNewItem($id, $setID = true) {
list($dummy, $className, $parentID, $suffix) = array_pad(explode('-',$id),4,null);

$newItem = new $className();

if( !$suffix ) {
$sessionTag = "NewItems." . $parentID . "." . $className;
     if(Session::get($sessionTag)) {
$suffix = '-' . Session::get($sessionTag);
Session::set($sessionTag, Session::get($sessionTag) + 1);
}
else
Session::set($sessionTag, 1);

$id = $id . $suffix;
}

$newItem->Title = _t(
'CMSMain.NEWPAGE',
"New {pagetype}",'followed by a page type title',
array('pagetype' => singleton($className)->i18n_singular_name())
);
$newItem->ClassName = $className;
$newItem->ParentID = $parentID;

// DataObject::fieldExists only checks the current class, not the hierarchy
// This allows the CMS to set the correct sort value
if($newItem->castingHelper('Sort')) {
$newItem->Sort = DB::query("SELECT MAX(\"Sort\") FROM \"SiteTree\" WHERE \"ParentID\" = '" . Convert::raw2sql($parentID) . "'")->value() + 1;
}

if($setID) $newItem->ID = $id;

# Some modules like subsites add extra fields that need to be set when the new item is created
$this->extend('augmentNewSiteTreeItem', $newItem);

return $newItem;
}

/**
* Delete the page from live. This means a page in draft mode might still exist.
*
* @see delete()
*/
public function deletefromlive($data, $form) {
Versioned::reading_stage('Live');
$record = DataObject::get_by_id("SiteTree", $data['ID']);
if($record && !($record->canDelete() && $record->canDeleteFromLive())) return Security::permissionFailure($this);

$descRemoved = '';
$descendantsRemoved = 0;
$recordTitle = $record->Title;
$recordID = $record->ID;

// before deleting the records, get the descendants of this tree
if($record) {
$descendantIDs = $record->getDescendantIDList();

// then delete them from the live site too
$descendantsRemoved = 0;
foreach( $descendantIDs as $descID )
if( $descendant = DataObject::get_by_id('SiteTree', $descID) ) {
$descendant->doDeleteFromLive();
$descendantsRemoved++;
}

// delete the record
$record->doDeleteFromLive();
}

Versioned::reading_stage('Stage');

if(isset($descendantsRemoved)) {
$descRemoved = " and $descendantsRemoved descendants";
$descRemoved = ' ' . _t('CMSMain.DESCREMOVED', 'and {count} descendants', array('count' => $descendantsRemoved));
} else {
$descRemoved = '';
}

$this->response->addHeader(
'X-Status',
rawurlencode(sprintf(_t('CMSMain.REMOVED', 'Deleted \'%s\'%s from live site'), $recordTitle, $descRemoved))
);

// Even if the record has been deleted from stage and live, it can be viewed in "archive mode"
return $this->getResponseNegotiator()->respond($this->request);
}

/**
* Actually perform the publication step
*/
public function performPublish($record) {
if($record && !$record->canPublish()) return Security::permissionFailure($this);

$record->doPublish();
}

/**
* Reverts a page by publishing it to live.
* Use {@link restorepage()} if you want to restore a page
* which was deleted from draft without publishing.
*
* @uses SiteTree->doRevertToLive()
*/
public function revert($data, $form) {
if(!isset($data['ID'])) return new SS_HTTPResponse("Please pass an ID in the form content", 400);

$id = (int) $data['ID'];
$restoredPage = Versioned::get_latest_version("SiteTree", $id);
if(!$restoredPage) return new SS_HTTPResponse("SiteTree #$id not found", 400);

$record = Versioned::get_one_by_stage(
'SiteTree',
'Live',
sprintf("\"SiteTree_Live\".\"ID\" = '%d'", (int)$data['ID'])
);

// a user can restore a page without publication rights, as it just adds a new draft state
// (this action should just be available when page has been "deleted from draft")
if($record && !$record->canEdit()) return Security::permissionFailure($this);
if(!$record || !$record->ID) throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404);

$record->doRevertToLive();

$this->response->addHeader(
'X-Status',
rawurlencode(_t(
'CMSMain.RESTORED',
"Restored '{title}' successfully",
'Param %s is a title',
array('title' => $record->Title)
))
);

return $this->getResponseNegotiator()->respond($this->request);
}

/**
* Delete the current page from draft stage.
* @see deletefromlive()
*/
public function delete($data, $form) {
$id = Convert::raw2sql($data['ID']);
$record = DataObject::get_one(
"SiteTree",
sprintf("\"SiteTree\".\"ID\" = %d", $id)
);
if($record && !$record->canDelete()) return Security::permissionFailure();
if(!$record || !$record->ID) throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404);

// save ID and delete record
$recordID = $record->ID;
$record->delete();

$this->response->addHeader(
'X-Status',
rawurlencode(sprintf(_t('CMSMain.REMOVEDPAGEFROMDRAFT',"Removed '%s' from the draft site"), $record->Title))
);

// Even if the record has been deleted from stage and live, it can be viewed in "archive mode"
return $this->getResponseNegotiator()->respond($this->request);
}

function publish($data, $form) {
$data['publish'] = '1';

return $this->save($data, $form);
}

function unpublish($data, $form) {
$className = $this->stat('tree_class');
$record = DataObject::get_by_id($className, $data['ID']);

if($record && !$record->canDeleteFromLive()) return Security::permissionFailure($this);
if(!$record || !$record->ID) throw new SS_HTTPResponse_Exception("Bad record ID #" . (int)$data['ID'], 404);

$record->doUnpublish();

$this->response->addHeader(
'X-Status',
rawurlencode(_t('CMSMain.REMOVEDPAGE',"Removed '{title}' from the published site", array('title' => $record->Title)))
);

return $this->getResponseNegotiator()->respond($this->request);
}

/**
* @return array
*/
function rollback() {
return $this->doRollback(array(
'ID' => $this->currentPageID(),
'Version' => $this->request->param('VersionID')
), null);
}

/**
* Rolls a site back to a given version ID
*
* @param array
* @param Form
*
* @return html
*/
function doRollback($data, $form) {
$this->extend('onBeforeRollback', $data['ID']);

$id = (isset($data['ID'])) ? (int) $data['ID'] : null;
$version = (isset($data['Version'])) ? (int) $data['Version'] : null;

$record = DataObject::get_by_id($this->stat('tree_class'), $id);
if($record && !$record->canEdit()) return Security::permissionFailure($this);

if($version) {
$record->doRollbackTo($version);
$message = _t(
'CMSMain.ROLLEDBACKVERSION',
"Rolled back to version #%d. New version number is #%d",
array('version' => $data['Version'], 'versionnew' => $record->Version)
);
} else {
$record->doRollbackTo('Live');
$message = _t(
'CMSMain.ROLLEDBACKPUB',"Rolled back to published version. New version number is #{version}",
array('version' => $record->Version)
);
}

$this->response->addHeader('X-Status', rawurlencode($message));

// Can be used in different contexts: In normal page edit view, in which case the redirect won't have any effect.
// Or in history view, in which case a revert causes the CMS to re-load the edit view.
// The X-Pjax header forces a "full" content refresh on redirect.
$url = Controller::join_links(singleton('CMSPageEditController')->Link('show'), $record->ID);
$this->response->addHeader('X-ControllerURL', $url);
$this->request->addHeader('X-Pjax', 'Content');
$this->response->addHeader('X-Pjax', 'Content');

return $this->getResponseNegotiator()->respond($this->request);
}

/**
* Batch Actions Handler
*/
function batchactions() {
return new CMSBatchActionHandler($this, 'batchactions');
}

function BatchActionParameters() {
$batchActions = CMSBatchActionHandler::$batch_actions;

$forms = array();
foreach($batchActions as $urlSegment => $batchAction) {
$SNG_action = singleton($batchAction);
if ($SNG_action->canView() && $fieldset = $SNG_action->getParameterFields()) {
$formHtml = '';
foreach($fieldset as $field) {
$formHtml .= $field->Field();
}
$forms[$urlSegment] = $formHtml;
}
}
$pageHtml = '';
foreach($forms as $urlSegment => $html) {
$pageHtml .= "<div class=\"params\" id=\"BatchActionParameters_$urlSegment\">$html</div>\n\n";
}
return new LiteralField("BatchActionParameters", '<div id="BatchActionParameters" style="display:none">'.$pageHtml.'</div>');
}
/**
* Returns a list of batch actions
*/
function BatchActionList() {
return $this->batchactions()->batchActionList();
}

function buildbrokenlinks($request) {
// Protect against CSRF on destructive action
if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400);

increase_time_limit_to();
increase_memory_limit_to();

if($this->urlParams['ID']) {
$newPageSet[] = DataObject::get_by_id("Page", $this->urlParams['ID']);
} else {
$pages = DataObject::get("Page");
foreach($pages as $page) $newPageSet[] = $page;
$pages = null;
}

$content = new HtmlEditorField('Content');
$download = new HtmlEditorField('Download');

foreach($newPageSet as $i => $page) {
$page->HasBrokenLink = 0;
$page->HasBrokenFile = 0;

$content->setValue($page->Content);
$content->saveInto($page);

$download->setValue($page->Download);
$download->saveInto($page);

echo "<li>$page->Title (link:$page->HasBrokenLink, file:$page->HasBrokenFile)";

$page->writeWithoutVersion();
$page->destroy();
$newPageSet[$i] = null;
}
}

function publishall($request) {
if(!Permission::check('ADMIN')) return Security::permissionFailure($this);

increase_time_limit_to();
increase_memory_limit_to();

$response = "";

if(isset($this->requestParams['confirm'])) {
// Protect against CSRF on destructive action
if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400);

$start = 0;
$pages = DataObject::get("SiteTree", "", "", "", "$start,30");
$count = 0;
while($pages) {
foreach($pages as $page) {
if($page && !$page->canPublish()) return Security::permissionFailure($this);

$page->doPublish();
$page->destroy();
unset($page);
$count++;
$response .= "<li>$count</li>";
}
if($pages->Count() > 29) {
$start += 30;
$pages = DataObject::get("SiteTree", "", "", "", "$start,30");
} else {
break;
}
}
$response .= _t('CMSMain.PUBPAGES',"Done: Published {count} pages", array('count' => $count));

} else {
$token = SecurityToken::inst();
$fields = new FieldList();
$token->updateFieldSet($fields);
$tokenField = $fields->First();
$tokenHtml = ($tokenField) ? $tokenField->FieldHolder() : '';
$response .= '<h1>' . _t('CMSMain.PUBALLFUN','"Publish All" functionality') . '</h1>
<p>' . _t('CMSMain.PUBALLFUN2', 'Pressing this button will do the equivalent of going to every page and pressing "publish". It\'s
intended to be used after there have been massive edits of the content, such as when the site was
first built.') . '</p>
<form method="post" action="publishall">
<input type="submit" name="confirm" value="'
. _t('CMSMain.PUBALLCONFIRM',"Please publish every page in the site, copying content stage to live",'Confirmation button') .'" />'
. $tokenHtml .
'</form>';
}

return $response;
}

/**
* Restore a completely deleted page from the SiteTree_versions table.
*/
function restore($data, $form) {
if(!isset($data['ID']) || !is_numeric($data['ID'])) {
return new SS_HTTPResponse("Please pass an ID in the form content", 400);
}

$id = (int)$data['ID'];
$restoredPage = Versioned::get_latest_version("SiteTree", $id);
if(!$restoredPage) return new SS_HTTPResponse("SiteTree #$id not found", 400);

$restoredPage = $restoredPage->doRestoreToStage();

$this->response->addHeader(
'X-Status',
rawurlencode(_t(
'CMSMain.RESTORED',
"Restored '{title}' successfully",
array('title' => $restoredPage->TreeTitle)
))
);

return $this->getResponseNegotiator()->respond($this->request);
}

function duplicate($request) {
// Protect against CSRF on destructive action
if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400);

if(($id = $this->urlParams['ID']) && is_numeric($id)) {
$page = DataObject::get_by_id("SiteTree", $id);
if($page && (!$page->canEdit() || !$page->canCreate())) return Security::permissionFailure($this);
if(!$page || !$page->ID) throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404);

$newPage = $page->duplicate();

// ParentID can be hard-set in the URL. This is useful for pages with multiple parents
if($_GET['parentID'] && is_numeric($_GET['parentID'])) {
$newPage->ParentID = $_GET['parentID'];
$newPage->write();
}

// Reload form, data and actions might have changed
$form = $this->getEditForm($newPage->ID);

return $form->forTemplate();
} else {
user_error("CMSMain::duplicate() Bad ID: '$id'", E_USER_WARNING);
}
}

function duplicatewithchildren($request) {
// Protect against CSRF on destructive action
if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400);

if(($id = $this->urlParams['ID']) && is_numeric($id)) {
$page = DataObject::get_by_id("SiteTree", $id);
if($page && (!$page->canEdit() || !$page->canCreate())) return Security::permissionFailure($this);
if(!$page || !$page->ID) throw new SS_HTTPResponse_Exception("Bad record ID #$id", 404);

$newPage = $page->duplicateWithChildren();

// Reload form, data and actions might have changed
$form = $this->getEditForm($newPage->ID);

return $form->forTemplate();
} else {
user_error("CMSMain::duplicate() Bad ID: '$id'", E_USER_WARNING);
}
}

/**
* Return the version number of this application.
* Uses the subversion path information in <mymodule>/silverstripe_version
* (automacially replaced by build scripts).
*
* @return string
*/
public function CMSVersion() {
$cmsVersion = file_get_contents(CMS_PATH . '/silverstripe_version');
if(!$cmsVersion) $cmsVersion = _t('LeftAndMain.VersionUnknown', 'Unknown');

$frameworkVersion = file_get_contents(FRAMEWORK_PATH . '/silverstripe_version');
if(!$frameworkVersion) $frameworkVersion = _t('LeftAndMain.VersionUnknown', 'Unknown');

return sprintf(
"CMS: %s Framework: %s",
$cmsVersion,
$frameworkVersion
);
}

function providePermissions() {
$title = _t("CMSPagesController.MENUTITLE", LeftAndMain::menu_title_for_class('CMSPagesController'));
return array(
"CMS_ACCESS_CMSMain" => array(
'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array('title' => $title)),
'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access'),
'help' => _t(
'CMSMain.ACCESS_HELP',
'Allow viewing of the section containing page tree and content. View and edit permissions can be handled through page specific dropdowns, as well as the separate "Content permissions".'
),
'sort' => -99 // below "CMS_ACCESS_LeftAndMain", but above everything else
)
);
}

}
Something went wrong with that request. Please try again.