-
Notifications
You must be signed in to change notification settings - Fork 7
/
extension.driver.php
143 lines (119 loc) · 4.56 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
133
134
135
136
137
138
139
140
141
142
143
<?php
Class Extension_CustomFieldCaptions extends Extension {
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreGenerate',
'callback' => '__appendAssets'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'FieldPostCreate',
'callback' => '__saveCustomCaptionToField'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'FieldPostEdit',
'callback' => '__saveCustomCaptionToField'
),
array(
'page' => '/blueprints/sections/',
'delegate' => 'SectionPostEdit',
'callback' => '__cleanUp'
)
);
}
public function install(){
return Symphony::Database()->query('
CREATE TABLE IF NOT EXISTS tbl_customcaptions (
`field_id` INT(4) UNSIGNED DEFAULT NULL,
`section_id` INT(4) UNSIGNED DEFAULT NULL,
`caption` TINYTEXT DEFAULT NULL,
PRIMARY KEY (`field_id`),
UNIQUE KEY field_id_section_id (`field_id`, `section_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
');
}
public function uninstall() {
Symphony::Database()->query('DROP TABLE IF EXISTS tbl_customcaptions');
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
private function addContextToPage(array $data = array()) {
if(!empty($data)) {
// Get current Captions and inject into Symphony Context
Administration::instance()->Page->addElementToHead(
new XMLElement(
'script',
"Symphony.Context.add('custom_captions', " . json_encode($data) . ");",
array('type' => 'text/javascript')
), 10000
);
}
}
public function getCustomCaptionsForSection($section_id = null) {
if(!is_null($section_id) && !is_numeric($section_id)) {
$section_id = SectionManager::fetchIDFromHandle($section_id);
}
if(is_null($section_id)) return array();
return Symphony::Database()->fetch(sprintf("
SELECT field_id, caption
FROM tbl_customcaptions
WHERE section_id = %d;",
$section_id
),
'field_id'
);
}
/*-------------------------------------------------------------------------
Delegate Callbacks
-------------------------------------------------------------------------*/
public function __appendAssets(&$context) {
if(class_exists('Administration')
&& Administration::instance() instanceof Administration
&& Administration::instance()->Page instanceof HTMLPage
) {
$callback = Administration::instance()->getPageCallback();
// Section Editor
if($context['oPage'] instanceof contentBlueprintsSections) {
$data = $this->getCustomCaptionsForSection($callback['context'][1]);
$this->addContextToPage($data);
Administration::instance()->Page->addScriptToHead(URL . '/extensions/customfieldcaptions/assets/customfieldcaptions.sections.js', 101, false);
}
// Publish Page
else if($context['oPage'] instanceof contentPublish) {
$data = $this->getCustomCaptionsForSection($callback['context']['section_handle']);
$this->addContextToPage($data);
Administration::instance()->Page->addStylesheetToHead(URL . '/extensions/customfieldcaptions/assets/customfieldcaptions.publish.css', 'all', 101, false);
Administration::instance()->Page->addScriptToHead(URL . '/extensions/customfieldcaptions/assets/customfieldcaptions.publish.js', 102, false);
}
}
}
public function __saveCustomCaptionToField(&$context) {
$field = $context['field'];
$data = array(
'field_id' => $field->get('id'),
'section_id' => $field->get('parent_section'),
'caption' => $field->get('custom_caption')
);
// Save custom caption against this field
return Symphony::Database()->insert($data, 'tbl_customcaptions', true);
}
public function __cleanUp(&$context) {
$section_id = (int)$context['section_id'];
$caption_field_ids = Symphony::Database()->fetchCol("field_id", "SELECT field_id FROM tbl_customcaptions WHERE section_id = " . $section_id);
$section_schema = FieldManager::fetchFieldsSchema($section_id);
$section_field_ids = array();
foreach($section_schema as $field) {
$section_field_ids[] = $field['id'];
}
// If we have any Field ID's that tbl_fields doesn't have
// remove them, as they have been deleted from the section
$field_ids = array_diff($caption_field_ids, $section_field_ids);
if(!empty($field_ids)) {
Symphony::Database()->delete('`tbl_customcaptions`', 'field_id IN (' . implode(',', $field_ids) . ');');
}
}
}