/** * Form used for defining the conversion form * @return {Form} Form to be used for configuring the conversion */ public function ConvertObjectForm() { //Reset the reading stage Versioned::reset(); $fields = new FieldList(CompositeField::create($convertModeField = new OptionsetField('ConvertMode', '', array('ReplacePage' => _t('KapostAdmin.REPLACES_AN_EXISTING_PAGE', '_This replaces an existing page'), 'NewPage' => _t('KapostAdmin.IS_NEW_PAGE', '_This is a new page')), 'NewPage'))->addExtraClass('kapostConvertLeftSide'), CompositeField::create($replacePageField = TreeDropdownField::create('ReplacePageID', _t('KapostAdmin.REPLACE_PAGE', '_Replace this page'), 'SiteTree')->addExtraClass('replace-page-id'), TreeDropdownField::create('ParentPageID', _t('KapostAdmin.USE_AS_PARENT', '_Use this page as the parent for the new page, leave empty for a top level page'), 'SiteTree')->addExtraClass('parent-page-id'))->addExtraClass('kapostConvertRightSide')); $actions = new FieldList(FormAction::create('doConvertObject', _t('KapostAdmin.CONTINUE_CONVERT', '_Continue'))->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'kapost-convert')); $validator = new RequiredFields('ConvertMode'); $form = new Form($this, 'ConvertObjectForm', $fields, $actions, $validator); $form->addExtraClass('KapostAdmin center')->setAttribute('data-layout-type', 'border')->setTemplate('KapostAdmin_ConvertForm'); //Handle pages to see if the page exists $convertToClass = $this->getDestinationClass(); if ($convertToClass !== false && ($convertToClass == 'SiteTree' || is_subclass_of($convertToClass, 'SiteTree'))) { $obj = SiteTree::get()->filter('KapostRefID', Convert::raw2sql($this->record->KapostRefID))->first(); if (!empty($obj) && $obj !== false && $obj->ID > 0) { $convertModeField->setValue('ReplacePage'); $replacePageField->setValue($obj->ID); $recordTitle = $this->record->Title; if (!empty($recordTitle) && $recordTitle != $obj->Title) { $urlFieldLabel = _t('KapostAdmin.TITLE_CHANGE_DETECT', '_The title differs from the page being replaced, it was "{wastitle}" and will be changed to "{newtitle}". Do you want to update the URL Segment?', array('wastitle' => $obj->Title, 'newtitle' => $recordTitle)); $fields->push(CheckboxField::create('UpdateURLSegment', $urlFieldLabel)->addExtraClass('urlsegmentcheck')->setAttribute('data-replace-id', $obj->ID)->setForm($form)->setDescription(_t('KapostAdmin.NEW_URL_SEGMENT', '_The new URL Segment will be or will be close to "{newsegment}"', array('newsegment' => $obj->generateURLSegment($recordTitle))))); } } } Requirements::css(KAPOST_DIR . '/css/KapostAdmin.css'); Requirements::add_i18n_javascript(KAPOST_DIR . '/javascript/lang/'); Requirements::javascript(KAPOST_DIR . '/javascript/KapostAdmin_convertPopup.js'); //Allow extensions to adjust the form $this->extend('updateConvertObjectForm', $form, $this->record); return $form; }
function setUp() { // Mark test as being run $this->originalIsRunningTest = self::$is_running_test; self::$is_running_test = true; // i18n needs to be set to the defaults or tests fail i18n::set_locale(i18n::default_locale()); i18n::set_date_format(null); i18n::set_time_format(null); // Remove password validation $this->originalMemberPasswordValidator = Member::password_validator(); $this->originalRequirements = Requirements::backend(); Member::set_password_validator(null); Cookie::set_report_errors(false); RootURLController::reset(); Translatable::reset(); Versioned::reset(); DataObject::reset(); SiteTree::reset(); Hierarchy::reset(); if (Controller::has_curr()) { Controller::curr()->setSession(new Session(array())); } $this->originalTheme = SSViewer::current_theme(); // Save nested_urls state, so we can restore it later $this->originalNestedURLsState = SiteTree::nested_urls(); $className = get_class($this); $fixtureFile = eval("return {$className}::\$fixture_file;"); $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_'; // Set up fixture if ($fixtureFile || $this->usesDatabase || !self::using_temp_db()) { if (substr(DB::getConn()->currentDatabase(), 0, strlen($prefix) + 5) != strtolower(sprintf('%stmpdb', $prefix))) { //echo "Re-creating temp database... "; self::create_temp_db(); //echo "done.\n"; } singleton('DataObject')->flushCache(); self::empty_temp_db(); foreach ($this->requireDefaultRecordsFrom as $className) { $instance = singleton($className); if (method_exists($instance, 'requireDefaultRecords')) { $instance->requireDefaultRecords(); } if (method_exists($instance, 'augmentDefaultRecords')) { $instance->augmentDefaultRecords(); } } if ($fixtureFile) { $fixtureFiles = is_array($fixtureFile) ? $fixtureFile : array($fixtureFile); $i = 0; foreach ($fixtureFiles as $fixtureFilePath) { $fixture = new YamlFixture($fixtureFilePath); $fixture->saveIntoDatabase(); $this->fixtures[] = $fixture; // backwards compatibility: Load first fixture into $this->fixture if ($i == 0) { $this->fixture = $fixture; } $i++; } } $this->logInWithPermission("ADMIN"); } // Set up email $this->originalMailer = Email::mailer(); $this->mailer = new TestMailer(); Email::set_mailer($this->mailer); Email::send_all_emails_to(null); // Preserve memory settings $this->originalMemoryLimit = ini_get('memory_limit'); }
public function setUp() { // We cannot run the tests on this abstract class. if (get_class($this) == "SapphireTest") { $this->skipTest = true; } if ($this->skipTest) { $this->markTestSkipped(sprintf('Skipping %s ', get_class($this))); return; } // Mark test as being run $this->originalIsRunningTest = self::$is_running_test; self::$is_running_test = true; // i18n needs to be set to the defaults or tests fail i18n::set_locale(i18n::default_locale()); i18n::config()->date_format = null; i18n::config()->time_format = null; // Set default timezone consistently to avoid NZ-specific dependencies date_default_timezone_set('UTC'); // Remove password validation $this->originalMemberPasswordValidator = Member::password_validator(); $this->originalRequirements = Requirements::backend(); Member::set_password_validator(null); Config::inst()->update('Cookie', 'report_errors', false); if (class_exists('RootURLController')) { RootURLController::reset(); } if (class_exists('Translatable')) { Translatable::reset(); } Versioned::reset(); DataObject::reset(); if (class_exists('SiteTree')) { SiteTree::reset(); } Hierarchy::reset(); if (Controller::has_curr()) { Controller::curr()->setSession(Injector::inst()->create('Session', array())); } Security::$database_is_ready = null; // Add controller-name auto-routing Config::inst()->update('Director', 'rules', array('$Controller//$Action/$ID/$OtherID' => '*')); $fixtureFile = static::get_fixture_file(); $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_'; // Set up email $this->originalMailer = Email::mailer(); $this->mailer = new TestMailer(); Email::set_mailer($this->mailer); Config::inst()->remove('Email', 'send_all_emails_to'); // Todo: this could be a special test model $this->model = DataModel::inst(); // Set up fixture if ($fixtureFile || $this->usesDatabase || !self::using_temp_db()) { if (substr(DB::get_conn()->getSelectedDatabase(), 0, strlen($prefix) + 5) != strtolower(sprintf('%stmpdb', $prefix))) { //echo "Re-creating temp database... "; self::create_temp_db(); //echo "done.\n"; } singleton('DataObject')->flushCache(); self::empty_temp_db(); foreach ($this->requireDefaultRecordsFrom as $className) { $instance = singleton($className); if (method_exists($instance, 'requireDefaultRecords')) { $instance->requireDefaultRecords(); } if (method_exists($instance, 'augmentDefaultRecords')) { $instance->augmentDefaultRecords(); } } if ($fixtureFile) { $pathForClass = $this->getCurrentAbsolutePath(); $fixtureFiles = is_array($fixtureFile) ? $fixtureFile : array($fixtureFile); $i = 0; foreach ($fixtureFiles as $fixtureFilePath) { // Support fixture paths relative to the test class, rather than relative to webroot // String checking is faster than file_exists() calls. $isRelativeToFile = strpos('/', $fixtureFilePath) === false || preg_match('/^\\.\\./', $fixtureFilePath); if ($isRelativeToFile) { $resolvedPath = realpath($pathForClass . '/' . $fixtureFilePath); if ($resolvedPath) { $fixtureFilePath = $resolvedPath; } } $fixture = Injector::inst()->create('YamlFixture', $fixtureFilePath); $fixture->writeInto($this->getFixtureFactory()); $this->fixtures[] = $fixture; // backwards compatibility: Load first fixture into $this->fixture if ($i == 0) { $this->fixture = $fixture; } $i++; } } $this->logInWithPermission("ADMIN"); } // Preserve memory settings $this->originalMemoryLimit = ini_get('memory_limit'); // turn off template debugging Config::inst()->update('SSViewer', 'source_file_comments', false); // Clear requirements Requirements::clear(); }
/** * Gets the referenced object of this kapost bridge log entry */ public function getReferenceObject() { if ($this->reference_object === false) { $kapostRefID = false; if ($this->Method == 'metaWeblog.getPost' || $this->Method == 'metaWeblog.editPost') { $xml = simplexml_load_string($this->Request); if ($xml) { $kapostRefID = $xml->params->param[0]->value->string->__toString(); } } else { if ($this->Method == 'metaWeblog.newPost' || $this->Method == 'kapost.getPreview' || $this->Method == 'metaWeblog.newMediaObject') { $xml = simplexml_load_string($this->Response); if ($xml && !isset($xml->fault)) { if ($this->Method == 'metaWeblog.newPost') { $kapostRefID = $xml->params->param[0]->value->string->__toString(); } else { if ($this->Method == 'kapost.getPreview') { $kapostRefID = $xml->params->param[0]->value->struct->member[1]->value->string->__toString(); } else { if ($this->Method == 'metaWeblog.newMediaObject') { //Attempt to lookup the referenced file $fileURL = $xml->params->param[0]->value->struct->member[1]->value->string->__toString(); if (!empty($fileURL)) { $file = KapostService::find_file_by_url($fileURL); if (!empty($file) && $file !== false && $file->ID > 0) { $this->reference_object = $file; return $this->reference_object; } else { $this->reference_object = null; return; } } else { $this->reference_object = null; return; } } } } } } else { $this->reference_object = null; return; } } //If we have a if (!empty($kapostRefID)) { Versioned::reset(); //Reset versioned to remove filters //Try looking for a page $page = SiteTree::get()->filter('KapostRefID', Convert::raw2sql($kapostRefID))->first(); if (!empty($page) && $page !== false && $page->exists()) { $this->reference_object = $page; } else { //Allow extensions to add their own logic $extensionResult = array_filter($this->extend('updateObjectLookup', $kapostRefID), function ($item) { return is_object($item); }); $extensionResult = array_shift($extensionResult); if (!empty($extensionResult) && $extensionResult !== false && $extensionResult->exists()) { $this->reference_object = $kapostObj; } else { //Look for a kapost object that is not a preview $kapostObj = KapostObject::get()->filter('KapostRefID', Convert::raw2sql($kapostRefID))->first(); if (!empty($kapostObj) && $kapostObj !== false && $kapostObj->exists() && $kapostObj->IsKapostPreview == false) { $this->reference_object = $kapostObj; } else { //No result so set to null $this->reference_object = null; } } } } else { //No Kapost Reference ID so set to null $this->reference_object = null; } } return $this->reference_object; }
function setUp() { // We cannot run the tests on this abstract class. if (get_class($this) == "SapphireTest") { $this->skipTest = true; } if ($this->skipTest) { $this->markTestSkipped(sprintf('Skipping %s ', get_class($this))); return; } // Mark test as being run $this->originalIsRunningTest = self::$is_running_test; self::$is_running_test = true; // i18n needs to be set to the defaults or tests fail i18n::set_locale(i18n::default_locale()); i18n::set_date_format(null); i18n::set_time_format(null); // Remove password validation $this->originalMemberPasswordValidator = Member::password_validator(); $this->originalRequirements = Requirements::backend(); Member::set_password_validator(null); Cookie::set_report_errors(false); if (class_exists('RootURLController')) { RootURLController::reset(); } if (class_exists('Translatable')) { Translatable::reset(); } Versioned::reset(); DataObject::reset(); if (class_exists('SiteTree')) { SiteTree::reset(); } Hierarchy::reset(); if (Controller::has_curr()) { Controller::curr()->setSession(new Session(array())); } $this->originalTheme = SSViewer::current_theme(); if (class_exists('SiteTree')) { // Save nested_urls state, so we can restore it later $this->originalNestedURLsState = SiteTree::nested_urls(); } $className = get_class($this); $fixtureFile = eval("return {$className}::\$fixture_file;"); $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_'; // Todo: this could be a special test model $this->model = DataModel::inst(); // Set up fixture if ($fixtureFile || $this->usesDatabase || !self::using_temp_db()) { if (substr(DB::getConn()->currentDatabase(), 0, strlen($prefix) + 5) != strtolower(sprintf('%stmpdb', $prefix))) { //echo "Re-creating temp database... "; self::create_temp_db(); //echo "done.\n"; } singleton('DataObject')->flushCache(); self::empty_temp_db(); foreach ($this->requireDefaultRecordsFrom as $className) { $instance = singleton($className); if (method_exists($instance, 'requireDefaultRecords')) { $instance->requireDefaultRecords(); } if (method_exists($instance, 'augmentDefaultRecords')) { $instance->augmentDefaultRecords(); } } if ($fixtureFile) { $pathForClass = $this->getCurrentAbsolutePath(); $fixtureFiles = is_array($fixtureFile) ? $fixtureFile : array($fixtureFile); $i = 0; foreach ($fixtureFiles as $fixtureFilePath) { // Support fixture paths relative to the test class, rather than relative to webroot // String checking is faster than file_exists() calls. $isRelativeToFile = strpos('/', $fixtureFilePath) === false || preg_match('/^\\.\\./', $fixtureFilePath); if ($isRelativeToFile) { $resolvedPath = realpath($pathForClass . '/' . $fixtureFilePath); if ($resolvedPath) { $fixtureFilePath = $resolvedPath; } } $fixture = new YamlFixture($fixtureFilePath); $fixture->saveIntoDatabase($this->model); $this->fixtures[] = $fixture; // backwards compatibility: Load first fixture into $this->fixture if ($i == 0) { $this->fixture = $fixture; } $i++; } } $this->logInWithPermission("ADMIN"); } // Set up email $this->originalMailer = Email::mailer(); $this->mailer = new TestMailer(); Email::set_mailer($this->mailer); Email::send_all_emails_to(null); // Preserve memory settings $this->originalMemoryLimit = ini_get('memory_limit'); }
/** * Handles editing of a given post * @param {mixed} $content_id Identifier for the post * @param {array} $content Post details * @param {int} $publish 0 or 1 depending on whether to publish the post or not * @param {bool} $isPreview Is preview mode or not (defaults to false) */ protected function editPost($content_id, $content, $publish, $isPreview = false) { //Decode all hash or array keys if (array_key_exists('custom_fields', $content)) { $hashKeys = $this->preg_grep_keys('/^_kapost_(array|hash)_/', $content['custom_fields']); if (!empty($hashKeys)) { foreach ($hashKeys as $key => $value) { //Decode the base64 encoded string $value = @base64_decode($value); if ($value !== false && !empty($value)) { //Decode the array string to a php associative array $value = @json_decode($value, true); if ($value !== false && !empty($value)) { //Remove the old key unset($content['custom_fields'][$key]); //Add the decoded array to the custom fields $content['custom_fields'][preg_replace('/^_kapost_(array|hash)_/', '', $key)] = $value; } } } } } //Allow extensions to handle the request $results = $this->extend('editPost', $content_id, $content, $publish, $isPreview); if ($results && is_array($results)) { $results = array_filter($results, function ($v) { return !is_null($v); }); if (count($results) > 0) { return array_shift($results); } } //Ensure the type is an extension of the KapostPage object if (array_key_exists('custom_fields', $content) && (!class_exists('Kapost' . $content['custom_fields']['kapost_custom_type']) || !class_exists('SiteTree') || !('Kapost' . $content['custom_fields']['kapost_custom_type'] == 'KapostPage' || is_subclass_of('Kapost' . $content['custom_fields']['kapost_custom_type'], 'KapostPage'))) || !array_key_exists('custom_fields', $content) && !class_exists('SiteTree')) { return $this->httpError(400, _t('KapostService.TYPE_NOT_KNOWN', '_The type "{type}" is not a known type', array('type' => $content['custom_fields']['kapost_custom_type']))); } //Assume we're looking for a page //Clear Versioned Versioned to stage $oldReadingStage = Versioned::current_stage(); Versioned::reset(); $page = SiteTree::get()->filter('KapostRefID', Convert::raw2sql($content_id))->first(); //Switch Versioned back Versioned::reading_stage($oldReadingStage); $pageTitle = $content['title']; if (array_key_exists('custom_fields', $content) && array_key_exists('SS_Title', $content['custom_fields']) && !empty($content['custom_fields']['SS_Title'])) { $pageTitle = $content['custom_fields']['SS_Title']; } $menuTitle = $content['title']; if (empty($content['title']) && array_key_exists('custom_fields', $content) && array_key_exists('SS_Title', $content['custom_fields']) && !empty($content['custom_fields']['SS_Title'])) { $menuTitle = $content['custom_fields']['SS_Title']; } $kapostObj = KapostObject::get()->filter('KapostRefID', Convert::raw2sql($content_id))->first(); if (!empty($kapostObj) && $kapostObj !== false && $kapostObj->exists()) { $kapostObj->Title = $pageTitle; $kapostObj->MenuTitle = $menuTitle; $kapostObj->Content = self::config()->filter_kapost_threads == true ? $this->filterKapostThreads($content['description']) : $content['description']; $kapostObj->MetaDescription = array_key_exists('custom_fields', $content) && array_key_exists('SS_MetaDescription', $content['custom_fields']) ? $content['custom_fields']['SS_MetaDescription'] : null; $kapostObj->LinkedPageID = !empty($page) && $page !== false && $page->exists() ? $page->ID : $kapostObj->LinkedPageID; $kapostObj->KapostRefID = array_key_exists('custom_fields', $content) ? $content['custom_fields']['kapost_post_id'] : null; $kapostObj->KapostAuthor = array_key_exists('custom_fields', $content) ? $content['custom_fields']['kapost_author'] : null; $kapostObj->KapostAuthorAvatar = array_key_exists('custom_fields', $content) ? $content['custom_fields']['kapost_author_avatar'] : null; $kapostObj->KapostConversionNotes = array_key_exists('custom_fields', $content) && array_key_exists('SS_KapostConversionNotes', $content['custom_fields']) ? $content['custom_fields']['SS_KapostConversionNotes'] : null; $kapostObj->ToPublish = $publish; $kapostObj->IsKapostPreview = $isPreview; $kapostObj->write(); //Allow extensions to adjust the existing object $this->extend('updateEditKapostPage', $kapostObj, $content_id, $content, $publish, $isPreview); return true; } else { $className = array_key_exists('custom_fields', $content) ? 'Kapost' . $content['custom_fields']['kapost_custom_type'] : 'KapostPage'; $obj = new $className(); $obj->Title = $pageTitle; $obj->MenuTitle = $menuTitle; $obj->Content = self::config()->filter_kapost_threads == true ? $this->filterKapostThreads($content['description']) : $content['description']; $obj->MetaDescription = array_key_exists('custom_fields', $content) && array_key_exists('SS_MetaDescription', $content['custom_fields']) ? $content['custom_fields']['SS_MetaDescription'] : null; $obj->KapostChangeType = 'edit'; $obj->LinkedPageID = !empty($page) && $page !== false && $page->exists() ? $page->ID : 0; $obj->KapostRefID = array_key_exists('custom_fields', $content) ? $content['custom_fields']['kapost_post_id'] : null; $obj->KapostAuthor = array_key_exists('custom_fields', $content) ? $content['custom_fields']['kapost_author'] : null; $obj->KapostAuthorAvatar = array_key_exists('custom_fields', $content) ? $content['custom_fields']['kapost_author_avatar'] : null; $obj->KapostConversionNotes = array_key_exists('custom_fields', $content) && array_key_exists('SS_KapostConversionNotes', $content['custom_fields']) ? $content['custom_fields']['SS_KapostConversionNotes'] : null; $obj->ToPublish = $publish; $obj->IsKapostPreview = $isPreview; $obj->write(); //Allow extensions to adjust the new page $this->extend('updateEditKapostPage', $obj, $content_id, $content, $publish, $isPreview); return true; } //Can't find the object so return a 404 code return new xmlrpcresp(0, 404, _t('KapostService.INVALID_POST_ID', '_Invalid post ID.')); }