-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.driver.php
133 lines (110 loc) · 4.03 KB
/
extension.driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
Class extension_datasourcefield extends Extension{
// Set the delegates:
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'checkDependency'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'checkDependency'
),
);
}
public function __getPageDatasources($id) {
$result = Symphony::Database()->fetch("
SELECT
p.data_sources
FROM
tbl_pages AS p
WHERE
p.id = '{$id}'
");
return explode(',',$result[0]['data_sources']);
}
public function __getRelatedDatasources($fieldid,$entryids) {
$where = "p.entry_id = '{$entryids}'";
if (is_array($entryids)){
$wheres = array();
foreach ($entryids as $entryid){
$wheres[] = "p.entry_id = '{$entryid}'";
}
$where = implode(' or ', $wheres);
}
$result = Symphony::Database()->fetchCol('handle',"
SELECT
p.handle
FROM
tbl_entries_data_{$fieldid} AS p
WHERE
{$where}
");
return $result;
}
public function checkDependency($context) {
//this section id - useful to check if pages field exists here
$sectionid = $context['section']->get('id');
//related section ids - usefull to check if field is included here
$relatedSections = $context['section']->fetchAssociatedSections();
//if no related sections to this section then nothing to update
if (count($relatedSections) == 0) return;
//check if field is in related sections.
// $fieldManager = new FieldManager(Symphony::Engine());
$fields = FieldManager::fetch(NULL, NULL, 'ASC', 'sortorder', 'datasource');
//if no datasource fields then we do not have anything to update
if (count($fields) == 0) return;
$datasroucefield_id = current($fields)->get('id');
//check that this DS has also a pagefield
$pagesField = $context['section']->fetchFields('pages');
if (count($pagesField) == 0) return;
$pagesFieldID = current($pagesField)->get('id');
//obtain link field info (subsection manager or selectbox link
$subsectionField = $context['section']->fetchFields('subsectionmanager');
$selectboxField = $context['section']->fetchFields('selectbox_link');
//if no subsection and no selectbox then return nothing to do
if (count($subsectionField) == 0 && count($selectboxField) == 0 ) return;
//if selectbox empty then use subsection
if (count($selectboxField) == 0 )
$linkFieldID = current($subsectionField)->get('id');
else $linkFieldID = current($selectboxField)->get('id');
//get subsection entry data
$subsectionData = $context['entry']->getData($linkFieldID);
$relatedEntries = $subsectionData['relation_id'];
//get page entry data
$pageData = $context['entry']->getData($pagesFieldID);
$page = $pageData['page_id'][0];
//get all existing page datasources
$pagedatasources = $this->__getPageDatasources($page);
//add related datasources to pagedatasources
$relatedDatasources = $this->__getRelatedDatasources($datasroucefield_id,$relatedEntries);
$mergedDatasources = array_merge ( $pagedatasources, $relatedDatasources );
$newDatasources = array_unique($mergedDatasources);
// create the fields to be updated
$fields = array('data_sources' => @implode(',', $newDatasources));
// update the fields
if (!Symphony::Database()->update($fields, 'tbl_pages', "`id` = '$page'")) {
$error = true;
break;
}
}
public function uninstall(){
Symphony::Database()->query("DROP TABLE `tbl_fields_datasource`");
}
public function install(){
return Symphony::Database()->query("CREATE TABLE `tbl_fields_datasource` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`allow_multiple_selection` enum('yes','no') NOT NULL default 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) ENGINE=MyISAM");
}
public function update($previousVersion) {
return true;
}
}