<?php

$cms_root_path = "../../../";
require_once($cms_root_path."includes/compatibility_includer.php");
ini_set("memory_limit","300M"); // fet

$copy_sister_pages = false;
if (isset($_POST['copy_sister_pages'])) {
    $copy_sister_pages = $_POST['copy_sister_pages'];
}

$with_data = true;
if (isset($_POST['with_data'])) {
    $with_data = $_POST['with_data'];
}

$copy_child_pages = true;
if (isset($_POST['copy_child_pages'])) {
    $copy_child_pages = $_POST['copy_child_pages'];
}

$keep_slugs = 0;
if (isset($_POST['keep_slugs'])) {
    $keep_slugs = $_POST['keep_slugs'];
}

$content_manager = new ContentManager();
$logic = array();
$pages = array();

$i = 0;
if (!empty($_POST['page_ids'])) {
    foreach ($_POST['page_ids'] as $page_id) {
        $page = new Page();
        $page->get($page_id);
        $pages[] = $page;
        if ($copy_sister_pages == true && $i === 0) {
            $sister_pages = Page::retrieve_subs($page->sub_id, PHP_INT_MAX, "order", "ASC", false);
            foreach ($sister_pages as $sister_page) {
                $pages[] = $sister_page;
            };
        }
        ++$i;
    }
}

$logic = array();
for ($i=0; $i < count($pages); $i++) {

    $logic[] = $content_manager->createPageLapsgLogic(
        $pages[$i],
        $copy_child_pages,
        0,
        $with_data,
        ($with_data ? 0 : 1), // maybe?
        false,
        0,
        0,
        $keep_slugs
    );
}
// wrap it in a action
$logic = [
    'action' => 'lapsg',
    'logic'  => [
        'pages' => $logic
    ]
];

$logic = addslashes(serialize($logic));
$_SESSION['content_manager']['clipboard_page'] = $logic;
die($logic);
