 __________________________________________________________________________ 
|    _    ____                _                   __ _      _     _        |
|   | |__|___ \ ___ _   _ ___| |_ ___  _ __ ___  / _(_) ___| | __| |___    |
|   | '_ \ __) / __| | | / __| __/ _ \| '_ ` _ \| |_| |/ _ \ |/ _` / __|   |
|   | |_) / __/ (__| |_| \__ \ || (_) | | | | | |  _| |  __/ | (_| \__ \   |
|   |_.__/_____\___|\__,_|___/\__\___/|_| |_| |_|_| |_|\___|_|\__,_|___/   |
|                                                                          |
|    By: Chuck Harmston  -  v2 Installation Instructions & Documentation   |
|__________________________________________________________________________|
|                                                                          |
|  About b2customfields                                                    |
|  b2customfields is a plugin/hack written for the popular b2 blogging     |
|  software, which is available at cafelog.com.  b2customfields enables    |
|  the webmaster to add custom fields to each post.  Why would somebody    |
|  want to do that?  There's a number of reasons.  Something as basic as   |
|  adding a field called 'music', in which you can put the song that you   |
|  were listening to at the time of your post, to something as complex as  |
|  a photoblog can be done with b2customfields.  b2customfields takes      |
|  full advantage of b2's templating system, and advances on it, giving    |
|  it extended functionality.                                              |
|__________________________________________________________________________|
|                                                                          |
|  Installing b2customfields                                               |
|  b2customfields has a relatively easy installation process.  You will    |
|  need an FTP program, text editor, and web-based mySQL administration    |
|  system, such as phpMyAdmin.  A note about the installation              |
|  instructions: any code that you are required to find or edit will NOT   |
|  be enclosed in a box, as all of the instructions and other text are.    |
|__________________________________________________________________________|
|                                                                          |
|  Step 1: Run the following SQL query on your database somehow.  In the   |
|  event that you have no method of accessing the database, upload and     |
|  run the file b2cf.install.php from your server, in your root b2         |
|  directory.                                                              |
|__________________________________________________________________________|

CREATE TABLE b2customfields (
  field_id int(11) NOT NULL auto_increment,
  field_name text NOT NULL,
  field_type text NOT NULL,
  field_options text NOT NULL,
  PRIMARY KEY  (field_id)
)
 __________________________________________________________________________
|                                                                          |
|  Step 2: Open up b2edit.php in your text editor, and find the following  |
|  code in it:                                                             |
|__________________________________________________________________________|

$query = "INSERT INTO $tableposts (ID, post_author, post_date, post_content, post_title, post_category) VALUES ('0','$user_ID','$now','$content','".$post_title."','".$post_category."')";
 __________________________________________________________________________
|                                                                          |
|  Step 2 (cont): Replace the found text with the following code:          |
|__________________________________________________________________________|

/* Edited for b2customfields */
$sql1 = "SELECT * FROM b2customfields";
$result1 = mysql_query($sql1);
$query = "INSERT INTO $tableposts (ID, post_author, post_date, post_content, post_title, post_category";
$queryadd = "";
while($row1 = mysql_fetch_array($result1)){
	$query .= ",".$row1['field_name'];
	if($row1['field_type']=="checkbox"&&empty($_POST[$row1['field_name']])){
		$queryadd .= ",'0'";
	}else{
		$queryadd .= ",'".$_POST[$row1['field_name']]."'";
	}
}
$query .= ") VALUES ('0','$user_ID','$now','$content','".$post_title."','".$post_category."'".$queryadd.")";
/* End edited for b2customfields */
 __________________________________________________________________________
|                                                                          |
|  Step 3: Remaning in b2edit.php, find the following code:                |
|__________________________________________________________________________|

$query = "UPDATE $tableposts SET post_content=\"$content\", post_title=\"$post_title\", post_category=\"$post_category\"".$datemodif." WHERE ID=$post_ID";
 __________________________________________________________________________
|                                                                          |
|  Step 3 (cont): Replace the found text with the following code:          |
|__________________________________________________________________________|

/* Edited for b2customfields */
$query = "UPDATE $tableposts SET post_content=\"$content\", post_title=\"$post_title\", post_category=\"$post_category\"".$datemodif;
$sql = "SELECT * FROM b2customfields";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
	if($row['field_type']=="checkbox"&&empty($_POST[$row['field_name']])){
		$query .= ", ".$row['field_name']."=\"0\"";
	}else{
		$query .= ", ".$row['field_name']."=\"".$_POST[$row['field_name']]."\"";
	}
}
$query .= " WHERE ID=$post_ID";
/* End edited for b2customfields */
 __________________________________________________________________________
|                                                                          |
|  Step 4: Now open up b2-include/b2edit.form.php in your text editor,     |
|  and find the following code:                                            |
|__________________________________________________________________________|

<textarea rows="9" cols="40" style="width:100%" name="content" tabindex="4" wrap="virtual" id="content"><?php echo $content ?></textarea><br />
 __________________________________________________________________________
|                                                                          |
|  Step 4 (cont): Immediately following that line of code, add the         |
|  following:                                                              |
|__________________________________________________________________________|

<?
/* Edited for b2customfields */
$sql = "SELECT * FROM b2customfields";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
	$subsql = "SELECT ".$row['field_name']." FROM $tableposts";
	if($_GET['action'] == 'edit'){
			$subsql .=  " where ID = ".$postdata["ID"];
	}
	$subsql .= " LIMIT 1";
	$subresult = mysql_query($subsql);
	while ($subrow = mysql_fetch_array($subresult)){
		switch($row['field_type']){
			default :
				if($_GET['action'] == 'edit'){
					echo "<b>".$row["field_name"]." :</b><br /><input size=\"20\" style=\"width: 170px;\" name=\"".$row["field_name"]."\" value=\"".$subrow[0]."\"><br />";
				}else{
					echo "<b>".$row["field_name"]." :</b><br /><input size=\"20\" style=\"width: 170px;\" name=\"".$row["field_name"]."\"><br />";
				}
			break;
			case "textarea" :
				if($_GET['action'] == 'edit'){
					echo "<b>".$row["field_name"]." :</b><br /><textarea rows=\"9\" cols=\"40\" style=\"width:100%\" name=\"".$row["field_name"]."\" wrap=\"virtual\">".$subrow[0]."</textarea><br />";
				}else{
					echo "<b>".$row["field_name"]." :</b><br /><textarea rows=\"9\" cols=\"40\" style=\"width:100%\" name=\"".$row["field_name"]."\" wrap=\"virtual\"></textarea><br />";
				}
			break;
			case "checkbox" :
				if($_GET['action'] == 'edit'){
					switch($subrow[0]){
						case "1" :
							$checked = " checked";
						break;
						default :
							$checked = "";
						break;
					}
					echo "<input type=\"checkbox\" class=\"checkbox\" name=".$row["field_name"]." value=\"1\"".$checked." /> ".$row["field_name"]."<br />";
				}else{
					echo "<input type=\"checkbox\" class=\"checkbox\" name=".$row["field_name"]." value=\"1\" /> ".$row["field_name"]."<br />";
				}
			break;
			case "dropdown" :
				if($_GET['action'] == 'edit'){
					echo "<b>".$row["field_name"]." :</b><br /><select style=\"width:170px;\" name=\"".$row["field_name"]."\">";
					$options = explode(';;',$row["field_options"]);
					foreach ($options as $option){
						switch($subrow[0]){
							case $option :
								$selected = " selected";
							break;
							default :
								$selected = "";
							break;
						}
						echo "<option value=\"".$option."\"".$selected.">".$option."</option>";							
					}
					echo "</select><br />";
				}else{
					echo "<b>".$row["field_name"]." :</b><br /><select style=\"width:170px;\" name=\"".$row["field_name"]."\">";
					$options = explode(';;',$row["field_options"]);
					foreach ($options as $option){
						echo "<option value=\"".$option."\">".$option."</option>";
					}
					echo "</select><br />";
				}
			break;
		}
	}
}
/* Edited for b2customfields */
?>
 __________________________________________________________________________
