/** * Add this item into a particular changeset */ public function addToChangeset() { // We only add content into changesets that exists already - this is because until it exists, it doesn't // have an ID, meaning the relationship can't be created. From a usage standpoint this is okay... not ideal, // but users will always change something about a default created page before wanting it published (it also // means that non-modified default content doesn't get accidentally published...) $oid = $this->owner->ID; $mid = Member::currentUserID(); if (!$this->owner->ID || !Member::currentUserID()) { return; } // first see if it's in an active changeset already $changeset = $this->getCurrentChangeset(); if ($changeset) { return; } // if not, get the current user's changeset try { $changeset = $this->changesetService->getChangesetForUser(); if (!$changeset) { $changeset = $this->changesetService->createChangeset(sprintf(_t('Changesets.DEFAULT_TITLE', '%s started at %s'), Member::currentUser()->getTitle(), date('Y-m-d H:i:s'))); } if ($changeset) { $changeset->addItem($this->owner); } } catch (Exception $e) { SS_Log::log($e, SS_Log::ERR); } }
/** * Create a new changeset for the given member (the current user is used as the default) * * @param String $name * A name for this changeset, if applicable * @return * The new changeset object */ public function createChangeset($name = '', $member = null) { if (!$member) { $member = Member::currentUser(); } $changeset = ContentChangeset::create(); $changeset->Title = $name; $changeset->OwnerID = $member->ID; $changeset->write(); return $changeset; }
/** * Push a content changeset to a syncro node * * @param ContentChangeset $changeset * @param RemoteSyncroNode $node * */ public function pushChangeset($changeset, $node) { $cs = $changeset->ChangesetItems(); $update = array('changes' => array(), 'deletes' => array(), 'rels' => array()); foreach ($cs as $changesetItem) { $record = null; // if it's a delete, we create a syncro delete, otherwise get the versioned item if ($changesetItem->ChangeType == 'Draft Deleted') { // find the syncrodelete record $record = DataList::create('SyncroDelete')->filter(array('ContentID' => $changesetItem->OtherContentID))->first(); $del = array('SYNCRODELETE' => 'DELETE', 'ContentID' => $record->ContentID, 'Type' => $record->Type, 'MasterNode' => SiteConfig::current_site_config()->SystemID, 'Deleted' => $record->Created); $update['deletes'][] = $del; } else { $record = Versioned::get_version($changesetItem->OtherClass, $changesetItem->OtherID, $changesetItem->ContentVersion); $syncd = $this->syncroObject($record); $syncd['ClassName'] = $record->ClassName; if (count($syncd['has_one']) || count($syncd['many_many'])) { $relInfo = new stdClass(); $relInfo->SYNCROREL = true; $relInfo->Type = $record->ClassName; $relInfo->ContentID = $record->ContentID; $relInfo->MasterNode = SiteConfig::current_site_config()->SystemID; $relInfo->has_one = $syncd['has_one']; $relInfo->many_many = $syncd['many_many']; $update['rels'][] = $relInfo; } unset($syncd['has_one']); unset($syncd['many_many']); $update['changes'][] = $syncd; } } if ($update && $node) { $url = $node->NodeURL; $service = new RestfulService($url, -1); $params = array('changes' => $update['changes'], 'deletes' => $update['deletes'], 'rels' => $update['rels']); $headers = array('X-Auth-Token: ' . $node->APIToken, 'Content-Type: application/json'); $body = json_encode($params); $response = $service->request(self::PUSH_URL, 'POST', $body, $headers); //, $headers, $curlOptions); if ($response && $response->isError()) { $body = $response->getBody(); if ($body) { $this->log->logError($body); } return array(false, "Deployment failed to {$url}: status {$response->getStatusCode()}"); } $body = $response->getBody(); if ($body) { return array(true, 'Deployment successful'); } } }