1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\EcommerceGoogleShoppingFeed\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\ORM\DB; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class \Sunnysideup\EcommerceGoogleShoppingFeed\Model\GoogleProductCategory |
10
|
|
|
* |
11
|
|
|
* @property int $GoogleID |
12
|
|
|
* @property string $Title |
13
|
|
|
*/ |
14
|
|
|
class GoogleProductCategory extends DataObject |
15
|
|
|
{ |
16
|
|
|
private static $table_name = 'GoogleProductCategory'; |
17
|
|
|
|
18
|
|
|
private static $db = [ |
19
|
|
|
'GoogleID' => 'Int', |
20
|
|
|
'Title' => 'Varchar(255)', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Build the initial list of Categories. |
25
|
|
|
*/ |
26
|
|
|
public function RequireDefaultRecords() |
27
|
|
|
{ |
28
|
|
|
parent::requireDefaultRecords(); |
29
|
|
|
|
30
|
|
|
if (! GoogleProductCategory::get()->exists()) { |
31
|
|
|
if (! empty($_GET['setup-google-categories'])) { |
32
|
|
|
DB::alteration_message('Creating categories (this may take 5 - 10 mins)', 'created'); |
33
|
|
|
|
34
|
|
|
$default_categories = $this->getGoogleCategories(); |
35
|
|
|
$count = 0; |
36
|
|
|
|
37
|
|
|
foreach ($default_categories as $key => $value) { |
38
|
|
|
$new_cat = GoogleProductCategory::create([ |
39
|
|
|
'GoogleID' => $key, |
40
|
|
|
'Title' => $value, |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$new_cat->write(); |
44
|
|
|
++$count; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
DB::alteration_message("Created {$count} Categories", 'created'); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a list of google shopping categories which are formatted as:. |
54
|
|
|
* |
55
|
|
|
* Key: ID of category |
56
|
|
|
* Value: Full name of category |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getGoogleCategories() |
61
|
|
|
{ |
62
|
|
|
// Get a list of Google Categories from the |
63
|
|
|
// product file. |
64
|
|
|
$file = dirname(__FILE__) . '/../../thirdparty/google_product_taxonomy.txt'; |
65
|
|
|
$fopen = fopen($file, 'r'); |
66
|
|
|
$fread = fread($fopen, filesize($file)); |
67
|
|
|
fclose($fopen); |
68
|
|
|
$result = []; |
69
|
|
|
|
70
|
|
|
foreach (explode("\n", $fread) as $string) { |
71
|
|
|
$exploded = explode(' - ', $string); |
72
|
|
|
if ($string && 2 === count($exploded)) { |
73
|
|
|
$result[$exploded[0]] = $exploded[1]; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function canDelete($member = null) |
81
|
|
|
{ |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|