PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/phplist/admin/commonlib/pages/attributes.php

https://github.com/radicaldesigns/amp
PHP | 368 lines | 289 code | 20 blank | 59 comment | 82 complexity | eed46325d71a73f6624bc974adce905c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?
  2. $types = array('textline','checkbox','checkboxgroup','radio','select',"hidden","textarea","date");
  3. $formtable_exists = Sql_Table_exists("formfield");
  4. ob_end_flush();
  5. #foreach ($_POST as $key => $val) {
  6. # print "$key = ".print_r($val)."<br/>";
  7. #}
  8. #return;
  9. print '<script language="Javascript" src="js/progressbar.js" type="text/javascript"></script>';
  10. if (isset($_POST["action"])) {
  11. if (isset($_POST["name"])) {
  12. print '<script language="Javascript" type="text/javascript"> document.write(progressmeter); start();</script>';flush();
  13. while (list($id,$val) = each ($_POST["name"])) {
  14. if (!$id && isset($_POST["name"][0]) && $_POST["name"][0] != "") {
  15. # it is a new one
  16. $lc_name = getNewAttributeTablename($_POST["name"][0]);
  17. if ($lc_name == "email") { print Warn($GLOBALS['I18N']->get('warnemailattribute')); }
  18. #print "New attribute: ".$_POST["name"][0]."<br/>";
  19. $query = sprintf('insert into %s (name,type,listorder,default_value,required,tablename) values("%s","%s",%d,"%s",%d,"%s")',
  20. $tables["attribute"],addslashes($_POST["name"][0]),$_POST["type"][0],$_POST["listorder"][0],addslashes($_POST["default"][0]),$_POST["required"][0],$lc_name);
  21. Sql_Query($query);
  22. $insertid = Sql_Insert_id();
  23. # text boxes and hidden fields do not have their own table
  24. if ($_POST["type"][$id] != "textline" && $_POST["type"]["id"] != "hidden") {
  25. $query = "create table $table_prefix"."listattr_$lc_name (id integer not null primary key auto_increment, name varchar(255) unique,listorder integer default 0)";
  26. Sql_Query($query);
  27. } else {
  28. # and they cannot currently be required, changed 29/08/01, insert javascript to require them, except for hidden ones :-)
  29. if ($_POST["type"]["id"] == "hidden")
  30. Sql_Query("update {$tables['attribute']} set required = 0 where id = $insertid");
  31. }
  32. if ($_POST["type"][$id] == "checkbox") {
  33. # with a checkbox we know the values
  34. # Sql_Query('insert into '.$table_prefix.'listattr_'.$lc_name.' (name) values("Checked")');
  35. # Sql_Query('insert into '.$table_prefix.'listattr_'.$lc_name.' (name) values("Unchecked")');
  36. # we cannot "require" checkboxes, that does not make sense
  37. Sql_Query("update {$tables['attribute']} set required = 0 where id = $insertid");
  38. }
  39. if ($_POST["type"][$id] == "checkboxgroup")
  40. Sql_Query("update {$tables['attribute']} set required = 0 where id = $insertid");
  41. # fix all existing users to have a record for this attribute, even with empty data
  42. $req = Sql_Query("select id from {$tables["user"]}");
  43. while ($row = Sql_Fetch_Row($req)) {
  44. Sql_Query(sprintf('insert ignore into %s (attributeid,userid) values(%d,%d)',
  45. $tables["user_attribute"],$insertid,$row[0]));
  46. }
  47. } elseif ($_POST["name"][$id] != "") {
  48. # it is a change
  49. # get the original type
  50. $req = Sql_Fetch_Row_Query("select type,tablename from {$tables['attribute']} where id = $id");
  51. $existingtype = $req[0];
  52. #print "Existing attribute: ".$_POST["name"][$id]." new type:".$_POST["type"][$id]." existing type: ".$req[0]."<br/>";
  53. if ($_POST["type"][$id] != $existingtype)
  54. switch ($existingtype) {
  55. case "textline":case "hidden":case "date":
  56. print "Converting ".$_POST["name"][$id]." from $existingtype to ".$_POST["type"][$id]."<br/>";
  57. switch ($_POST["type"][$id]) {
  58. case "radio":
  59. case "checkboxgroup":
  60. case "select":
  61. $lc_name = getNewAttributeTablename($req[1]);
  62. Sql_Query("create table $table_prefix"."listattr_$lc_name (id integer not null primary key auto_increment, name varchar(255) unique,listorder integer default 0)");
  63. $attreq = Sql_Query("select distinct value from {$tables['user_attribute']} where attributeid = $id");
  64. while ($row = Sql_Fetch_Row($attreq)) {
  65. $attindexreq = Sql_Query("select id from $table_prefix"."listattr_$lc_name where name = \"$row[0]\"");
  66. if (!Sql_Affected_Rows()) {
  67. Sql_Query("insert into $table_prefix"."listattr_$lc_name (name) values(\"$row[0]\")");
  68. $attid = Sql_Insert_Id();
  69. } else {
  70. $attindex = Sql_Fetch_Row($attindexreq);
  71. $attid = $attindex[0];
  72. }
  73. Sql_Query("update {$tables['user_attribute']} set value = $attid where attributeid = $id and value = \"$row[0]\"");
  74. }
  75. break;
  76. case "checkbox":
  77. # in case of checkbox we just need to set the value to "on"
  78. Sql_Query("update {$tables['user_attribute']} set value = \"off\" where attributeid = $id and (value = 0 or value = \"off\")");
  79. Sql_Query("update {$tables['user_attribute']} set value = \"on\" where attributeid = $id and (value = 1 or value = \"on\") ");
  80. case "date":
  81. $attreq = Sql_Query("select * from {$tables['user_attribute']} where attributeid = $id");
  82. while ($row = Sql_Fetch_Array($attreq)) {
  83. # if (strlen($row["value"] > 5)) {
  84. Sql_Query(sprintf('update %s set value = "%s" where attributeid = %d and userid = %d',$tables["user_attribute"],parseDate($row["value"]),$row["attributeid"],$row["userid"]));
  85. # }
  86. }
  87. break;
  88. }
  89. break;
  90. case "radio":case "select": case "checkbox":
  91. if ($_POST["type"][$id] != "date" && $_POST["type"][$id] != "hidden" && $_POST["type"][$id] != "textline") break;
  92. print "Converting ".$_POST["name"][$id]." from $existingtype to ".$_POST["type"][$id]."<br/>";
  93. # we are turning a radio,select or checkbox into a hidden or textline field
  94. $valuereq = Sql_Query("select id,name from $table_prefix"."listattr_$req[1]");
  95. while ($row = Sql_Fetch_Row($valuereq))
  96. Sql_Query("update {$tables['user_attribute']} set value = \"$row[1]\" where attributeid = $id and value=\"$row[0]\"");
  97. Sql_Query("drop table $table_prefix"."listattr_$req[1]");
  98. break;
  99. case "checkboxgroup":
  100. if ($_POST["type"][$id] == "hidden" || $_POST["type"][$id] == "textline") {
  101. print $GLOBALS['I18N']->get('converting')." ".$_POST["name"][$id]." ".$GLOBALS['I18N']->get('from')." $existingtype ".$GLOBALS['I18N']->get('to')." ".$_POST["type"][$id]."<br/>";
  102. # we are changing a checkbox group into a hidden or textline
  103. # take the first value!
  104. $valuereq = Sql_Query("select id,name from $table_prefix"."listattr_$req[1]");
  105. while ($row = Sql_Fetch_Row($valuereq))
  106. Sql_Query("update {$tables['user_attribute']} set value = \"$row[1]\" where attributeid = $id and value like \"$row[0]%\"");
  107. Sql_Query("drop table if exists $table_prefix"."listattr_$req[1]");
  108. } elseif ($_POST["type"][$id] == "radio" || $_POST["type"][$id] == "select") {
  109. $valuereq = Sql_Query("select userid,value from {$tables["user_attribute"]} where attributeid = $id");
  110. # take the first value!
  111. while ($row = Sql_Fetch_Row($valuereq)) {
  112. $values = split(",",$row[1]);
  113. Sql_Query("update {$tables['user_attribute']} set value = \"$values[0]\" where attributeid = $id and userid = \"$row[0]\"");
  114. }
  115. }
  116. break;
  117. }
  118. $query = sprintf('update %s set name = "%s" ,type = "%s" ,listorder = %d,default_value = "%s" ,required = %d where id = %d',
  119. $tables["attribute"],addslashes($_POST["name"][$id]),$_POST["type"][$id],$listorder[$id],$default[$id],$required[$id],$id);
  120. Sql_Query($query);
  121. }
  122. }
  123. print '<script language="Javascript" type="text/javascript"> finish();</script>';flush();
  124. }
  125. } elseif (isset($_POST["tagaction"]) && is_array($_POST["tag"])) {
  126. ksort($_POST["tag"]);
  127. if ($_POST["tagaction"] == $GLOBALS['I18N']->get('delete')) {
  128. while (list($k,$id) = each ($_POST["tag"])) {
  129. # check for dependencies
  130. if ($formtable_exists) {
  131. $req = Sql_Query("select * from formfield where attribute = $id");
  132. $candelete = !Sql_Affected_Rows();
  133. } else {
  134. $candelete = 1;
  135. }
  136. if ($candelete) {
  137. print $GLOBALS['I18N']->get('deleting')." $id<br/>";
  138. $row = Sql_Fetch_Row_Query("select tablename,type from {$tables['attribute']} where id = $id");
  139. Sql_Query("drop table if exists $table_prefix"."listattr_$row[0]");
  140. Sql_Query("delete from {$tables['attribute']} where id = $id");
  141. # delete all user attributes as well
  142. Sql_Query("delete from {$tables['user_attribute']} where attributeid = $id");
  143. } else {
  144. print Error($GLOBALS['I18N']->get('cannotdelete')."<br/>");
  145. while ($row = Sql_Fetch_Array($req)) {
  146. print PageLink2("editelements&id=".$row["form"]."&option=edit_elements&pi=formbuilder","form ".$row["form"]."")."<br/>\n";
  147. }
  148. }
  149. }
  150. } elseif ($_POST["tagaction"] == $GLOBALS['I18N']->get('merge')) {
  151. $first = array_shift($_POST["tag"]);
  152. $firstdata = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d',$tables["attribute"],$first));
  153. if (!sizeof($_POST["tag"])) {
  154. print Error($GLOBALS['I18N']->get('cannotmergeone'));
  155. } else {
  156. $cbg_initiated = 0;
  157. foreach ($_POST["tag"] as $attid) {
  158. print $GLOBALS['I18N']->get('merging')." $attid ".$GLOBALS['I18N']->get('into')." $first<br/>";
  159. $attdata = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d',$tables["attribute"],$attid));
  160. if ($attdata["type"] != $firstdata["type"]) {
  161. print Error($GLOBALS['I18N']->get('notsametype'));
  162. } else {
  163. # debugging: check values for every user. This is very memory demanding, so you'll need to
  164. # add loads of memory to actually use it.
  165. /*
  166. $before = array();
  167. $second = array();
  168. $after = array();
  169. $req = Sql_Query(sprintf('select * from %s where attributeid = %d',$tables["user_attribute"],$first));
  170. while ($row = Sql_Fetch_Array($req)) {
  171. $before[$row["userid"]] = $row["value"];
  172. }
  173. $req = Sql_Query(sprintf('select * from %s where attributeid = %d',$tables["user_attribute"],$attid));
  174. while ($row = Sql_Fetch_Array($req)) {
  175. $second[$row["userid"]] = $row["value"];
  176. }
  177. */
  178. $valuestable = sprintf('%slistattr_%s',$table_prefix,$firstdata["tablename"]);
  179. if ($firstdata["type"] == "checkbox" && !$cbg_initiated) {
  180. # checkboxes are merged into a checkbox group
  181. # set that up first
  182. Sql_query(sprintf('create table %s
  183. (id integer not null primary key auto_increment, name varchar(255) unique,
  184. listorder integer default 0)',$valuestable),1);
  185. Sql_query(sprintf('insert into %s (name) values("%s")',$valuestable,$firstdata["name"]));
  186. $val = Sql_Insert_Id();
  187. Sql_query(sprintf('update %s set value="%s" where attributeid = %d',
  188. $tables["user_attribute"],$val,$first));
  189. Sql_query(sprintf('update %s set type="checkboxgroup" where id = %d',
  190. $tables["attribute"],$first));
  191. $cbg_initiated = 1;
  192. }
  193. switch ($firstdata["type"]) {
  194. case "textline":
  195. case "hidden":
  196. case "textarea":
  197. case "date":
  198. Sql_query(sprintf('delete from %s where attributeid = %d and value = ""',$tables["user_attribute"],$first));
  199. # we can just keep the data and mark it as the first attribute
  200. Sql_query(sprintf('update ignore %s set attributeid = %d where attributeid = %d',
  201. $tables["user_attribute"],$first,$attid),1);
  202. # delete the ones that didn't copy across, because there was a value already
  203. Sql_query(sprintf('delete from %s where id = %d',
  204. $tables["attribute"],$attid));
  205. # mark forms to use the merged attribute
  206. if ($formtable_exists)
  207. Sql_Query(sprintf('update formfield set attribute = %d where attribute = %d',$first,$attid),1);
  208. break;
  209. case "radio":
  210. case "select":
  211. # merge user values
  212. Sql_Query(sprintf('delete from %s where attributeid = %d and value = ""',$tables["user_attribute"],$first));
  213. $req = Sql_Query(sprintf('select * from %s',
  214. $table_prefix."listattr_".$attdata["tablename"]));
  215. while ($val = Sql_Fetch_Array($req)) {
  216. # check if it already exists
  217. $exists = Sql_Fetch_row_Query(sprintf('select id from %s where name = "%s"',
  218. $valuestable,$val["name"]));
  219. if (!$exists[0]) {
  220. Sql_Query(sprintf('insert into %s (name) values("%s")',
  221. $valuestable,$val["name"]));
  222. $val_index = Sql_Insert_id();
  223. } else {
  224. $val_index = $exists[0];
  225. }
  226. Sql_Query(sprintf('update %s set value = %d where attributeid = %d',
  227. $tables["user_attribute"],$val_index,$attid));
  228. }
  229. Sql_Query(sprintf('update %s set attributeid = %d where attributeid = %d',
  230. $tables["user_attribute"],$first,$attid),1);
  231. Sql_Query(sprintf('drop table %s',$table_prefix."listattr_".$attdata["tablename"]),1);
  232. Sql_Query(sprintf('delete from %s where id = %d',
  233. $tables["attribute"],$attid));
  234. # mark forms to use the merged attribute
  235. if ($formtable_exists)
  236. Sql_Query(sprintf('update formfield set attribute = %d where attribute = %d',$first,$attid),1);
  237. break;
  238. case "checkbox":
  239. $exists = Sql_Fetch_row_Query(sprintf('select id from %s where name = "%s"',
  240. $valuestable,$attdata["name"]));
  241. if (!$exists[0]) {
  242. Sql_Query(sprintf('insert into %s (name) values("%s")',
  243. $valuestable,$attdata["name"]));
  244. $val_index = Sql_Insert_id();
  245. } else {
  246. $val_index = $exists[0];
  247. }
  248. Sql_Query(sprintf('update %s set value = concat(value,",","%s") where attributeid = %d and (value != 0 or value != "off") ',
  249. $tables["user_attribute"],$val_index,$first));
  250. Sql_Query(sprintf('delete from %s where id = %d',
  251. $tables["attribute"],$attid));
  252. # mark forms to use the merged attribute
  253. if ($formtable_exists)
  254. Sql_Query(sprintf('update formfield set attribute = %d where attribute = %d',$first,$attid),1);
  255. break;
  256. case "checkboxgroup":
  257. # hmm, this is a tricky one.
  258. print Error($GLOBALS['I18N']->get('sorrymergingcbgroupnotimplemented'));
  259. break;
  260. }
  261. /*
  262. $req = Sql_Query(sprintf('select * from %s where attributeid = %d',$tables["user_attribute"],$first));
  263. while ($row = Sql_Fetch_Array($req)) {
  264. $after[$row["userid"]] = $row["value"];
  265. }
  266. foreach ($before as $userid => $value) {
  267. printf("\n".'<br/>%d before -> %s and %s<br/>after ->%s',$userid,$value,$second[$userid],$after[$userid]);
  268. }
  269. */
  270. }
  271. }
  272. }
  273. }
  274. }
  275. Sql_Query("update {$tables['attribute']} set required = 0 where type = \"checkbox\" or type = \"checkboxgroup\" or type = \"hidden\"");
  276. ?>
  277. <script language="Javascript" type="text/javascript">
  278. var warned = 0;
  279. function warn() {
  280. if (!warned)
  281. alert("<?php echo $GLOBALS['I18N']->get('warninglongtime')?>");
  282. warned = 1;
  283. }
  284. </script>
  285. <?=formStart()?>
  286. <?
  287. print $GLOBALS['I18N']->get('loadfrom')." ".PageLink2("defaults",$GLOBALS['I18N']->get('defaults')).'<br />';
  288. $res = Sql_Query("select * from {$tables['attribute']} order by listorder");
  289. if (Sql_Affected_Rows())
  290. print $GLOBALS['I18N']->get('existing').":<p>";
  291. else {
  292. print $GLOBALS['I18N']->get('noattributesdefined').'<br />';
  293. }
  294. $c= 0;
  295. while ($row = Sql_Fetch_array($res)) {
  296. $c++;
  297. print '<table border=1><tr><td colspan=2>'.
  298. $GLOBALS['I18N']->get('attribute').':'. $row["id"];
  299. if ($formtable_exists) {
  300. sql_query("select * from formfield where attribute = ".$row["id"]);
  301. print " (".$GLOBALS['I18N']->get('usedin').' '.Sql_affected_rows().' '.$GLOBALS['I18N']->get('forms');
  302. }
  303. print '</td><td colspan=2>'.$GLOBALS['I18N']->get('tag').' <input type="checkbox" name="tag['.$c.']" value="'.$row["id"].'"></td></tr>';
  304. print '<tr><td colspan=2>'.$GLOBALS['I18N']->get('name').': </td><td colspan=2><input type=text name="name['.$row["id"].']" value="'.htmlspecialchars(stripslashes($row["name"])).'" size=40></td></tr>';
  305. print '<tr><td colspan=2>'.$GLOBALS['I18N']->get('type').': </td><td colspan=2><!--input type=hidden name="type['.$row["id"].']" value="'.$row["type"].'">'.$row["type"].'-->';
  306. print '<select name="type['.$row["id"].']" onChange="warn();">';
  307. foreach($types as $key => $val) {
  308. printf('<option value="%s" %s>%s</option>',$val,$val == $row["type"] ? "selected": "",$GLOBALS['I18N']->get($val));
  309. }
  310. print '
  311. </select>';
  312. print '</td></tr>';
  313. print '<tr><td colspan=2>'.$GLOBALS['I18N']->get('defaultvalue').': </td><td colspan=2><input type=text name="default['.$row["id"].']" value="'.htmlspecialchars(stripslashes($row["default_value"])).'" size=40></td></tr>';
  314. print '<tr><td>'.$GLOBALS['I18N']->get('orderoflisting').': </td><td><input type=text name="listorder['.$row["id"].']" value="'.$row["listorder"].'" size=5></td>';
  315. print '<td>'.$GLOBALS['I18N']->get('isrequired').': </td><td><input type=checkbox name="required['.$row["id"].']" value="1" ';
  316. print $row["required"] ? "checked": "";
  317. print '></td></tr>';
  318. print '</table><hr>';
  319. }
  320. printf('<input type=submit name="action" value="%s">',$GLOBALS['I18N']->get('savechanges'));
  321. print '<br/><br/>
  322. <script language="Javascript" src="js/jslib.js" type="text/javascript"></script>';
  323. if ($c) {
  324. printf('<i>%s: </i><br/>',$GLOBALS['I18N']->get('withtagged'));
  325. printf('<input type=submit name="tagaction" value="%s">&nbsp;
  326. <input type=submit name="tagaction" value="%s"> &nbsp;&nbsp;%s<br/>
  327. <p><hr/></p>',$GLOBALS['I18N']->get('delete'),$GLOBALS['I18N']->get('merge'),Help("mergeattributes"));
  328. }
  329. print '
  330. <a name="new"></a>
  331. <h3>'.$GLOBALS['I18N']->get('addnew').':</h3>
  332. <table border=1>
  333. <tr><td colspan=2>'.$GLOBALS['I18N']->get('name').': </td><td colspan=2><input type=text name="name[0]" value="" size=40></td></tr>
  334. <tr><td colspan=2>'.$GLOBALS['I18N']->get('type').': </td><td colspan=2><select name="type[0]">';
  335. foreach($types as $key => $val) {
  336. printf('<option value="%s" %s>%s</option>',$val,"",$GLOBALS['I18N']->get($val));
  337. }
  338. print'
  339. </select></td></tr>
  340. <tr><td colspan=2>'.$GLOBALS['I18N']->get('defaultvalue').': </td><td colspan=2><input type=text name="default[0]" value="" size=40></td></tr>
  341. <tr><td>'.$GLOBALS['I18N']->get('orderoflisting').': </td><td><input type=text name="listorder[0]" value="" size=5></td>
  342. <td>'.$GLOBALS['I18N']->get('isrequired').': </td><td><input type=checkbox name="required[0]" value="1" checked></td></tr>
  343. </table><hr>
  344. <input type=submit name="action" value="'.$GLOBALS['I18N']->get('savechanges').'">
  345. </form>
  346. ';