public function duplicate($nc, $preserveUserID = false) { $db = Database::get(); // the passed collection is the parent collection $cParentID = $nc->getCollectionID(); $u = new User(); $uID = $u->getUserID(); if ($preserveUserID) { $uID = $this->getCollectionUserID(); } $cobj = parent::getByID($this->cID); // create new name $newCollectionName = $this->getCollectionName(); $index = 1; $nameCount = 1; while ($nameCount > 0) { // if we have a node at the new level with the same name, we keep incrementing til we don't $nameCount = $db->GetOne('select count(Pages.cID) from CollectionVersions inner join Pages on (CollectionVersions.cID = Pages.cID and CollectionVersions.cvIsApproved = 1) where Pages.cParentID = ? and CollectionVersions.cvName = ?', array($cParentID, $newCollectionName)); if ($nameCount > 0) { $index++; $newCollectionName = $this->getCollectionName() . ' ' . $index; } } $newC = $cobj->duplicateCollection(); $newCID = $newC->getCollectionID(); $v = array($newCID, $this->getPageTypeID(), $cParentID, $uID, $this->overrideTemplatePermissions(), $this->getPermissionsCollectionID(), $this->getCollectionInheritance(), $this->cFilename, $this->cPointerID, $this->cPointerExternalLink, $this->cPointerExternalLinkNewWindow, $this->cDisplayOrder, $this->pkgID); $q = 'insert into Pages (cID, ptID, cParentID, uID, cOverrideTemplatePermissions, cInheritPermissionsFromCID, cInheritPermissionsFrom, cFilename, cPointerID, cPointerExternalLink, cPointerExternalLinkNewWindow, cDisplayOrder, pkgID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; $res = $db->query($q, $v); // Composer specific $rows = $db->GetAll('select cID, arHandle, cbDisplayOrder, ptComposerFormLayoutSetControlID, bID from PageTypeComposerOutputBlocks where cID = ?', array($this->cID)); if ($rows && is_array($rows)) { foreach ($rows as $row) { if (is_array($row) && $row['cID']) { $db->insert('PageTypeComposerOutputBlocks', array('cID' => $newCID, 'arHandle' => $row['arHandle'], 'cbDisplayOrder' => $row['cbDisplayOrder'], 'ptComposerFormLayoutSetControlID' => $row['ptComposerFormLayoutSetControlID'], 'bID' => $row['bID'])); } } } PageStatistics::incrementParents($newCID); if ($res) { // rescan the collection path $nc2 = Page::getByID($newCID); // now with any specific permissions - but only if this collection is set to override if ($this->getCollectionInheritance() == 'OVERRIDE') { $nc2->acquirePagePermissions($this->getPermissionsCollectionID()); $nc2->acquireAreaPermissions($this->getPermissionsCollectionID()); // make sure we update the proper permissions pointer to the new page ID $q = 'update Pages set cInheritPermissionsFromCID = ? where cID = ?'; $v = array($newCID, $newCID); $db->query($q, $v); } elseif ($this->getCollectionInheritance() == 'PARENT') { // we need to clear out any lingering permissions groups (just in case), and set this collection to inherit from the parent $npID = $nc->getPermissionsCollectionID(); $q = "update Pages set cInheritPermissionsFromCID = {$npID} where cID = {$newCID}"; $db->query($q); } $args = array(); if ($index > 1) { $args['cName'] = $newCollectionName; $args['cHandle'] = $nc2->getCollectionHandle() . '-' . $index; } $nc2->update($args); // arguments for event // 1. new page // 2. old page $pe = new DuplicatePageEvent($this); $pe->setNewPageObject($nc2); Section::registerDuplicate($nc2, $this); Events::dispatch('on_page_duplicate', $pe); $nc2->rescanCollectionPath(); $nc2->movePageDisplayOrderToBottom(); return $nc2; } }