|                                                                          |
|  Step 4 (cont): Ensure that you get all of the previous code added.  It  |
|  is a crucial step.                                                      |
|                                                                          |
|  Step 5: Open up the file b2-include/b2functions.php.  At the end of     |
|  the file, add the following two functions:                              |
|__________________________________________________________________________|

function createfield_form(){
	?>
	<script>
	function checkInput(element) {
		if (element.value == "dropdown") {
			document.form.field_options.disabled=false;
		} else {
			document.form.field_options.disabled=true;
		}
	}
	</script>


	<table cellspacing="0" cellpadding="1" width="85%" border="0" bgcolor="#cccccc" align="center">
			<td align="left">
				<table cellspacing="0" cellpadding="15" width="100%" border="0"bgcolor="#ffffff" align="center">
				  <td align="left"> <strong>Create Custom Field</strong><br />
					<br />
					<form name="form" method="post" action="b2fields.php?action=create&id=1">
					  Field Name: 
					  <input type="text" name="field_name"><br />
					  Field Type: 
					  <select name="field_type" onchange="checkInput(this)">
						<option value="input">Text box (small)</option>
						<option value="textarea">Text box (large)</option>
						<option value="dropdown">Dropdown/combo box</option>
						<option value="checkbox">Checkbox</option>
					  </select></br>
					  Field Options: 
					  <input type="text" name="field_options" value="Separate values with ;;" disabled><br />
					  <input type="submit" name="Submit" value="Create Field"></form> <br /><br /><br />
					<b>Documentation</b><br />
					<i>Field Name:</i> The name by which the custom field will be called.  Please avoid putting spaces in the field name.  Instead use underscores (_), or no spaces at all.<br />
					<i>Field Type:</i> The type of form field that will be associated with it.  'Text box (small)' produces an &lt;input type="text">, 'Text box (large)' produces a &lt;textarea>, 'Dropdown/combo box' produces a &lt;select> box, and 'Checkbox' produces an &lt;input type="checkbox">  If you do not know what these are, some expirimentation will help.<br />
					<i>Field Options:</i> This field will be disabled unless the field type is set to 'Dropdown/combo box'.  Here you write down the names of the possible options that will populate the dropdown box, separated by 2 semicolons (;;).  ie "Option 1;;Option 2;;Option 3;;Option 4"<br />
					</td>
				</table>
			</td>
		</table>
		<?
}

