PageRenderTime 51ms CodeModel.GetById 25ms app.highlight 19ms RepoModel.GetById 0ms app.codeStats 1ms

/htroot/manager/actions/mutate_snippet.dynamic.php

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