Skip to content

Instantly share code, notes, and snippets.

@andrunix
Created May 9, 2014 14:27
Show Gist options
  • Save andrunix/c5952d1ffca02a9e37ec to your computer and use it in GitHub Desktop.
Save andrunix/c5952d1ffca02a9e37ec to your computer and use it in GitHub Desktop.
<?php
# ---------------------------------------------------------------------------
# listitem.php - List an item for sale
# ---------------------------------------------------------------------------
require_once('suresale.php');
require_once('navfn.php');
require_once "dbfn.php";
require_once "lgnfrm.php";
require_once('listitems.php');
require_once('itmupd.php');
$title = "List a School Uniform for Sale";
login_check_redirect();
$flds = array('title'=> array('val'=>'','req'=>true,'min'=>10, 'max'=>100, 'name'=>'Title', 'm'=>''),
'long_desc'=> array('val'=>'','req'=>true,'min'=>5,'max'=>500,'name'=>'Description', 'm'=>''),
'quantity'=> array('val'=>'','req'=>true,'min'=>1, 'max'=>3, 'name'=>'Quantity', 'm'=>''),
'price'=> array('val'=>'','req'=>true,'min'=>1, 'max'=>6, 'name'=>'Price', 'm'=>''),
'size'=> array('val'=>'','req'=>true,'min'=>1, 'max'=>20, 'name'=>'Size', 'm'=>''),
'school_id'=> array('val'=>'','req'=>true,'min'=>0, 'max'=>10, 'name'=>'', 'm'=>''),
);
if(isset($_POST['submit'])) {
if(isset($_SESSION['item_id'])) {
update_item();
}
else {
# if add mode
$bOK = true;
foreach($_POST as $key=>$val) {
if(array_key_exists($key, $flds)) {
$flds[$key]['val'] = $val;
if( ! ( (strlen($val) >= $flds[$key]['min']) &&
(strlen($val) <= $flds[$key]['max']) ) ) {
$flds[$key]['m'] = $flds[$key]['name'].' must be between '
.$flds[$key]['min'].' and '
.$flds[$key]['max'].' characters';
$bOK = false;
}
}
}
# if everything checked out ok, proceed with saving the record
if($bOK) {
save_item();
header("Location: index.php");
exit;
}
}
}
show_header();
open_body();
$cat = "";
if(isset($_REQUEST['c'])) {
$cat = $_REQUEST['c'];
}
$condition="";
# if we have the session var 'item_id' set, we are in
# edit mode.
$mode = 'Add';
$expires=0;
$image='';
$sold=0;
$deleted=0;
$viewcount=0;
if(isset($_SESSION['item_id'])) {
$mode = 'Edit';
# get the details for this item
$sql = "SELECT a.id, a.school_id, a.user_id, "
."a.cat_id, a.title, a.long_desc, a.quantity,"
."a.price, a.size, a.expires, a.image, a.sold, a.deleted, a.view_count, a.condition_id"
." FROM item a WHERE id=".$_SESSION['item_id'];
$item = $db->get_row($sql);
if($item != null) {
$flds['school_id']['val']=$item->school_id;
$flds['cat_id']['val']=$item->cat_id;
$flds['title']['val']=$item->title;
$flds['long_desc']['val']=$item->long_desc;
$flds['quantity']['val']=$item->quantity;
$flds['price']['val']=$item->price;
$flds['size']['val']=$item->size;
$expires=$item->expires;
$image=$item->image;
$sold=$item->sold;
$deleted=$item->deleted;
$viewcount=$item->viewcount;
$cat = $item->cat_id;
$condition = $item->condition_id;
}
}
?>
<script type="text/javascript">
var xhr;
function processSchoolList() {
if(xhr.readyState != 4) return;
toggleLayer('resultsForm');
var xmlt = xhr.responseText;
var data = eval('(' + xhr.responseText + ')');
var lst = document.getElementById("searchList");
for(i = 0; i < data.schools.length; i++) {
lst.options[i] = new Option(data.schools[i].name, data.schools[i].id, false, false);
}
}
function lookupSchool() {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xhr = false;
}
}
if(!xhr && typeof XMLHttpRequest != 'undefined') {
xhr = new XMLHttpRequest();
}
xhr.open("GET", encodeURI("schllu.php?n=" + document.getElementById("s_name").value));
xhr.onreadystatechange=processSchoolList;
xhr.send(null);
}
function schoolSelected() {
var sel = document.getElementById('searchList');
var theVal = sel.options[sel.selectedIndex].text;
document.getElementById('school').value = theVal;
document.getElementById('school_id').value = sel.options[sel.selectedIndex].value;
toggleLayer('schoolForm');
}
function searchForm() {
toggleLayer('schoolForm');
}
function cancelIt() {
document.location.href = "index.php";
}
// client-side validation. server-side will be done as well.
function formComplete() {
var b;
bqty = required('quantity')&&isnumeric('quantity');
bprc = required('price');
bsz = required('size');
bttl = required('title');
return bqty&&bprc&&bsz&&bttl;
}
function required(el) {
var e = document.getElementById(el);
var v = e.value;
if(v.trim().length == 0) {
document.getElementById((el+'msg')).innerHTML=e.title+" is a required field.";
return false;
}
else
document.getElementById((el+'msg')).innerHTML="";
return true;
}
function isnumeric(el) {
var e = document.getElementById(el);
var v = e.value;
if(v*1==NaN) {
document.getElementById((el+'msg')).innerHTML=e.title+" must be a number.";
return false;
}
else
document.getElementById((el+'msg')).innerHTML="";
return true;
}
function setSchool() {
var d = document.getElementById("school_id_list").selectedIndex;;
document.getElementById("school_id").value = document.getElementById("school_id_list")[d].value;
}
</script>
<div class="bigBox">
<div class="roundedInset">
<?php
if($mode == "Add") {
?>
<div class="boxHeader">LIST A NEW ITEM FOR SALE</div>
<?php
}
else {
?>
<div class="boxHeader">EDIT ITEM</div>
<?php
}
?>
<br/>
<form enctype="multipart/form-data" name="frm"
action="<?php echo $_SERVER['PHP_SELF'] ?>"
method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="5368709120" />
<?php
$s = '';
if($mode == "Add") {
if(isset($_REQUEST['s'])) {
$s = $_REQUEST['s'];
}
else if(isset($flds['school_id']['val'])) {
$s = $flds['school_id']['val'];
}
}
else {
$s = $flds['school_id']['val'];
}
?>
<table>
<tr>
<td valign="top">School:</td>
<td>
<?php
$schools = get_schools_for_user($_SESSION['user_id']);
print "<select id=\"school_id_list\" onChange=\"setSchool();\">";
$bFirst = 1;
foreach($schools as $sch) {
if($mode == "Add" && $bFirst == 1) {
$s = $sch->school_id;
}
$bFirst = 0;
print "<option value=\"".$sch->school_id . "\"";
if ($sch->school_id==$s)
print " SELECTED";
print ">" . $sch->name . "</option>";
}
print "</select>";
?>
<input id="school_id" type="hidden" name="school_id" value="<?php echo $s ?>"/>
<img src="images/question.png" title="Select a school for your item. Don't see the school you want in this list? Click the Find a School link to find another school to add to this list."/>
<a href="schlfind.php">Find a school</a>
<br/>
<span class="fieldDescription">Select a school for your item</span>
</td>
</tr>
<tr>
<td>Category:</td>
<td>
<?php
category_box($cat);
?>
<br/>
<span class="fieldDescription">Select a category to help users find your item</span>
</td>
</tr>
<tr>
<td>Title:</td>
<td>
<input type="text" name="title" size="60" maxlength="100"
value="<?php echo $flds['title']['val'] ?>"/>
<br/>
<span class="fieldDescription">The title will be shown in item listings. Please make this brief but descriptive.</span>
</td>
</tr>
<tr>
<td colspan="2">
<span id="titlemsg" class="inputError">
<?php echo $flds['title']['m'] ?>
</span>
</td>
<tr>
<td>Price:</td>
<td>
<input type="text" name="price" size="10"
maxlength="8"
onkeypress="return iscurrency(event)"
value="<?php echo $flds['price']['val'] ?>"/> USD
<br/>
<span class="fieldDescription">How much would you like to charge for this item?</span>
</td>
</tr>
<tr>
<td colspan="2">
<span id="pricemsg" class="inputError">
<?php echo $flds['price']['m'] ?>
</span>
</td>
<tr>
<td>Quantity:</td>
<td>
<input type="text" name="quantity" size="10"
maxlength="3"
onkeypress="return isdigit(event)"
value="<?php echo $flds['quantity']['val'] ?>"/>
<br/>
<span class="fieldDescription">Do you have more than one of these for sale?</span>
</td>
</tr>
<tr>
<td colspan="2">
<span id="quantitymsg" class="inputError">
<?php echo $flds['quantity']['m'] ?>
</span>
</td>
<tr>
<tr>
<td>Size:</td>
<td>
<input type="text" name="size" size="10"
maxlength="10"
value="<?php echo $flds['size']['val'] ?>"/>
<br/>
<span class="fieldDescription">Enter the size (e.g. S, M, L or 5, 7, 9, etc.)</span>
</td>
</tr>
<tr>
<td>Condition:</td>
<td>
<?php
condition_box($condition);
?>
<br/>
<span class="fieldDescription">Select a Condition. Please make sure this is accurate.</span>
</td>
</tr>
<tr>
<td colspan="2">
<span id="sizemsg" class="inputError">
<?php echo $flds['size']['m'] ?>
</span>
</td>
<tr>
<?php
if($mode == "Add") {
?>
<tr>
<td>Add a picture:
</td>
<td>
<input type="file" name="image"/> (Max size: 5GB)
<br/>
<span class="fieldDescription">You can add a picture of your item if you would like.
This will make your item more appealing and help your sale.</span>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="2">Description (HTML OK)</td>
</tr>
<tr>
<td colspan="2">
<textarea name="long_desc" rows="8" cols="60"><?php echo $flds['long_desc']['val'] ?></textarea>
<br/>
<span class="fieldDescription">Describe your item here.</span>
</td>
</tr>
<tr>
<td colspan="2">
<span id="long_descmsg" class="inputError">
<?php echo $flds['long_desc']['m'] ?>
</span>
</tr>
<tr>
<td colspan="2">
<?php
if($mode == "Add") {
?>
<input type="submit" name="submit" value="List this item" onclick="formComplete()"/>
<?php
}
else {
?>
<input type="submit" name="submit" value="Update this item" onclick="formComplete()"/>
<?php
}
?>
<input type="button" name="cancel" onclick="cancelIt(); return false;" value="Cancel"/>
</td>
</tr>
</table>
</form>
</div>
</div>
<?php
ad_box();
close_body();
show_footer();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment