/** * Generate an XML representation of the given {@link DataObjectSet}. * * @param DataObjectSet $set * @return String XML */ public function convertDataObjectSet(DataObjectSet $set, $fields = null) { Controller::curr()->getResponse()->addHeader("Content-Type", "text/csv"); // csv header $headerArray = array(); foreach ($this->getFieldsForObj($set->First()) as $fieldName => $fieldType) { $headerArray[] = $fieldName; $fieldValue = '"' . $set->First()->{$fieldName} . '"'; } $csv = implode(',', $headerArray) . "\r\n"; foreach ($set as $item) { if ($item->canView()) { $csv .= $this->convertDataObjectWithoutHeader($item, $fields); } } return $csv; }
/** * Check that current product variation is valid * * @param Array $data Submitted data * @return Boolean Returns TRUE if the submitted data is valid, otherwise FALSE. */ function php($data) { $valid = parent::php($data); $fields = $this->form->Fields(); //Check that variation exists if necessary $form = $this->form; $request = $this->form->getRequest(); //Get product variations from options sent //TODO refactor this $productVariations = new DataObjectSet(); $options = $request->postVar('Options'); $product = DataObject::get_by_id($data['ProductClass'], $data['ProductID']); $variations = $product ? $product->Variations() : new DataObjectSet(); if ($variations && $variations->exists()) { foreach ($variations as $variation) { $variationOptions = $variation->Options()->map('AttributeID', 'ID'); if ($options == $variationOptions && $variation->isEnabled()) { $productVariations->push($variation); } } } if ((!$productVariations || !$productVariations->exists()) && $product && $product->requiresVariation()) { $this->form->sessionMessage(_t('Form.VARIATIONS_REQUIRED', 'This product requires options before it can be added to the cart.'), 'bad'); //Have to set an error for Form::validate() $this->errors[] = true; $valid = false; return $valid; } //Validate that the product/variation being added is inStock() $stockLevel = 0; if ($product) { if ($product->requiresVariation()) { $stockLevel = $productVariations->First()->StockLevel()->Level; } else { $stockLevel = $product->StockLevel()->Level; } } if ($stockLevel == 0) { $this->form->sessionMessage(_t('Form.STOCK_LEVEL', ''), 'bad'); //Have to set an error for Form::validate() $this->errors[] = true; $valid = false; } //Validate the quantity is not greater than the available stock $quantity = $request->postVar('Quantity'); if ($stockLevel > 0 && $stockLevel < $quantity) { $this->form->sessionMessage(_t('Form.STOCK_LEVEL_MORE_THAN_QUANTITY', 'The quantity is greater than available stock for this product.'), 'bad'); //Have to set an error for Form::validate() $this->errors[] = true; $valid = false; } return $valid; }
function testIterator() { $set = new DataObjectSet(array($one = new DataObject(array('Title' => 'one')), $two = new DataObject(array('Title' => 'two')), $three = new DataObject(array('Title' => 'three')), $four = new DataObject(array('Title' => 'four')))); // test Pos() with foreach() $i = 0; foreach ($set as $item) { $i++; $this->assertEquals($i, $item->Pos(), "Iterator position is set correctly on ViewableData when iterated with foreach()"); } // test Pos() manually $this->assertEquals(1, $one->Pos()); $this->assertEquals(2, $two->Pos()); $this->assertEquals(3, $three->Pos()); $this->assertEquals(4, $four->Pos()); // test DataObjectSet->Count() $this->assertEquals(4, $set->Count()); // test DataObjectSet->First() $this->assertSame($one, $set->First()); // test DataObjectSet->Last() $this->assertSame($four, $set->Last()); // test ViewableData->First() $this->assertTrue($one->First()); $this->assertFalse($two->First()); $this->assertFalse($three->First()); $this->assertFalse($four->First()); // test ViewableData->Last() $this->assertFalse($one->Last()); $this->assertFalse($two->Last()); $this->assertFalse($three->Last()); $this->assertTrue($four->Last()); // test ViewableData->Middle() $this->assertFalse($one->Middle()); $this->assertTrue($two->Middle()); $this->assertTrue($three->Middle()); $this->assertFalse($four->Middle()); // test ViewableData->Even() $this->assertFalse($one->Even()); $this->assertTrue($two->Even()); $this->assertFalse($three->Even()); $this->assertTrue($four->Even()); // test ViewableData->Odd() $this->assertTrue($one->Odd()); $this->assertFalse($two->Odd()); $this->assertTrue($three->Odd()); $this->assertFalse($four->Odd()); }
function run(DataObjectSet $pages) { $pageIDs = $pages->column('ID'); foreach ($pageIDs as $pageID) { FormResponse::add("\$('Form_EditForm').reloadIfSetTo({$pageID});"); } $count = array(); $count['PUBLISH_SUCCESS'] = $count['DELETE_SUCCESS'] = 0; $count['PUBLISH_FAILURE'] = $count['DELETE_FAILURE'] = 0; $arbitraryPage = $pages->First(); $arbitraryPage->invokeWithExtensions('onBeforeBatchPublish', $pages); foreach ($pages as $page) { $type = $page->openWorkflowRequest() instanceof WorkflowDeletionRequest ? 'DELETE' : 'PUBLISH'; if ($page->batchPublish()) { $count[$type . '_SUCCESS']++; // Now make sure the tree title is appropriately updated $publishedRecord = DataObject::get_by_id('SiteTree', $page->ID); if ($publishedRecord) { $JS_title = Convert::raw2js($publishedRecord->TreeTitle()); FormResponse::add("\$('sitetree').setNodeTitle({$page->ID}, '{$JS_title}');"); } } else { $count[$type . '_FAILURE']++; FormResponse::add("\$('sitetree').addNodeClassByIdx('{$page->ID}', 'failed');"); } $page->destroy(); unset($page); } $arbitraryPage->invokeWithExtensions('onAfterBatchPublish', $pages); $messages = array('PUBLISH_SUCCESS' => _t('BatchPublishPages.PUBLISH_SUCCESS', 'Published %d pages.'), 'PUBLISH_FAILURE' => _t('BatchPublishPages.PUBLISH_FAILURE', 'Failed to publish %d pages.'), 'DELETE_SUCCESS' => _t('BatchPublishPages.DELETE_SUCCESS', 'Deleted %d pages from the published site.'), 'DELETE_FAILURE' => _t('BatchPublishPages.DELETE_FAILURE', 'Failed to delete %d pages from the published site.'), 'PUBLISH_SUCCESS_ONE' => _t('BatchPublishPages.PUBLISH_SUCCESS_ONE', 'Published %d page.'), 'PUBLISH_FAILURE_ONE' => _t('BatchPublishPages.PUBLISH_FAILURE_ONE', 'Failed to publish %d page.'), 'DELETE_SUCCESS_ONE' => _t('BatchPublishPages.DELETE_SUCCESS_ONE', 'Deleted %d page from the published site.'), 'DELETE_FAILURE_ONE' => _t('BatchPublishPages.DELETE_FAILURE_ONE', 'Failed to delete %d page from the published site.')); $displayedMessages = array(); foreach ($count as $type => $count) { if ($count) { $message = $count == 1 ? $messages[$type . '_ONE'] : $messages[$type]; $displayedMessages[] = sprintf($message, $count); } } $displayedMessage = implode(" ", $displayedMessages); FormResponse::add('statusMessage("' . $displayedMessage . '","good");'); return FormResponse::respond(); }
/** * Test {@link DataObjectSet->sort()} */ function testSort() { $set = new DataObjectSet(array(array('Name' => 'Object1', 'F1' => 1, 'F2' => 2, 'F3' => 3), array('Name' => 'Object2', 'F1' => 2, 'F2' => 1, 'F3' => 4), array('Name' => 'Object3', 'F1' => 5, 'F2' => 2, 'F3' => 2))); // test a single sort ASC $set->sort('F3', 'ASC'); $this->assertEquals($set->First()->Name, 'Object3', 'Object3 should be first in the set'); // test a single sort DESC $set->sort('F3', 'DESC'); $this->assertEquals($set->First()->Name, 'Object2', 'Object2 should be first in the set'); // test a multi sort $set->sort(array('F2' => 'ASC', 'F1' => 'ASC')); $this->assertEquals($set->Last()->Name, 'Object3', 'Object3 should be last in the set'); // test a multi sort $set->sort(array('F2' => 'ASC', 'F1' => 'DESC')); $this->assertEquals($set->Last()->Name, 'Object1', 'Object1 should be last in the set'); }
/** * Test {@link DataObjectSet->unshift()} */ function testUnshift() { $set = new DataObjectSet(); $set->push(new ArrayData(array('Name' => 'Joe'))); $set->push(new ArrayData(array('Name' => 'Bob'))); $set->push(new ArrayData(array('Name' => 'Ted'))); $set->unshift(new ArrayData(array('Name' => 'Steve'))); $this->assertEquals('Steve', $set->First()->Name); }