PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/actions/mutate_snippet.dynamic.php

https://github.com/good-web-master/modx.evo.custom
PHP | 402 lines | 363 code | 30 blank | 9 comment | 55 complexity | d84b9c2ec888ecb8416ac4d086bc2a1b MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. if(IN_MANAGER_MODE!="true") die("<b>INCLUDE_ORDERING_ERROR</b><br /><br />Please use the MODx Content Manager instead of accessing this file directly.");
  3. switch((int) $_REQUEST['a']) {
  4. case 22:
  5. if(!$modx->hasPermission('edit_snippet')) {
  6. $e->setError(3);
  7. $e->dumpError();
  8. }
  9. break;
  10. case 23:
  11. if(!$modx->hasPermission('new_snippet')) {
  12. $e->setError(3);
  13. $e->dumpError();
  14. }
  15. break;
  16. default:
  17. $e->setError(3);
  18. $e->dumpError();
  19. }
  20. $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
  21. if ($manager_theme)
  22. $manager_theme .= '/';
  23. else $manager_theme = '';
  24. // Get table Names (alphabetical)
  25. $tbl_active_users = $modx->getFullTableName('active_users');
  26. $tbl_site_module_depobj = $modx->getFullTableName('site_module_depobj');
  27. $tbl_site_modules = $modx->getFullTableName('site_modules');
  28. $tbl_site_snippets = $modx->getFullTableName('site_snippets');
  29. // check to see the snippet editor isn't locked
  30. $sql = 'SELECT internalKey, username FROM '.$tbl_active_users.' WHERE action=22 AND id='.$id;
  31. $rs = mysql_query($sql);
  32. $limit = mysql_num_rows($rs);
  33. if($limit>1) {
  34. for ($i=0;$i<$limit;$i++) {
  35. $lock = mysql_fetch_assoc($rs);
  36. if($lock['internalKey']!=$modx->getLoginUserID()) {
  37. $msg = sprintf($_lang['lock_msg'],$lock['username'],"snippet");
  38. $e->setError(5, $msg);
  39. $e->dumpError();
  40. }
  41. }
  42. }
  43. // end check for lock
  44. if(isset($_GET['id'])) {
  45. $sql = 'SELECT * FROM '.$tbl_site_snippets.' WHERE id='.$id;
  46. $rs = mysql_query($sql);
  47. $limit = mysql_num_rows($rs);
  48. if($limit>1) {
  49. echo "Oops, Multiple snippets sharing same unique id. Not good.<p>";
  50. exit;
  51. }
  52. if($limit<1) {
  53. header("Location: /index.php?id=".$site_start);
  54. }
  55. $content = mysql_fetch_assoc($rs);
  56. $_SESSION['itemname']=$content['name'];
  57. if($content['locked']==1 && $_SESSION['mgrRole']!=1) {
  58. $e->setError(3);
  59. $e->dumpError();
  60. }
  61. } else {
  62. $_SESSION['itemname']="New snippet";
  63. }
  64. ?>
  65. <script type="text/javascript">
  66. function duplicaterecord(){
  67. if(confirm("<?php echo $_lang['confirm_duplicate_record']?>")==true) {
  68. documentDirty=false;
  69. document.location.href="index.php?id=<?php echo $_REQUEST['id']?>&a=98";
  70. }
  71. }
  72. function deletedocument() {
  73. if(confirm("<?php echo $_lang['confirm_delete_snippet']?>")==true) {
  74. documentDirty=false;
  75. document.location.href="index.php?id=" + document.mutate.id.value + "&a=25";
  76. }
  77. }
  78. function setTextWrap(ctrl,b){
  79. if(!ctrl) return;
  80. ctrl.wrap = (b)? "soft":"off";
  81. }
  82. // Current Params
  83. var currentParams = {};
  84. function showParameters(ctrl) {
  85. var c,p,df,cp;
  86. var ar,desc,value,key,dt;
  87. currentParams = {}; // reset;
  88. if (ctrl) {
  89. f = ctrl.form;
  90. } else {
  91. f= document.forms['mutate'];
  92. if(!f) return;
  93. }
  94. // setup parameters
  95. tr = (document.getElementById) ? document.getElementById('displayparamrow'):document.all['displayparamrow'];
  96. dp = (f.properties.value) ? f.properties.value.split("&"):"";
  97. if(!dp) tr.style.display='none';
  98. else {
  99. t='<table width="300" style="margin-bottom:3px;margin-left:14px;background-color:#EEEEEE" cellpadding="2" cellspacing="1"><thead><tr><td width="50%"><?php echo $_lang['parameter']?></td><td width="50%"><?php echo $_lang['value']?></td></tr></thead>';
  100. for(p = 0; p < dp.length; p++) {
  101. dp[p]=(dp[p]+'').replace(/^\s|\s$/,""); // trim
  102. ar = dp[p].split("=");
  103. key = ar[0] // param
  104. ar = (ar[1]+'').split(";");
  105. desc = ar[0]; // description
  106. dt = ar[1]; // data type
  107. value = decode((ar[2])? ar[2]:'');
  108. // store values for later retrieval
  109. if (key && dt=='list') currentParams[key] = [desc,dt,value,ar[3]];
  110. else if (key) currentParams[key] = [desc,dt,value];
  111. if (dt) {
  112. switch(dt) {
  113. case 'int':
  114. c = '<input type="text" name="prop_'+key+'" value="'+value+'" size="30" onchange="setParameter(\''+key+'\',\''+dt+'\',this)" />';
  115. break;
  116. case 'menu':
  117. value = ar[3];
  118. c = '<select name="prop_'+key+'" style="width:168px" onchange="setParameter(\''+key+'\',\''+dt+'\',this)">';
  119. ls = (ar[2]+'').split(",");
  120. if(currentParams[key]==ar[2]) currentParams[key] = ls[0]; // use first list item as default
  121. for(i=0;i<ls.length;i++){
  122. c += '<option value="'+ls[i]+'"'+((ls[i]==value)? ' selected="selected"':'')+'>'+ls[i]+'</option>';
  123. }
  124. c += '</select>';
  125. break;
  126. case 'list':
  127. value = ar[3];
  128. ls = (ar[2]+'').split(",");
  129. if(currentParams[key]==ar[2]) currentParams[key] = ls[0]; // use first list item as default
  130. c = '<select name="prop_'+key+'" size="'+ls.length+'" style="width:168px" onchange="setParameter(\''+key+'\',\''+dt+'\',this)">';
  131. for(i=0;i<ls.length;i++){
  132. c += '<option value="'+ls[i]+'"'+((ls[i]==value)? ' selected="selected"':'')+'>'+ls[i]+'</option>';
  133. }
  134. c += '</select>';
  135. break;
  136. case 'list-multi':
  137. value = (ar[3]+'').replace(/^\s|\s$/,"");
  138. arrValue = value.split(",")
  139. ls = (ar[2]+'').split(",");
  140. if(currentParams[key]==ar[2]) currentParams[key] = ls[0]; // use first list item as default
  141. c = '<select name="prop_'+key+'" size="'+ls.length+'" multiple="multiple" style="width:168px" onchange="setParameter(\''+key+'\',\''+dt+'\',this)">';
  142. for(i=0;i<ls.length;i++){
  143. if(arrValue.length){
  144. for(j=0;j<arrValue.length;j++){
  145. if(ls[i]==arrValue[j]){
  146. c += '<option value="'+ls[i]+'" selected="selected">'+ls[i]+'</option>';
  147. }else{
  148. c += '<option value="'+ls[i]+'">'+ls[i]+'</option>';
  149. }
  150. }
  151. }else{
  152. c += '<option value="'+ls[i]+'">'+ls[i]+'</option>';
  153. }
  154. }
  155. c += '</select>';
  156. break;
  157. case 'textarea':
  158. c = '<textarea class="phptextarea" name="prop_'+key+'" cols="50" rows="4" onchange="setParameter(\''+key+'\',\''+dt+'\',this)">'+value+'</textarea>';
  159. break;
  160. default: // string
  161. c = '<input type="text" name="prop_'+key+'" value="'+value+'" size="30" onchange="setParameter(\''+key+'\',\''+dt+'\',this)" />';
  162. break;
  163. }
  164. t +='<tr><td bgcolor="#FFFFFF" width="50%">'+desc+'</td><td bgcolor="#FFFFFF" width="50%">'+c+'</td></tr>';
  165. };
  166. }
  167. t+='</table>';
  168. td = (document.getElementById) ? document.getElementById('displayparams'):document.all['displayparams'];
  169. td.innerHTML = t;
  170. tr.style.display='';
  171. }
  172. implodeParameters();
  173. }
  174. function setParameter(key,dt,ctrl) {
  175. var v;
  176. if(!ctrl) return null;
  177. switch (dt) {
  178. case 'int':
  179. ctrl.value = parseInt(ctrl.value);
  180. if(isNaN(ctrl.value)) ctrl.value = 0;
  181. v = ctrl.value;
  182. break;
  183. case 'menu':
  184. v = ctrl.options[ctrl.selectedIndex].value;
  185. currentParams[key][3] = v;
  186. implodeParameters();
  187. return;
  188. break;
  189. case 'list':
  190. v = ctrl.options[ctrl.selectedIndex].value;
  191. currentParams[key][3] = v;
  192. implodeParameters();
  193. return;
  194. break;
  195. case 'list-multi':
  196. var arrValues = new Array;
  197. for(var i=0; i < ctrl.options.length; i++){
  198. if(ctrl.options[i].selected){
  199. arrValues.push(ctrl.options[i].value);
  200. }
  201. }
  202. currentParams[key][3] = arrValues.toString();
  203. implodeParameters();
  204. return;
  205. break;
  206. default:
  207. v = ctrl.value+'';
  208. break;
  209. }
  210. currentParams[key][2] = v;
  211. implodeParameters();
  212. }
  213. // implode parameters
  214. function implodeParameters(){
  215. var v, p, s='';
  216. for(p in currentParams){
  217. if(currentParams[p]) {
  218. v = currentParams[p].join(";");
  219. if(s && v) s+=' ';
  220. if(v) s += '&'+p+'='+ v;
  221. }
  222. }
  223. document.forms['mutate'].properties.value = s;
  224. }
  225. function encode(s){
  226. s=s+'';
  227. s = s.replace(/\=/g,'%3D'); // =
  228. s = s.replace(/\&/g,'%26'); // &
  229. return s;
  230. }
  231. function decode(s){
  232. s=s+'';
  233. s = s.replace(/\%3D/g,'='); // =
  234. s = s.replace(/\%26/g,'&'); // &
  235. return s;
  236. }
  237. </script>
  238. <form name="mutate" method="post" action="index.php?a=24">
  239. <?php
  240. // invoke OnSnipFormPrerender event
  241. $evtOut = $modx->invokeEvent("OnSnipFormPrerender",array("id" => $id));
  242. if(is_array($evtOut)) echo implode("",$evtOut);
  243. ?>
  244. <input type="hidden" name="id" value="<?php echo $content['id']?>">
  245. <input type="hidden" name="mode" value="<?php echo $_GET['a']?>">
  246. <div id="actions">
  247. <ul class="actionButtons">
  248. <li id="Button1">
  249. <a href="#" onclick="documentDirty=false; document.mutate.save.click();saveWait('mutate');">
  250. <img src="<?php echo $_style["icons_save"]?>" /> <?php echo $_lang['save']?>
  251. </a>
  252. <span class="and"> + </span>
  253. <select id="stay" name="stay">
  254. <option id="stay1" value="1" <?php echo $_REQUEST['stay']=='1' ? ' selected=""' : ''?> ><?php echo $_lang['stay_new']?></option>
  255. <option id="stay2" value="2" <?php echo $_REQUEST['stay']=='2' ? ' selected="selected"' : ''?> ><?php echo $_lang['stay']?></option>
  256. <option id="stay3" value="" <?php echo $_REQUEST['stay']=='' ? ' selected=""' : ''?> ><?php echo $_lang['close']?></option>
  257. </select>
  258. </li>
  259. <?php
  260. if ($_GET['a'] == '22') { ?>
  261. <li id="Button2"><a href="#" onclick="duplicaterecord();"><img src="media/style/<?php echo $manager_theme?>/images/icons/copy.gif" /> <?php echo $_lang["duplicate"]; ?></a></li>
  262. <li id="Button3" class="disabled"><a href="#" onclick="deletedocument();"><img src="<?php echo $_style["icons_delete_document"] ?>" /> <?php echo $_lang['delete']?></a></li>
  263. <?php } else { ?>
  264. <li id="Button3"><a href="#" onclick="deletedocument();"><img src="<?php echo $_style["icons_delete_document"] ?>" /> <?php echo $_lang['delete']?></a></li>
  265. <?php } ?>
  266. <li id="Button5"><a href="#" onclick="documentDirty=false;document.location.href='index.php?a=76';"><img src="media/style/<?php echo $manager_theme?>/images/icons/stop.png" /> <?php echo $_lang['cancel']?></a></li>
  267. </ul>
  268. </div>
  269. <h1><?php echo $_lang['snippet_title']?></h1>
  270. <div class="sectionBody">
  271. <?php echo $_lang['snippet_msg']?>
  272. <link type="text/css" rel="stylesheet" href="media/style/<?php echo $manager_theme?>style.css<?php echo '?'.$theme_refresher?>" />
  273. <script type="text/javascript" src="media/script/tabpane.js"></script>
  274. <div class="tab-pane" id="snipetPane">
  275. <script type="text/javascript">
  276. tpSnippet = new WebFXTabPane( document.getElementById( "snipetPane"), <?php echo $modx->config['remember_last_tab'] == 1 ? 'true' : 'false'; ?> );
  277. </script>
  278. <!-- General -->
  279. <div class="tab-page" id="tabSnippet">
  280. <h2 class="tab"><?php echo $_lang['settings_general']?></h2>
  281. <script type="text/javascript">tpSnippet.addTabPage( document.getElementById( "tabSnippet" ) );</script>
  282. <table border="0" cellspacing="0" cellpadding="0">
  283. <tr>
  284. <td align="left"><?php echo $_lang['snippet_name']?>:</td>
  285. <td align="left"><span style="font-family:'Courier New', Courier, mono">[[</span><input name="name" type="text" maxlength="100" value="<?php echo htmlspecialchars($content['name'])?>" class="inputBox" style="width:150px;" onChange="documentDirty=true;"><span style="font-family:'Courier New', Courier, mono">]]</span><span class="warning" id="savingMessage">&nbsp;</span></td>
  286. </tr>
  287. <tr>
  288. <td align="left" style="padding-top:10px"><?php echo $_lang['snippet_desc']?>:&nbsp;&nbsp;</td>
  289. <td align="left" style="padding-top:10px"><span style="font-family:'Courier New', Courier, mono">&nbsp;&nbsp;</span><input name="description" type="text" maxlength="255" value="<?php echo $content['description']?>" class="inputBox" style="width:300px;" onChange="documentDirty=true;"></td>
  290. </tr>
  291. <tr>
  292. <td style="padding-top:10px" align="left" valign="top" colspan="2"><input style="padding:0;margin:0;" name="locked" type="checkbox" <?php echo $content['locked']==1 ? "checked='checked'" : ""?> class="inputBox"> <?php echo $_lang['lock_snippet']?> <span class="comment"><?php echo $_lang['lock_snippet_msg']?></span></td>
  293. </tr>
  294. </table>
  295. <!-- PHP text editor start -->
  296. <div style="width:100%;position:relative">
  297. <div style="padding:1px 1px 5px 1px; width:100%; height:16px;background-color:#eeeeee; border-top:1px solid #e0e0e0;margin-top:5px">
  298. <span style="float:left;color:#707070;font-weight:bold; padding:3px">&nbsp;<?php echo $_lang['snippet_code']?></span>
  299. <span style="float:right;color:#707070;"><?php echo $_lang['wrap_lines']?><input name="wrap" type="checkbox" <?php echo $content['wrap']== 1 ? "checked='checked'" : ""?> class="inputBox" onclick="setTextWrap(document.mutate.post,this.checked)" /></span>
  300. </div>
  301. <textarea dir="ltr" name="post" class="phptextarea" style="width:100%; height:370px;" wrap="<?php echo $content['wrap']== 1 ? "soft" : "off"?>" onchange="documentDirty=true;"><?php echo "<?php"."\n".trim(htmlspecialchars($content['snippet']))."\n"."?>"?></textarea>
  302. </div>
  303. <!-- PHP text editor end -->
  304. </div>
  305. <!-- Properties -->
  306. <div class="tab-page" id="tabProps">
  307. <h2 class="tab"><?php echo $_lang['settings_properties']?></h2>
  308. <script type="text/javascript">tpSnippet.addTabPage( document.getElementById( "tabProps" ) );</script>
  309. <table width="90%" border="0" cellspacing="0" cellpadding="0">
  310. <tr>
  311. <td align="left"><?php echo $_lang['existing_category']?>:&nbsp;&nbsp;</td>
  312. <td align="left"><span style="font-family:'Courier New', Courier, mono">&nbsp;&nbsp;</span><select name="categoryid" style="width:300px;" onChange="documentDirty=true;">
  313. <option>&nbsp;</option>
  314. <?php
  315. include_once "categories.inc.php";
  316. $ds = getCategories();
  317. if($ds) foreach($ds as $n=>$v){
  318. echo '<option value="'.$v['id'].'"'.($content['category']==$v['id']? ' selected="selected"':'').'>'.htmlspecialchars($v['category']).'</option>';
  319. }
  320. ?>
  321. </select>
  322. </td>
  323. </tr>
  324. <tr>
  325. <td align="left" valign="top" style="padding-top:10px;"><?php echo $_lang['new_category']?>:</td>
  326. <td align="left" valign="top" style="padding-top:10px;"><span style="font-family:'Courier New', Courier, mono">&nbsp;&nbsp;</span><input name="newcategory" type="text" maxlength="45" value="" class="inputBox" style="width:300px;" onChange="documentDirty=true;"></td>
  327. </tr>
  328. <tr>
  329. <td align="left" style="padding-top:10px;"><?php echo $_lang['import_params']?>:&nbsp;&nbsp;</td>
  330. <td align="left" valign="top" style="padding-top:10px;"><span style="font-family:'Courier New', Courier, mono">&nbsp;&nbsp;</span><select name="moduleguid" style="width:300px;" onChange="documentDirty=true;">
  331. <option>&nbsp;</option>
  332. <?php
  333. $sql = 'SELECT sm.id,sm.name,sm.guid '.
  334. 'FROM '.$tbl_site_modules.' AS sm '.
  335. 'INNER JOIN '.$tbl_site_module_depobj.' AS smd ON smd.module=sm.id AND smd.type=40 '.
  336. 'INNER JOIN '.$tbl_site_snippets.' AS ss ON ss.id=smd.resource '.
  337. 'WHERE smd.resource=\''.$id.'\' AND sm.enable_sharedparams=\'1\' '.
  338. 'ORDER BY sm.name';
  339. $ds = $modx->dbQuery($sql);
  340. if($ds) while($row = $modx->fetchRow($ds)){
  341. echo "<option value='".$row['guid']."'".($content['moduleguid']==$row['guid']? " selected='selected'":"").">".htmlspecialchars($row['name'])."</option>";
  342. }
  343. ?>
  344. </select>
  345. </td>
  346. </tr>
  347. <tr>
  348. <td>&nbsp;</td>
  349. <td align="left" valign="top" style="padding-left:1.3em;"><span class="comment" ><?php echo $_lang['import_params_msg']?></div><br /><br /></td>
  350. </tr>
  351. <tr>
  352. <td align="left" valign="top"><?php echo $_lang['snippet_properties']?>:</td>
  353. <td align="left" valign="top"><span style="font-family:'Courier New', Courier, mono">&nbsp;&nbsp;</span><input name="properties" type="text" maxlength="65535" value="<?php echo $content['properties']?>" class="inputBox phptextarea" style="width:300px;" onChange="showParameters(this);documentDirty=true;"></td>
  354. </tr>
  355. <tr id="displayparamrow">
  356. <td valign="top" align="left">&nbsp;</td>
  357. <td align="left" id="displayparams">&nbsp;</td>
  358. </tr>
  359. </table>
  360. </div>
  361. </div>
  362. <input type="submit" name="save" style="display:none">
  363. </div>
  364. <?php
  365. // invoke OnSnipFormRender event
  366. $evtOut = $modx->invokeEvent("OnSnipFormRender",array("id" => $id));
  367. if(is_array($evtOut)) echo implode("",$evtOut);
  368. ?>
  369. </form>
  370. <script type="text/javascript">
  371. setTimeout('showParameters();',10);
  372. </script>