Change page type by API

Permalink
How can I change the page type using the API.
For example, I have two types "default" and "default2", and if i press the button on the page its type is changed to another.

I think firstly I should write $myPageType = CollectionType::getByHandle('default2');

:)

Pibamba
 
jordanlev replied on at Permalink Reply
jordanlev
Where exactly do you want to change the page type from? When a package is installed? When a dashboard button is clicked? After a page is added? etc.
Pibamba replied on at Permalink Reply
Pibamba
Right on the page.

<input type="submit" value="Change" />

Visitor comes and press the button after which the page is reloaded and the site is changing form.
jordanlev replied on at Permalink Best Answer Reply
jordanlev
This is tricky... you can change the page type from code, but it's going to change it for everyone (not just that user).

I pulled this code from various pieces of the concrete/startup/process.php file, so I have no idea if it actually works, but:
$newPageTypeHandle = 'left_sidebar'; //or whatever
$c = Page::getCurrentPage();
$nvc = $c->getVersionToModify();
Loader::model('collection_types');
$ct = CollectionType::getByHandle($newPageTypeHandle);
$ctID = $ct->getCollectionTypeID();
$nvc->update(array('ctID' => $ctID));
$nvc->approve();
header('Location: ' . BASE_URL . View::url());

You're going to want to create a new block and put a form on it, then have an action function in the controller that responds to that form and runs that code snippet above.

Good luck!
Pibamba replied on at Permalink Reply
Pibamba
I have a big problem... I put code into default.php and default2.php:

$nvc = $c->getVersionToModify();
Loader::model('collection_types');

if (($_COOKIE['choose'] == 2) && ($c->getCollectionTypeHandle() == 'default')) {
$ct = CollectionType::getByHandle('default2');
$ctID = $ct->getCollectionTypeID();
$nvc->update(array('ctID' => $ctID));
#$nvc->approve();
header('Location: ' . BASE_URL . View::url());
}
elseif ((($_COOKIE['choose'] == 1) || (!isset($_COOKIE['choose']))) && ($c->getCollectionTypeHandle() == 'default2')) {
$ct = CollectionType::getByHandle('default');
$ctID = $ct->getCollectionTypeID();
$nvc->update(array('ctID' => $ctID));
#$nvc->approve();
header('Location: ' . BASE_URL . View::url());
}

It work but only from admin panel. If I go to the page as a normal user then an error:

mysql error: [1062: Duplicate entry '1-17' for key 1] in EXECUTE("insert into CollectionVersions (cID, cvID, cvName, cvHandle, cvDescription, cvDatePublic, cvDateCreated, cvComments, cvAuthorUID, cvIsNew) values ('1', 17, 'Главная', '', '', '2010-09-23 16:49:00', '2010-10-04 12:14:28', 'Новая версия 17', NULL, 1)")
jordanlev replied on at Permalink Reply
jordanlev
Yeah, I don't think this is going to be possible without a lot of experimenting with code. Sorry that didn't work, I'm not really sure what to tell you as I don't know how this works exactly.
This is definitely not the kind of thing that was intended to be done from the front-end by normal site visitors, so it might not even be possible.

:(
Pibamba replied on at Permalink Reply
Pibamba
Anyway thank you very much :)