function field_list(){
	?>
	<table cellspacing="0" cellpadding="1" width="85%" border="0" bgcolor="#cccccc" align="center">
	  <td align="left"> <table cellspacing="0" cellpadding="15" width="100%" border="0"bgcolor="#ffffff" align="center">
		  <td align="left"> <p><b>Custom Fields</b> 
			<table cellpadding="5" cellspacing="0">
			  <tr> 
				<td class="tabletoprow">ID</td>
				<td class="tabletoprow">Category Name</td>
				<td class="tabletoprow">Type</td>
				<td class="tabletoprow">&nbsp;</td>
				<td class="tabletoprow">&nbsp;</td>
				<td class="tabletoprow">&nbsp;</td>
			  </tr>
			  <?
			  $sql = "SELECT * FROM b2customfields";
			  $result = mysql_query($sql);
			  while($row = mysql_fetch_array($result)){
			  ?>
			  <tr> 
				<td style="background-image: url('b2-img/b2button.gif');"><?php echo $row['field_id'] ?></td>
				<td style="background-image: url('b2-img/b2button.gif');"><?php echo $row['field_name'] ?></td>
				<td style="background-image: url('b2-img/b2button.gif');"><?php echo $row['field_type'] ?></td>
				<td style="background-image: url('b2-img/b2button.gif');"><a href="b2fields.php?action=gencode&id=<?php echo $row['field_id'] ?>&name=<?php echo $row['field_name'] ?>">Generate Code</a></td>
				<td style="background-image: url('b2-img/b2button.gif');"><a href="b2fields.php?action=edit&id=<?php echo $row['field_id'] ?>">Edit</a></td>
				<td style="background-image: url('b2-img/b2button.gif');"><a href="b2fields.php?action=delete&id=<?php echo $row['field_id'] ?>">Delete</a></td>
			  </tr>
			  <?
			  }
			  ?>
			</table>
			<p></p></td>
		</table></td>
	</table>
	<br />
	<?
}
 __________________________________________________________________________
|                                                                          |
|  Step 5: Open up the file b2-include/b2template.functions.php.  At the   |
|  end of the file, add the following function:                            |
|__________________________________________________________________________|

function getcustomfield($name){
	global $tableposts,$id;
	$sql = "SELECT ".$name." FROM ".$tableposts." where id = ".$id." LIMIT 1";
	$result = mysql_query($sql);
	while ($row = mysql_fetch_array($result)){
		$output = $row[$name];
	}
	$output = convert_bbcode(convert_smilies(stripslashes($output)));
	echo $output;
}
 __________________________________________________________________________
|                                                                          |
|  Step 6: Open up the file b2-include/b2menutop.txt.  Before the '***',   |
|  add the following line.  Adjust the '10' to reflect the minimum level   |
|  the user needs to access the section in the administrative panel:       |
|  between 0 and 10.  I reccomend leaving it at 10, unless you have a co-  |
|  administrator.                                                          |
|__________________________________________________________________________|

10	b2fields.php	Custom Fields
 __________________________________________________________________________
|                                                                          |
|  Step 7: Save and upload all of the changed files, and also upload       |
|  the included b2fields.php to your root b2 directory, and you are done!  |
|  Log into your admin panel and click on the 'Custom Fields' link along   |
|  the navigation in the top, and begin using b2customfields!              |
|__________________________________________________________________________|
|                                                                          |
|  Using b2customfields                                                    |
|  When you create a custom field, an extra form field is automatically    |
|  added to the 'Post / Edit' page, enabling you to begin adding entries   |
|  immediately.  However, the custom field will NOT automatically be       |
|  added to your website.  You will need to edit the template with the     |
|  template tag, which can be obtained using the code generator.  Usage    |
|  should become apparent after fooling around with it a little bit.       |
|__________________________________________________________________________|
|                                                                          |
|  Advanced usage                                                          |
|  This space will soon contain ideas and examples of usage of             |
|  b2customfields, but first I would like to make sure that the code       |
|  works well, without any bugs.  Support requests go to the thread at     |
|  the b2 forums please.                                                   |
|                                                                          |
|                                                                          |
|                                        Documentation updated 06/08/2003  |
|__________________________________________________________________________|