1 | <?php |
||
2 | |||
3 | namespace SilverShop\Tasks; |
||
4 | |||
5 | use Page; |
||
6 | use SilverShop\Page\AccountPage; |
||
7 | use SilverShop\Page\CartPage; |
||
8 | use SilverShop\Page\CheckoutPage; |
||
9 | use SilverShop\Page\Product; |
||
10 | use SilverShop\Page\ProductCategory; |
||
11 | use SilverStripe\Core\Injector\Injector; |
||
12 | use SilverStripe\Dev\BuildTask; |
||
13 | use SilverStripe\Dev\FixtureFactory; |
||
14 | use SilverStripe\Dev\YamlFixture; |
||
15 | use SilverStripe\ORM\DB; |
||
16 | use SilverStripe\SiteConfig\SiteConfig; |
||
17 | |||
18 | /** |
||
19 | * Populate shop task |
||
20 | */ |
||
21 | class PopulateShopTask extends BuildTask |
||
22 | { |
||
23 | protected $title = 'Populate Shop'; |
||
24 | |||
25 | protected $description = 'Creates dummy account page, products, checkout page, terms page.'; |
||
26 | |||
27 | private static $segment = 'PopulateShopTask'; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
28 | |||
29 | public function run($request) |
||
30 | { |
||
31 | $this->extend('beforePopulate'); |
||
32 | |||
33 | $factory = Injector::inst()->create(FixtureFactory::class); |
||
34 | |||
35 | $parentid = 0; |
||
36 | |||
37 | $fixtureDir = realpath(__DIR__ . '/../../tests/php/Fixtures'); |
||
38 | |||
39 | //create products |
||
40 | if (!Product::get()->count()) { |
||
41 | $fixture = YamlFixture::create($fixtureDir . '/dummyproducts.yml'); |
||
42 | $fixture->writeInto($factory);//needs to be a data model |
||
43 | |||
44 | $shoppage = ProductCategory::get()->filter('URLSegment', 'shop')->first(); |
||
45 | $parentid = $shoppage->ID; |
||
46 | |||
47 | $categoriestopublish = array( |
||
48 | 'products', |
||
49 | 'electronics', |
||
50 | 'apparel', |
||
51 | 'entertainment', |
||
52 | 'music', |
||
53 | 'movies', |
||
54 | 'drama', |
||
55 | 'toys', |
||
56 | 'food', |
||
57 | 'books', |
||
58 | 'jewellery', |
||
59 | 'furniture', |
||
60 | 'kitchen', |
||
61 | 'bedroom', |
||
62 | 'stationery', |
||
63 | ); |
||
64 | foreach ($categoriestopublish as $categoryname) { |
||
65 | $factory->get(ProductCategory::class, $categoryname)->publishSingle(); |
||
66 | } |
||
67 | $productstopublish = [ |
||
68 | 'mp3player', |
||
69 | 'hdtv', |
||
70 | 'socks', |
||
71 | 'tshirt', |
||
72 | 'beachball', |
||
73 | 'hoop', |
||
74 | 'kite', |
||
75 | 'genericmovie', |
||
76 | 'lemonchicken', |
||
77 | 'ring', |
||
78 | 'book', |
||
79 | 'lamp', |
||
80 | 'paper', |
||
81 | 'pens', |
||
82 | ]; |
||
83 | foreach ($productstopublish as $productname) { |
||
84 | $factory->get(Product::class, $productname)->publishSingle(); |
||
85 | } |
||
86 | DB::alteration_message('Created dummy products and categories', 'created'); |
||
87 | } else { |
||
88 | echo '<p style="color:orange;">Products and categories were not created because some already exist.</p>'; |
||
89 | } |
||
90 | |||
91 | //cart page |
||
92 | if (!$page = CartPage::get()->first()) { |
||
93 | $fixture = YamlFixture::create($fixtureDir . '/pages/Cart.yml'); |
||
94 | $fixture->writeInto($factory); |
||
95 | $page = $factory->get(CartPage::class, 'cart'); |
||
96 | $page->ParentID = $parentid; |
||
97 | $page->writeToStage('Stage'); |
||
98 | $page->publishSingle(); |
||
99 | DB::alteration_message('Cart page created', 'created'); |
||
100 | } |
||
101 | |||
102 | //checkout page |
||
103 | if (!$page = CheckoutPage::get()->first()) { |
||
104 | $fixture = YamlFixture::create($fixtureDir . '/pages/Checkout.yml'); |
||
105 | $fixture->writeInto($factory); |
||
106 | $page = $factory->get(CheckoutPage::class, 'checkout'); |
||
107 | $page->ParentID = $parentid; |
||
108 | $page->writeToStage('Stage'); |
||
109 | $page->publishSingle(); |
||
110 | DB::alteration_message('Checkout page created', 'created'); |
||
111 | } |
||
112 | |||
113 | //account page |
||
114 | if (!AccountPage::get()->first()) { |
||
115 | $fixture = YamlFixture::create($fixtureDir . '/pages/Account.yml'); |
||
116 | $fixture->writeInto($factory); |
||
117 | $page = $factory->get(AccountPage::class, 'account'); |
||
118 | $page->ParentID = $parentid; |
||
119 | $page->writeToStage('Stage'); |
||
120 | $page->publishSingle(); |
||
121 | DB::alteration_message('Account page \'Account\' created', 'created'); |
||
122 | } |
||
123 | |||
124 | //terms page |
||
125 | if (!$termsPage = Page::get()->filter('URLSegment', 'terms-and-conditions')->first()) { |
||
126 | $fixture = YamlFixture::create($fixtureDir . '/pages/TermsConditions.yml'); |
||
127 | $fixture->writeInto($factory); |
||
128 | $page = $factory->get('Page', 'termsconditions'); |
||
129 | $page->ParentID = $parentid; |
||
130 | $page->writeToStage('Stage'); |
||
131 | $page->publishSingle(); |
||
132 | //set terms page id in config |
||
133 | $config = SiteConfig::current_site_config(); |
||
134 | $config->TermsPageID = $page->ID; |
||
135 | $config->write(); |
||
136 | DB::alteration_message('Terms and conditions page created', 'created'); |
||
137 | } |
||
138 | |||
139 | //countries config - removes some countries |
||
140 | $siteconfig = SiteConfig::current_site_config(); |
||
141 | if (empty($siteconfig->AllowedCountries)) { |
||
142 | $siteconfig->AllowedCountries = |
||
143 | 'AF,AL,DZ,AS,AD,AO,AG,AR,AM,AU,AT,AZ,BS,BH, |
||
144 | BD,BB,BY,BE,BZ,BJ,BT,BO,BA,BW,BR,BN,BG,BF,BI, |
||
145 | KH,CM,CA,CV,CF,TD,CL,CN,CO,KM,CG,CR,CI,HR,CU, |
||
146 | CY,CZ,DK,DJ,DM,DO,EC,EG,SV,GQ,ER,EE,ET,FJ,FI, |
||
147 | FR,GA,GM,GE,DE,GH,GR,GD,GT,GN,GW,GY,HT,HN,HK, |
||
148 | HU,IS,IN,ID,IR,IQ,IE,IL,IT,JM,JP,JO,KZ,KE,KI, |
||
149 | KP,KR,KW,KG,LA,LV,LB,LS,LR,LY,LI,LT,LU,MG,MW, |
||
150 | MY,MV,ML,MT,MH,MR,MU,MX,FM,MD,MC,MN,MS,MA,MZ, |
||
151 | MM,NA,NR,NP,NL,NZ,NI,NE,NG,NO,OM,PK,PW,PA,PG, |
||
152 | PY,PE,PH,PL,PT,QA,RO,RU,RW,KN,LC,VC,WS,SM,ST, |
||
153 | SA,SN,SC,SL,SG,SK,SI,SB,SO,ZA,ES,LK,SD,SR,SZ, |
||
154 | SE,CH,SY,TJ,TZ,TH,TG,TO,TT,TN,TR,TM,TV,UG,UA, |
||
155 | AE,GB,US,UY,UZ,VU,VE,VN,YE,YU,ZM,ZW'; |
||
156 | $siteconfig->write(); |
||
157 | } |
||
158 | $this->extend('afterPopulate'); |
||
159 | } |
||
160 | } |
||
161 |