PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mailz/lists/admin/editattributes.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 191 lines | 160 code | 27 blank | 4 comment | 42 complexity | d9c46102913b33d5e760d33137aaa42c MD5 | raw file
  1. <?php
  2. require_once dirname(__FILE__).'/accesscheck.php';
  3. # $Id: editattributes.php,v 1.3.4.3 2007-07-16 19:18:14 basovink Exp $
  4. $id = !empty($_GET['id']) ? sprintf('%d',$_GET['id']) : 0;
  5. ob_end_flush();
  6. function adminMenu() {
  7. global $adminlevel,$config;
  8. if ($adminlevel == "superuser"){
  9. $html .= menuLink("admins","administrators");
  10. $html .= menuLink("groups","groups");
  11. $html .= menuLink("users","users");
  12. $html .= menuLink("userattributes","user attributes");
  13. $req = Sql_Query('select * from attribute where type = "select" or type = "radio" or type = "checkboxgroup"');
  14. while ($row = Sql_Fetch_Array($req)) {
  15. $html .= menuLink("editattributes&id=".$row["id"],"&gt;&nbsp;".$row["name"]);
  16. }
  17. $html .= menuLink("branches","branch fields","option=branchfields");
  18. $html .= menuLink("templates","templates");
  19. }
  20. return $html;
  21. }
  22. if (!$id) {
  23. Fatal_Error($GLOBALS['I18N']->get('NoAttr')." $id");
  24. return;
  25. }
  26. if (!isset($tables["attribute"])) {
  27. $tables["attribute"] = "attribute";
  28. $tables["user_attribute"] = "user_attribute";
  29. }
  30. if (!isset($table_prefix )) {
  31. $table_prefix = 'phplist_';
  32. }
  33. $res = Sql_Query("select * from $tables[attribute] where id = $id");
  34. $data = Sql_Fetch_array($res);
  35. $table = $table_prefix ."listattr_".$data["tablename"];
  36. switch ($data['type']) {
  37. case 'checkboxgroup':
  38. case 'select':
  39. case 'radio':
  40. break;
  41. default:
  42. print $GLOBALS['I18N']->get('This datatype does not have editable values');
  43. return;
  44. }
  45. ?>
  46. <script language="Javascript" src="js/jslib.js" type="text/javascript"></script>
  47. <br><?php echo PageLink2("editattributes",$GLOBALS['I18N']->get('AddNew'),"id=$id&action=new")?> <?php echo $data["name"]?>
  48. <br><a href="javascript:deleteRec2('<?php echo $GLOBALS['I18N']->get('SureToDeleteAll');?>','<?php echo PageURL2("editattributes",$GLOBALS['I18N']->get('DelAll'),"id=$id&deleteall=yes")?>');"><?php echo $GLOBALS['I18N']->get('DelAll');?></a>
  49. <hr><p>
  50. <?php echo formStart()?>
  51. <input type=hidden name="action" value="add">
  52. <input type=hidden name="id" value="<?php echo $id?>">
  53. <?php
  54. if (isset($_POST["addnew"])) {
  55. $items = explode("\n", $_POST["itemlist"]);
  56. $query = sprintf('SELECT MAX(listorder) AS listorder FROM %s',$table);
  57. $maxitem = Sql_Fetch_Row_Query($query);
  58. if (!Sql_Affected_Rows() || !is_numeric($maxitem[0])) {
  59. $listorder = 1; # insert the listorder as it's in the textarea / start with 1
  60. }
  61. else {
  62. $listorder = $maxitem[0]+1; # One more than the maximun
  63. }
  64. while (list($key,$val) = each($items)) {
  65. $val = clean($val);
  66. if ($val != "") {
  67. $query = sprintf('INSERT into %s (name,listorder) values("%s","%s")',$table,$val,$listorder);
  68. $result = Sql_query($query);
  69. }
  70. $listorder++;
  71. }
  72. }
  73. if (isset($_POST["listorder"]) && is_array($_POST["listorder"])) {
  74. foreach ($_POST["listorder"] as $key => $val) {
  75. Sql_Verbose_Query("update $table set listorder = $val where id = $key");
  76. }
  77. }
  78. function giveAlternative($table,$delete,$attributeid) {
  79. print $GLOBALS['I18N']->get('ReplaceAllWith').formStart();
  80. print '<select name=replace><option value="0">-- '.$GLOBALS['I18N']->get('ReplaceWith').'</option>';
  81. $req = Sql_Query("select * from $table order by listorder,name");
  82. while ($row = Sql_Fetch_array($req))
  83. if ($row["id"] != $delete)
  84. printf('<option value="%d">%s</option>',$row["id"],$row["name"]);
  85. print "</select>";
  86. printf('<input type=hidden name="delete" value="%d">',$delete);
  87. printf('<input type=hidden name="id" value="%d">',$attributeid);
  88. printf('<input type=submit name="deleteandreplace" value="%s"></form>',$GLOBALS['I18N']->get('deleteandreplace'));
  89. }
  90. function deleteItem($table,$attributeid,$delete) {
  91. global $tables,$replace;
  92. # delete the index in delete
  93. $valreq = Sql_Fetch_Row_query("select name from $table where id = $delete");
  94. $val = $valreq[0];
  95. # check dependencies
  96. $dependencies = array();
  97. $result = Sql_query("select distinct userid from $tables[user_attribute] where
  98. attributeid = $attributeid and value = $delete");
  99. while ($row = Sql_fetch_array($result)) {
  100. array_push($dependencies,$row["userid"]);
  101. }
  102. if (sizeof($dependencies) == 0)
  103. $result = Sql_query("delete from $table where id = $delete");
  104. else if ($replace) {
  105. $result = Sql_Query("update $tables[user_attribute] set value = $replace where value = $delete");
  106. $result = Sql_query("delete from $table where id = $delete");
  107. } else {
  108. print $GLOBALS["I18N"]->get("cannotdelete");
  109. print " <b>$val</b><br />";
  110. print $GLOBALS["I18N"]->get("dependentrecords").'<p></p>';
  111. for ($i=0;$i<sizeof($dependencies);$i++) {
  112. print PageLink2("user",$GLOBALS["I18N"]->get("user")." ".$dependencies[$i],"id=$dependencies[$i]")."<br />\n";
  113. if ($i>10) {
  114. print $GLOBALS['I18N']->get('TooManyToList')."
  115. ".sizeof($dependencies)."<br /><br />";
  116. giveAlternative($table,$delete,$attributeid);
  117. return 0;
  118. }
  119. }
  120. print "</p><br />";
  121. giveAlternative($table,$delete,$attributeid);
  122. }
  123. return 1;
  124. }
  125. if (isset($_GET["delete"])) {
  126. deleteItem($table,$id,$_GET["delete"]);
  127. } elseif(isset($_GET["deleteall"])) {
  128. $count = 0;
  129. $errcount = 0;
  130. $res = Sql_Query("select id from $table");
  131. while ($row = Sql_Fetch_Row($res)) {
  132. if (deleteItem($table,$id,$row[0])) {
  133. $count++;
  134. } else {
  135. $errcount++;
  136. if ($errcount > 10) {
  137. print $GLOBALS['I18N']->get('TooManyErrors')."<br /><br /><br />\n";
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. if (isset($_GET["action"]) && $_GET["action"] == "new") {
  144. // ??
  145. ?>
  146. <p><?php echo $GLOBALS["I18N"]->get("addnew")." ".$data["name"].', '.$GLOBALS["I18N"]->get("oneperline") ?><br />
  147. <textarea name="itemlist" rows=20 cols=50></textarea><br />
  148. <input type="Submit" name="addnew" value="<?php echo $GLOBALS["I18N"]->get("addnew")." ".$data["name"] ?>"><br />
  149. <?php
  150. }
  151. $result = Sql_query("SELECT * FROM $table order by listorder,name");
  152. $num = Sql_Affected_Rows();
  153. if ($num < 100 && $num > 25)
  154. printf('<input type=submit name=action value="%s"><br />',$GLOBALS["I18N"]->get("changeorder"));
  155. while ($row = Sql_Fetch_array($result)) {
  156. printf( '<a href="javascript:deleteRec(\'%s\');">'.$GLOBALS['I18N']->get('Delete').'</a> |',PageURL2("editattributes","","id=$id&delete=".$row["id"]));
  157. if ($num < 100)
  158. printf(' <input type=text name="listorder[%d]" value="%s" size=5>',$row["id"],$row["listorder"]);
  159. printf(' %s %s <br />', $row["name"],($row["name"] == $data["default_value"]) ? $GLOBALS['I18N']->get('Default'):"");
  160. }
  161. if ($num && $num < 100)
  162. printf('<input type=submit name=action value="%s">',$GLOBALS["I18N"]->get("changeorder"));
  163. ?>
  164. </form>