This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.knowitop-checklist.php
executable file
·180 lines (153 loc) · 6.16 KB
/
main.knowitop-checklist.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Checklist plugin
*
* @copyright Copyright (C) 2017 Vladimir Kunin <[email protected]>
*/
class ChecklistPlugin implements iApplicationUIExtension, iApplicationObjectExtension
{
const MODULE_NAME = 'knowitop-checklist';
public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false)
{
}
public function OnDisplayRelations($oObject, WebPage $oPage, $bEditMode = false)
{
if ($this->IsTargetObject($oObject) && !$oObject->IsNew()) {
$this->DisplayChecklists($oObject, $oPage, $bEditMode);
}
}
public function OnFormSubmit($oObject, $sFormPrefix = '')
{
}
public function OnFormCancel($sTempId)
{
}
public function EnumUsedAttributes($oObject)
{
return array();
}
public function GetIcon($oObject)
{
return '';
}
public function GetHilightClass($oObject)
{
return HILIGHT_CLASS_NONE;
}
public function EnumAllowedActions(\DBObjectSet $oSet)
{
return array();
}
public function OnIsModified($oObject)
{
return false;
}
public function OnCheckToWrite($oObject)
{
return array();
}
public function OnCheckToDelete($oObject)
{
return array();
}
public function OnDBUpdate($oObject, $oChange = null)
{
if ($this->IsTargetObject($oObject)) {
$oSearch = DBObjectSearch::FromOQL("SELECT Checklist WHERE obj_class = :class AND obj_key = :key");
$oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'key' => $oObject->GetKey()));
while ($oChecklist = $oSet->Fetch()) {
$oChecklist->SetHostObject($oObject, true);
}
}
}
public function OnDBInsert($oObject, $oChange = null)
{
}
public function OnDBDelete($oObject, $oChange = null)
{
if ($this->IsTargetObject($oObject)) {
$oSearch = DBObjectSearch::FromOQL("SELECT Checklist WHERE obj_class = :class AND obj_key = :key");
$oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'key' => $oObject->GetKey()));
while ($oChecklist = $oSet->Fetch()) {
$oChecklist->DBDelete();
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Plug-ins specific functions
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
protected function IsTargetObject($oObject)
{
$aAllowedClasses = \MetaModel::GetModuleSetting(self::MODULE_NAME, 'allowed_classes', array());
foreach ($aAllowedClasses as $sAllowedClass) {
if ($oObject instanceof $sAllowedClass) {
return true;
}
}
return false;
}
public static function IsEditInPlaceAllowed()
{
return MetaModel::GetModuleSetting(ChecklistPlugin::MODULE_NAME, 'edit_in_place', true);
}
public static function IsHostObjEditAllowed($oHost)
{
return UserRights::IsActionAllowed(get_class($oHost), UR_ACTION_MODIFY, DBObjectSet::FromObject($oHost));
}
public function DisplayChecklists(\DBObject $oHost, \iTopWebPage $oPage, $bEditMode = false)
{
$bEditMode = $bEditMode || self::IsEditInPlaceAllowed();
$oPage->add_linked_stylesheet(\utils::GetAbsoluteUrlModulesRoot() . self::MODULE_NAME . '/css/checklist.css');
$oSearch = \DBObjectSearch::FromOQL("SELECT Checklist WHERE obj_class = :class AND obj_key = :obj_key");
$oSet = new \DBObjectSet($oSearch, array(), array('class' => get_class($oHost), 'obj_key' => $oHost->GetKey()));
// Display tab and header
$sTabLabel = $oSet->Count() > 0 ? \Dict::Format('Checklist:TabTitle_Count', $oSet->Count()) : \Dict::S('Checklist:EmptyTabTitle');
$oPage->SetCurrentTab($sTabLabel);
$oPage->p(\MetaModel::GetClassIcon('Checklist') . ' ' . \Dict::S('Checklist:TabTitle+'));
// Display checklists
while ($oChecklist = $oSet->Fetch()) {
$oPage->add($oChecklist->Render($bEditMode));
}
// Display actions if current user has rights to modify host object
if (self::IsHostObjEditAllowed($oHost)) {
if ($bEditMode) {
$sNewListDiv = el('div.checklist-new-list', null, array(
el('a.checklist-btn', array('data-checklist-action' => 'create_list'), \Dict::S('Checklist:Button:NewList')),
el('a.checklist-btn', array('data-checklist-action' => 'create_list_from_template'), \Dict::S('Checklist:Button:NewListFromTmpl')),
));
$oPage->add($sNewListDiv);
}
$oPage->add_dict_entry('UI:Checklist:DlgPickATemplate');
$oPage->add_dict_entry('UI:Checklist:DeleteDlg:Title');
$oPage->add_dict_entry('UI:Checklist:DeleteDlg:Msg');
$oPage->add_dict_entry('UI:Checklist:DeleteDlg:Delete');
$oPage->add_dict_entry('UI:Checklist:DeleteDlg:Cancel');
$oPage->add_dict_entry('UI:ChecklistItem:DeleteDlg:Title');
$oPage->add_dict_entry('UI:ChecklistItem:DeleteDlg:Msg');
$oPage->add_dict_entry('UI:ChecklistItem:DeleteDlg:Delete');
$oPage->add_dict_entry('UI:ChecklistItem:DeleteDlg:Cancel');
// Load js scripts
$oPage->add_linked_script("../js/wizardhelper.js");
$sApiUrl = \utils::GetAbsoluteUrlModulesRoot() . self::MODULE_NAME . '/ajax.php';
$iHostObjId = $oHost->GetKey();
$sHostObjClass = get_class($oHost);
$oPage->add_script(
<<<EOF
var checklistApi = new ChecklistAPI({
url: '$sApiUrl',
hostId: $iHostObjId,
hostClass: '$sHostObjClass'
});
EOF
);
$oPage->add_linked_script(\utils::GetAbsoluteUrlModulesRoot() . self::MODULE_NAME . '/js/checklist.js');
}
}
}
// TODO: Checklist history – create, rename, complete, remove...
function el($tag, $attrs = null, $contents = null)
{
return \Knowitop\HtmlRenderer::render($tag, $attrs, $contents);
}