1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Taxonomy\Vocabulary;
13
14 class Model extends \ICanBoogie\ActiveRecord\Model
15 {
16 public function save(array $properties, $key=null, array $options=array())
17 {
18 if (isset($properties['vocabulary']) && empty($properties['vocabularyslug']))
19 {
20 $properties['vocabularyslug'] = \Icybee\slugize($properties['vocabulary']);
21 }
22
23 if (isset($properties['vocabularyslug']))
24 {
25 $properties['vocabularyslug'] = \ICanBoogie\normalize($properties['vocabularyslug']);
26 }
27
28 $key = parent::save($properties, $key, $options);
29
30 if (!$key)
31 {
32 return $key;
33 }
34
35 $scope = array();
36
37 if (isset($properties['scope']))
38 {
39 $insert = $this->prepare('INSERT IGNORE INTO {self}__scopes (vid, constructor) VALUES(?, ?)');
40
41 foreach ($properties['scope'] as $constructor => $ok)
42 {
43 $ok = filter_var($ok, FILTER_VALIDATE_BOOLEAN);
44
45 if (!$ok)
46 {
47 continue;
48 }
49
50 $scope[] = $constructor;
51 $insert->execute(array($key, $constructor));
52 }
53 }
54
55 if ($scope)
56 {
57 $scope = array_map(array($this, 'quote'), $scope);
58
59 $this->execute('DELETE FROM {self}__scopes WHERE vid = ? AND constructor NOT IN(' . implode(',', $scope) . ')', array($key));
60 }
61
62 return $key;
63 }
64
65 public function delete($key)
66 {
67 $rc = parent::delete($key);
68
69 if ($rc)
70 {
71 $this->execute('DELETE FROM {self}__scopes WHERE vid = ?', array($key));
72 $this->clearTerms($key);
73 }
74
75 return $rc;
76 }
77
78 protected function clearTerms($vid)
79 {
80
81
82 global $core;
83
84 $model = $core->models['taxonomy.terms'];
85 $model->execute('DELETE FROM {self}__nodes WHERE (SELECT vid FROM {self} WHERE {self}__nodes.vtid = {self}.vtid) = ?', array($vid));
86 $model->execute('DELETE FROM {self} WHERE vid = ?', array($vid));
87 }
88 }