-
Notifications
You must be signed in to change notification settings - Fork 6
/
listing_edit.php
189 lines (160 loc) · 7.4 KB
/
listing_edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
include_once("includes/inc.global.php");
$cUser->MustBeLoggedOn();
$p->site_section = LISTINGS;
$title = $cDB->UnEscTxt($_REQUEST['title']);
$p->page_title = 'Edit '. $_REQUEST['type'] .': '.$title;
include("classes/class.listing.php");
include("includes/inc.forms.php");
//
// First we define the form
//
if($_REQUEST["mode"] == "admin") { // Administrator is creating listing for another member
$cUser->MustBeLevel(1);
$form->addElement("hidden","mode","admin");
$form->addElement("hidden", "member_id", $_REQUEST["member_id"]);
} else { // Member is creating offer for his/her self
$cUser->MustBeLoggedOn();
$form->addElement("hidden","member_id", $cUser->member_id);
$form->addElement("hidden","mode","self");
}
$form->addRule('title','Enter a title','required');
$form->registerRule('verify_not_duplicate','function','verify_not_duplicate');
//$form->addRule('title','You already have a listing with this title','verify_not_duplicate');
$category_list = new cCategoryList();
$form->addElement('select', 'category', 'Category', $category_list->MakeCategoryArray());
if(USE_RATES)
$form->addElement('text', 'rate', 'Rate', array('size' => 15, 'maxlength' => 30));
else
$form->addElement('hidden', 'rate');
$form->addElement('hidden', 'title', $title);
$form->addElement('hidden','type',$_REQUEST['type']);
$form->addElement('static', null, 'Description', null);
$form->addElement('textarea', 'description', null, array('cols'=>45, 'rows'=>5, 'wrap'=>'soft'));
$form->addElement('html', '<TR><TD></TD><TD><BR></TD></TR>');
$form->addElement('advcheckbox', 'set_expire_date', 'Should this listing be set to automatically expire?');
$today = getdate();
$options = array('language'=> 'en', 'format' => 'dFY', 'minYear' => $today['year'],'maxYear' =>$today['year']+5, 'addEmptyOption'=>'Y', 'emptyOptionValue'=>'0');
$form->addElement('date','expire_date', 'Expires', $options);
$form->registerRule('verify_future_date','function','verify_future_date');
$form->addRule('expire_date','Expiration must be for a future date','verify_future_date');
$form->registerRule('verify_valid_date','function','verify_valid_date');
$form->addRule('expire_date','Date is invalid','verify_valid_date');
$form->addElement('advcheckbox', 'set_reactivate_date', 'Should this listing be made temporarily inactive?');
$form->addElement('date','reactivate_date', 'Reactivates', $options);
$form->addRule('reactivate_date','Must be a future date','verify_future_date');
$form->addRule('reactivate_date','Date is invalid','verify_valid_date');
$form->addElement('submit', 'btnSubmit', 'Update');
//
// Then check if we are processing a submission or just displaying the form
//
if ($form->validate()) { // Form is validated so processes the data
$form->freeze();
$form->process('process_data', false);
} else { // Download existing values and display them
$listing = new cListing;
$listing->LoadListing($title,$_REQUEST['member_id'],substr($_REQUEST['type'],0,1));
if ($listing->expire_date) {
$temporary_listing = true;
$expire_date = array ('d'=>substr($listing->expire_date,8,2),'F'=>date('n',strtotime($listing->expire_date)),'Y'=>substr($listing->expire_date,0,4)); // Using 'n' due to a bug in Quickform
} else {
$temporary_listing = false;
$expire_date = array("d"=>0, "F"=>0, "Y"=>0);
}
if ($listing->reactivate_date) {
$inactive_listing = true;
$reactivate_date = array ('d'=>substr($listing->reactivate_date,8,2),'F'=>date('n',strtotime($listing->reactivate_date)),'Y'=>substr($listing->reactivate_date,0,4)); // Using 'n' due to a bug in Quickform
} else {
$inactive_listing = false;
$reactivate_date = array("d"=>0, "F"=>0, "Y"=>0);
}
$current_values = array ("title"=>$listing->title, "description"=>$listing->description, "rate"=>$listing->rate, "category"=>$listing->category->id, "set_expire_date"=>$temporary_listing, "expire_date"=>$expire_date, "set_reactivate_date"=>$inactive_listing, "reactivate_date"=>$reactivate_date);
$form->setDefaults($current_values);
$p->DisplayPage($form->toHtml()); // just display the form
}
//
// The form has been submitted with valid data, so process it
//
function process_data ($values) {
global $p, $cUser,$cErr, $cDB, $title;
$list = "";
$listing = new cListing();
$listing->LoadListing($title,$_REQUEST['member_id'],substr($_REQUEST['type'],0,1));
$date = $values['expire_date'];
$expire_date = $date['Y'] . '/' . $date['F'] . '/' . $date['d'];
$date = $values['reactivate_date'];
$reactivate_date = $date['Y'] . '/' . $date['F'] . '/' . $date['d'];
$today = getdate();
if($values['set_expire_date'] and $expire_date != "0/0/0") {
// they checked the box and entered a date, so store the value
$listing->expire_date = htmlspecialchars($expire_date);
} elseif ($listing->expire_date==null and $expire_date != "0/0/0") {
// they didn't check it but they changed the date, so store
$listing->expire_date = htmlspecialchars($expire_date);
} else {
$listing->expire_date = null;
if($listing->status == 'E') // they must have unchecked the box or blanked the date
$listing->status = 'A';
}
if($values['set_reactivate_date'] and $reactivate_date != "0/0/0") {
// they checked the box and entered a date, so store the value
$listing->reactivate_date = htmlspecialchars($reactivate_date);
$listing->status = 'I';
} elseif ($listing->reactivate_date==null and $reactivate_date != "0/0/0") {
// they didn't check it but they changed the date, so store
$listing->reactivate_date = htmlspecialchars($reactivate_date);
$listing->status = 'I';
} else {
$listing->reactivate_date = null;
if($listing->status == 'I') // they must have unchecked the box or blanked the date
$listing->status = 'A';
}
$listing->title = htmlspecialchars($title);
$listing->description = htmlspecialchars($values['description']);
$listing->category->id = htmlspecialchars($values['category']);
$listing->rate = $values['rate'];
$created = $listing->SaveListing();
if($created) {
$list .= 'Listing changes saved. Do you want to <A HREF="listing_to_edit.php?mode='. $_REQUEST['mode'] .'&member_id='. $_REQUEST["member_id"] .'&type='. $_REQUEST["type"] .'">edit</A> another listing?';
} else {
$cErr->Error("There was an error saving the listing. Please try again later.");
}
$p->DisplayPage($list);
}
//
// And finally, the following functions verify form data
//
function verify_future_date ($element_name,$element_value) {
global $form, $title;
$listing = new cListing;
$listing->LoadListing($title,$_REQUEST['member_id'],substr($_REQUEST['type'],0,1));
if ($listing->status == 'E' and !$form->getElementValue("set_expire_date")) {
return true; // They must have unchecked the box to reactivate the listing
}
$today = getdate();
$date = $element_value;
if($date['F'] == '0' and $date['d'] == '0' and $date['Y'] == '0')
return true;
$date_str = $date['Y'] . '/' . $date['F'] . '/' . $date['d'];
if (strtotime($date_str) <= strtotime("now")) // date is a past date
return false;
else
return true;
}
function verify_valid_date ($element_name,$element_value) {
$date = $element_value;
if($date['F'] == '0' and $date['d'] == '0' and $date['Y'] == '0')
return true;
return checkdate($date['F'],$date['d'],$date['Y']);
}
function verify_not_duplicate ($element_name,$element_value) {
global $cUser;
$title_list = new cTitleList();
$titles = $title_list->MakeTitleArray($cUser->member_id);
foreach ($titles as $title) {
if($element_value == $title)
return false;
}
return true;
}
?>