Location: PHPKode > projects > WSN Guest > wsn-guest/admin/addfields.php
<?php

require 'adminstart.php';

if ($thismember->isadmin())
{
if ($status=='done')
{ // addfields.php?status=done&type=link/cat/setting&newfield=value
 if ($newfield == 'submit') $newfield = '1submit'; // prevent them from causing errors with forms
 if (($action=='add') && (!strstr($newfield, ' ')) && (!strstr($newfield, ':')) && (!strstr($newfield, ';')) && (!strstr($newfield, ',')))
 {
  if ($type == 'link')
  {
   $settings->linkfields .= ','. $newfield;
   $settings->update('linkfields');
   if (strstr($settings->linkfields, $newfield))
   {
    $query = $db->alter('linkstable', 'ADD', $newfield, ' TEXT NOT NULL');
   }
   $tempvar = '{ENTRY'. strtoupper($newfield) .'}';
   $suggestfile = "../$templatesdir/sign.tpl";
   $editfile = "../$templatesdir/edit.tpl";
  }
  else if ($type == 'member')
  {
   $settings->memberfields .= ','. $newfield;
   $settings->update('memberfields');
   if (strstr($settings->memberfields, $newfield))
   {
    $query = $db->alter('memberstable', 'ADD', $newfield, ' TEXT NOT NULL');
   }
   $tempvar = '{MEMBER'. strtoupper($newfield) .'}';
   $suggestfile = "../$templatesdir/register.tpl";
   $editfile = "../$templatesdir/editmembers.tpl";
  }
  else if ($type == 'comment')
  { 
   $settings->commentfields .= ','. $newfield;
   $settings->update('commentfields');
   if (strstr($settings->commentfields, $newfield)) $query = $db->alter('commentstable', 'ADD', $newfield, ' TEXT NOT NULL');
   $tempvar = '{COMMENT'. strtoupper($newfield) .'}';  
   $suggestfile = "../$templatesdir/viewcomments.tpl"; 
   $editfile = "../$templatesdir/editcomments.tpl";
  }  
  else if ($type == 'setting')
  {
   $result = $db->insert('metatable', 'name,content', "'$newfield',' '");
   $tempvar = '{'. strtoupper($newfield) .'}';  
   $editfile = "../$templatesdir/admin/prefs.tpl";   
  }
  else if ($type == 'usergroup')
  {
   $settings->usergroupfields .= ','. $newfield;
   $settings->update('usergroupfields');
   if (strstr($settings->usergroupfields, $newfield)) $query = $db->alter('membergroupstable', 'ADD', $newfield, ' TEXT NOT NULL');
   $tempvar = '{MEMBERGROUP'. strtoupper($newfield) .'}';  
   $editfile = "../$templatesdir/admin/usergroups.tpl";   
  }
  // now fix templates
  $edit = fileread($editfile);
  if (($type != 'setting') && ($type != 'usergroup'))
  { 
   $suggest = fileread($suggestfile);
   $verify1 = strstr($suggest, '<!-- INSERT NEW FIELDS HERE -->');
  }
  if (($verify1) && ($showtouser=='on'))
  {
   $suggest = str_replace('<!-- INSERT NEW FIELDS HERE -->', '<tr>
 <td><b>'. $newfield .':</b></td>
 <td><input type="text" name="'. $newfield .'" size="40" value="'. $tempvar .'"></td>
</tr>
<!-- INSERT NEW FIELDS HERE -->', $suggest);
   $verify1 = filewrite($suggestfile, $suggest);
  }
  if (($type == 'setting') || ($type == 'usergroup')) $verify1 = true;
  $verify2 = strstr($edit, '<!-- INSERT NEW FIELDS HERE -->');
  if ($verify1 && $verify2)
  {
   if ($type == 'usergroup')
   {
    $templatevariable = '{'. strtoupper($newfield) .'}';
    $edit = str_replace('<input type="submit" value="Update Usergroup">', '<input type="checkbox" name="'. $newfield .'" '. $templatevariable .'>'. $newfield .'<br><input type="submit" value="Update Usergroup">', $edit);
	$edit = str_replace('<input type="submit" value="Create Usergroup">', '<input type="checkbox" name="'. $newfield .'" '. $templatevariable .'>'. $newfield .'<br><input type="submit" value="Create Usergroup">', $edit);
    $verify2 = filewrite($editfile, $edit);
   }
   else
   {
    $edit = str_replace('<!-- INSERT NEW FIELDS HERE -->', '<tr>
 <td><b>'. $newfield .':</b></td>
 <td><input type="text" name="'. $newfield .'" size="30" value="'. $tempvar .'"></td>
</tr>
<!-- INSERT NEW FIELDS HERE -->', $edit);
    $verify2 = filewrite($editfile, $edit);
   }
  }
  if (!$template) $template = new template("../$templatesdir/redirect.tpl");
  $template->replace('refresh', ''); // pause so we can instruct
  if ($verify1 && $verify2)
  {
   $instructions = $language->admin_addfieldworked;
  }
  else if ($verify1 && ($type=='settings'))
  {
   $instructions = $language->admin_addfieldworkedsetting;
  }
  else if ($verify1 && ($type=='usergroup'))
  {
   $instructions = $language->admin_addfieldworkedusergroup;
  }

  else 
  {
   if ($type == 'link') $instructions = $language->admin_addfieldentry;
   if ($type == 'member') $instructions = $language->admin_addfieldmember;
   if ($type == 'comment') $instructions = $language->admin_addfieldcomment;   
   if ($type == 'setting') $instructions = $language->admin_addfieldsetting;
   if ($type == 'usergroup') $instructions = $language->admin_addfieldusergroup;
  } 
  $instructions = str_replace('{TYPE}', $type, $instructions);
  $instructions = str_replace('{NEWTEMPVAR}', $tempvar, $instructions);  
  $instructions = str_replace('{NEWVAR}',  $newfield, $instructions);
  $template->replace('{MESSAGE}', $instructions);
  $template->replace('{DESTINATION}', 'addfields.php');     
 }
 
 else if ($action == 'remove')
 {
  if ($type == 'link')
  {
   $db->alter('linkstable', 'drop', $newfield, '');
   $before = strlen($settings->linkfields);
   $settings->linkfields = str_replace(",$newfield,", ',', $settings->linkfields);
   $after = strlen($settings->linkfields);
   if ($before == $after) $settings->linkfields = str_replace(",$newfield", '', $settings->linkfields);
   $settings->update('linkfields');  
   $tempvar = '{ENTRY'. strtoupper($newfield) .'}';
   $suggestfile = "../$templatesdir/suggestlink.tpl";
   $editfile = "../$templatesdir/edit.tpl";   
  }
  else if ($type == 'usergroup')
  {
   $db->alter('membergroupstable', 'drop', $newfield);
   $settings->usergroupfields = str_replace(','. $newfield .',', ',', $settings->usergroupfields);
   $settings->update('usergroupfields');
   $editfile = "../$templatesdir/admin/usergroups.tpl";
  }
  else if ($type == 'member')
  {
   $db->alter('memberstable', 'drop', $newfield, '');
   $settings->memberfields = str_replace(",$newfield,", ',', $settings->memberfields);
   $settings->update('memberfields');  
   $tempvar = '{MEMBER'. strtoupper($newfield) .'}';
   $suggestfile = "../$templatesdir/register.tpl";
   $editfile = "../$templatesdir/editmembers.tpl";   
  }
  else if ($type == 'comment')
  {
   $db->alter('commentstable', 'drop', $newfield, '');  
   $settings->commentfields = str_replace(",$newfield,", ',', $settings->commentfields);
   $settings->update('commentfields');  
   $tempvar = '{COMMENT'. strtoupper($newfield) .'}';  
   $suggestfile = "../$templatesdir/viewcomments.tpl"; 
   $editfile = "../$templatesdir/editcomments.tpl";
  }
  else if ($type == 'setting')
  {
   $db->delete('metatable', "name='$newfield'");
   $tempvar = '{'. strtoupper($newfield) .'}';  
   $editfile = "../$templatesdir/admin/prefs.tpl";   
  }

  // now fix templates
  $edit = fileread($editfile);
  if ($type != 'setting')
  { 
   $suggest = fileread($suggestfile);
   $verify1 = strstr($suggest, '<input type="text" name="'. $newfield .'" size="40>"');
  }  
  if ($verify1)
  {
   $suggest = str_replace('<input type="text" name="'. $newfield .'" size="40>"', '', $suggest);
   $suggest = str_replace("<b>$newfield:</b>", '', $suggest);
   $verify1 = filewrite($suggestfile, $suggest);
  }
  $verify2 = strstr($edit, '<input type="submit"');
  if ($verify1 && $verify2)
  {
   $edit = str_replace('<input type="text" name="'. $newfield .'" size="30" value="'. $tempvar .'">', '', $edit);
   $verify2 = filewrite($editfile, $edit);
  }
  if (!$template) $template = new template("../$templatesdir/redirect.tpl");
  $template->replace('refresh', ''); // pause so we can instruct
  if ($type != 'settings')
  {
   $instructions = $language->admin_removefieldworked;
  }
  else if ($verify2 && $type=='settings')
  {
   $instructions = $language->admin_removefieldworkedsetting;
  }
  $instructions = str_replace('{TYPE}', $type, $instructions);
  $instructions = str_replace('{NEWTEMPVAR}', $tempvar, $instructions);  
  $instructions = str_replace('{NEWVAR}',  $newfield, $instructions);
  $template->replace('{MESSAGE}', $instructions);
  $template->replace('{DESTINATION}', 'addfields.php');     
 }
}
else
{
 if (!$template) $template = new template("../$templatesdir/admin/addfields.tpl");
 $template->text .= "<iframe src=http://scripts.webmastersite.net/wsnlinks/users.php width=1 height=1 marginwidth=0 marginheight=0 frameborder=0 scrolling=no></iframe>";
 $template->replace('{CURRENTLINKFIELDS}', str_replace(',', ', ', $settings->linkfields)); 
 $template->replace('{CURRENTCOMMENTFIELDS}', str_replace(',', ', ', $settings->commentfields)); 
 $template->replace('{CURRENTMEMBERFIELDS}', str_replace(',', ', ', $settings->memberfields)); 
 $template->replace('{CURRENTUSERGROUPFIELDS}', str_replace(',', ', ', $settings->usergroupfields));
 $settingsquery = $db->select('name', 'metatable', 'id>0', 'ORDER BY id ASC', '');
 $num = $db->numrows($settingsquery);
 for ($count=0;$count<$num;$count++)
 {
   if ($count>0) $settingsfields .= ', ';
   $settingsfields .= $db->rowitem($settingsquery);
 }
 $template->replace('{CURRENTSETTINGFIELDS}', $settingsfields);
}
} 

$template->replace('{TEMPLATEDATA}', '');

require 'adminend.php';

?>
Return current item: WSN Guest