-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
155 lines (103 loc) · 3.61 KB
/
functions.php
File metadata and controls
155 lines (103 loc) · 3.61 KB
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
<?
//returns an array with the value of the TV and at what level was found. Level is used if multiple levels are configured in the TV
function TR_getTvValueAndLevel($startId, $templateRulesTvID){
global $modx;
//init level
$level = 0;
//init the TV value
$tplVal = '';
//find the first parent that has an defaultTemplate TV and on which level
while($startId){
//increase level
$level++;
//check if the parent has the defaultTemplate TV
$sql = "SELECT * FROM ".$modx->db->config['table_prefix']."site_tmplvar_contentvalues WHERE tmplvarid = " . $templateRulesTvID . " AND contentid = " . $startId;
$rs= $modx->dbQuery($sql);
$row = $modx->fetchRow($rs);
if($row && $row['value'] != ''){
//stop while loop
$startId = 0;
//save TV value
$tplVal = $row['value'];
}else{
//get next parent
$sql = "SELECT parent FROM ".$modx->db->config['table_prefix']."site_content WHERE id = " . $startId;
$rs= $modx->dbQuery($sql);
$row = $modx->fetchRow($rs);
$startId = $row['parent'];
}
}
$retval = array("value"=>$tplVal, "level"=>$level);
return $retval;
}
// gets the value of the default teplate. Used when a new document/resource is created
function TR_getDefaultTemplate($tvValueAndLevel){
$tvValue = $tvValueAndLevel['value'];
$level = $tvValueAndLevel['level'];
$template = 'x';
//set template to the defaultTemplate value
$aTplVals = explode(",",$tvValue);
if(isset($aTplVals[$level-1])){
$defaultTemplateInfo = $aTplVals[$level-1];
$hasAllowed = strpos($defaultTemplateInfo, '[');
if($hasAllowed){//parse allowed templates
$aAllowedTemplatesTemp = explode("[",$defaultTemplateInfo);
$template = intval($aAllowedTemplatesTemp[0]);
}else{ //no allowed templates functionality
$template = intval($defaultTemplateInfo);
}
}else{
//NTD
}
return $template;
}
//gets the js code for showing only configured templates
function TR_getJsCode($tvValueAndLevel, $documentTemplateId = ''){
$tvValue = $tvValueAndLevel['value'];
$level = $tvValueAndLevel['level'];
$jsOutput = '';
$aTplVals = explode(",",$tvValue);
if(isset($aTplVals[$level-1])){
$defaultTemplateInfo = $aTplVals[$level-1];
$hasAllowed = strpos($defaultTemplateInfo, '[');
if($hasAllowed){//parse allowed templates
$aAllowedTemplatesTemp = explode("[",$defaultTemplateInfo);
//prepare list for javascript, add default id and switch | to , for javascript array compatibility
if($documentTemplateId){
$allowedTemplatesList = '[' . $documentTemplateId . ',' . $aAllowedTemplatesTemp[0] . ',' . str_replace("|", ",", $aAllowedTemplatesTemp[1]);
}else{
$allowedTemplatesList = '[' . $aAllowedTemplatesTemp[0] . ',' . str_replace("|", ",", $aAllowedTemplatesTemp[1]);
}
//javascript for hiding unwanted templates in drop down
$jsOutput = <<<DTJS
<script type="text/javascript">
window.addEvent('domready', function() {
var allowedTemplatesList = $allowedTemplatesList;
//remove unwanted templates from dropdown list
$$("#template option").each(function(opt){
if( allowedTemplatesList.contains( parseInt(opt.getProperty('value')) ) ){
//nothing to do
}else{
opt.remove();
}
});
//remove empty optGroups
$$("#template optgroup").each(function(optGr){
if(optGr.getText().trim() == ""){
optGr.remove();
}else{
//nothing to do
}
});
});
</script>
DTJS;
}else{
//NTD - no allowed templates functionality
}
}else{
//NTD
}
return $jsOutput;
}
?>