forked from tectronics/ChristianFlatshare
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpost-wanted.php
More file actions
1880 lines (1583 loc) · 80.2 KB
/
post-wanted.php
File metadata and controls
1880 lines (1583 loc) · 80.2 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use CFS\Database\CFSDatabase;
use CFS\GeoEncoding\CFSGeoEncoding;
use CFS\Image\CFSImage;
ini_set("session.gc_maxlifetime","3600");
session_start();
// Autoloader
require_once 'web/global.php';
connectToDB();
$database = new CFSDatabase();
$connection = $database->getConnection();
// Dissallow access if user not logged in
if (!isset($_SESSION['u_id'])) { header("Location:index.php"); exit; }
// ********************************************************************************
// Initalise variables
// ********************************************************************************
$id = init_var("id","REQUEST"); // Necessary, refers to ad being edited
$step = init_var("step","REQUEST",1); // Necessary, refers to current step in the process
if ($step > 6) { $step = 1; }
$step_request = init_var("step_request");
$direction = init_var("submit","POST","next_step"); // By default, we're going forward in steps
// http://www.webmasterworld.com/forum88/4894.htm Problem using "VALUE = " in IE
if ($_POST['submit_save_changes_x'] > 0) { $direction = "save_changes";}
if ($_POST['submit_prev_step_x'] > 0) { $direction = "prev_step"; }
if ($_POST['submit_next_step_x'] > 0) { $direction = "next_step"; }
if ($_POST['submit_publish_x'] > 0) { $direction = "publish"; }
$error = array();
$update_error = "";
// FIRST PANE elements
$location = init_var("location");
$postcode = init_var("postcode");
$distance_from_postcode = init_var("distance_from_postcode", "POST", 5);
$available_date = init_var("available_date");
$min_term = init_var("min_term","POST",0);
$max_term = init_var("max_term","POST",999);
$street = init_var("street");
$area = init_var("area");
$region = init_var("region");
$lookup_type = init_var("lookup_type");
$country = init_var("country");
$postal_code = init_var("postal_code");
$longitude = init_var("longitude");
$latitude = init_var("latitude");
// Use correct country settings
// If we have a country in the databse use that, otherwise
// use the users country to start the ad off
if (!empty($ad['latitude'])) {
$country = (empty($ad['country'])) ? 'GB' : $ad['country'];
$CFSIntl->setAppCountry($country);
}
else {
$CFSIntl->setAppCountry($userCountry['iso']);
}
$appCountry = $CFSIntl->getAppCountry();
// SECOND PANE elements
$accommodation_type_flat_share = init_var("accommodation_type_flat_share");
$accommodation_type_family_share = init_var("accommodation_type_family_share");
$accommodation_type_whole_place = init_var("accommodation_type_whole_place");
$accommodation_type_room_share = init_var("accommodation_type_room_share");
if (isset($_POST['building_type_house'])) { $building_type_house = $_POST['building_type_house']; } else { if (!$_POST) { $building_type_house = 1; } else { $building_type_house = 1; } }
if (isset($_POST['building_type_flat'])) { $building_type_flat = $_POST['building_type_flat']; } else { if (!$_POST) { $building_type_flat = 1; } else { $building_type_flat = 1; } }
$bedrooms_required = init_var("bedrooms_required","POST",1);
$price_pcm = (isset($_POST['price_pcm'])) ? $CFSIntl->parseCountryCurrency($_POST['price_pcm'], 'app') : NULL;
$furnished = init_var("furnished","POST","furnished or unfurnished");
$shared_lounge_area = init_var("shared_lounge_area");
$central_heating = init_var("central_heating");
$washing_machine = init_var("washing_machine");
$garden_or_terrace = init_var("garden_or_terrace");
$bicycle_store = init_var("bicycle_store");
$dish_washer = init_var("dish_washer");
$tumble_dryer = init_var("tumble_dryer");
$ensuite_bathroom = init_var("ensuite_bathroom");
$parking = init_var("parking");
// THIRD PANE elements
$current_age = init_var("current_age","POST",0); // Not for DB insert, used to calc min_age and max_age if one person.
$current_num_males = init_var("current_num_males");
$current_num_females = init_var("current_num_females");
$current_min_age = init_var("current_min_age","POST",0);
$current_max_age = init_var("current_max_age","POST",0);
$current_occupation = init_var("current_occupation");
$current_is_couple = init_var("current_is_couple");
$current_is_family = init_var("current_is_family");
$church_reference = init_var("church_reference");
$church_attended = addslashes(init_var("church_attended"));
if (isset($_POST['church_url'])) { $church_url = addslashes(strip_http(trim($_POST['church_url']))); } else { $church_url = NULL; }
$accommodation_situation = addslashes(init_var("accommodation_situation"));
// FOURTH PANE elements
if (isset($_POST['shared_adult_members'])) { $shared_adult_members = $_POST['shared_adult_members']; } else { $shared_adult_members = ($_POST)? NULL : "4+"; }
$shared_males = init_var("shared_males");
$shared_females = init_var("shared_females");
$shared_mixed = init_var("shared_mixed");
$shared_min_age = init_var("shared_min_age");
$shared_max_age = init_var("shared_max_age");
$shared_student = init_var("shared_student");
$shared_mature_student = init_var("shared_mature_student");
$shared_professional = init_var("shared_professional");
$shared_other = init_var("shared_other");
$shared_owner_lives_in = init_var("shared_owner_lives_in");
$shared_married_couple = init_var("shared_married_couple");
$shared_family = init_var("shared_family");
// FIFTH PANE elements
$contact_name = addslashes(init_var("contact_name"));
$contact_phone = addslashes(init_var("contact_phone"));
$flatmatch = init_var("flatmatch","POST",0);
$palup = init_var("palup","POST",0);
$picture = init_var("picture");
// SIXTH PANE elements
$photo_update_captions = init_var("photo_update_captions");
$photo_delete = init_var("photo_delete");
// ********************************************************************************
// Security check
// Ensure ad we're editing belongs to current user
// ********************************************************************************
$query = "select * from cf_wanted where wanted_id = '".$id."'";
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
if (!$result || mysqli_num_rows($result) == 0) { header("Location: post-choice.php"); exit; }
$ad = mysqli_fetch_assoc($result);
if ($ad['user_id'] != $_SESSION['u_id']) {
header("Location: your-account-manage-posts.php");
//die("You do not have permissions to edit this advertisement.");
}
// Age Slider Setup
if (isset($_POST['currentAgeRange'])) {
$currentAgeRange = $_POST['currentAgeRange'];
}
else if (isset($ad['current_min_age']) && isset($ad['current_max_age'])) {
if ($ad['current_min_age'] <= 18) $ad['current_min_age'] = 18;
if ($ad['current_max_age'] >= 51) $ad['current_max_age'] = 51;
$currentAgeRangePreview = cleanAge($ad['current_max_age'], $ad['current_max_age'], 'current');
$currentAgeRange = reverseAgeConvert($ad['current_min_age']) . '-' . reverseAgeConvert($ad['current_max_age']);
}
else {
$currentAgeRangePreview = cleanAge(21, 45, 'current');
$currentAgeRange = '2-6';
}
if (isset($_POST['suitAgeRange'])) {
$suitAgeRange = $_POST['suitAgeRange'];
}
else if (isset($ad['shared_min_age']) && isset($ad['shared_max_age'])) {
if ($ad['shared_min_age'] <= 18) $ad['shared_min_age'] = 18;
if ($ad['shared_max_age'] >= 51) $ad['shared_max_age'] = 51;
$suitAgeRangePreview = cleanAge($ad['shared_min_age'], $ad['shared_max_age'], 'suit');
$suitAgeRange = reverseAgeConvert($ad['shared_min_age']) . '-' . reverseAgeConvert($ad['shared_max_age']);
}
else {
$suitAgeRangePreview = cleanAge(18, 51, 'suit');
$suitAgeRange = '1-8';
}
// LEGACY SUPPORT
// Convert just postcodes into Lat/Lng Data
if (!empty($ad['postcode']) && empty($ad['latitude'])) {
$geoEncode = new CFSGeoEncoding();
$location = $geoEncode->getLookup($ad['postcode'], 'GB');
$address = $geoEncode->getReverseLookup($location['lat'], $location['lng']);
$sql = "UPDATE cf_wanted SET
latitude = :latitude,
longitude = :longitude,
street = :street,
area = :area,
region = :region,
country = :country,
postal_code = :postal_code
WHERE wanted_id = :id
AND user_id = :user_id";
$stmt = $connection->prepare($sql);
$stmt->bindValue("latitude", $address['latitude']);
$stmt->bindValue("longitude", $address['longitude']);
$stmt->bindValue("street", $address['street']);
$stmt->bindValue("area", $address['area']);
$stmt->bindValue("region", $address['region']);
$stmt->bindValue("country", $address['country']);
$stmt->bindValue("postal_code", $address['postal_code']);
$stmt->bindValue("id", $id);
$stmt->bindValue("user_id", getUserIdFromSession());
$result = $stmt->execute();
$sql = "SELECT * FROM cf_wanted WHERE wanted_id = :id";
$stmt = $connection->prepare($sql);
$stmt->bindValue("id", $id);
$stmt->execute();
$ad = $stmt->fetch();
}
// ********************************************************************************
// Whole place exception
// If ONLY whole place is chosen, set a flag (to help us hide elements as needed)
// ********************************************************************************
if (
$ad['accommodation_type_whole_place'] &&
!$ad['accommodation_type_flat_share'] &&
!$ad['accommodation_type_family_share'] &&
!$ad['accommodation_type_room_share']
){
$whole_place = TRUE;
if ($step == 4) {
header("Location: ".$_SERVER['PHP_SELF']."?id=".$id."&step=5");
exit;
}
} else {
$whole_place = FALSE;
}
// ********************************************************************************
// Handle POST action
// $_POST to this page = we need to 1) validate and 2) update db with data
// ********************************************************************************
if ($_POST) {
switch($step) {
// STEP 1: Location and dates
case 1:
$debug .= debugEvent("Step 1 POST",print_r($_POST,true));
if (empty($longitude) || empty($latitude)) {
$error['geo'] = 'Type in desired location and select a match from the list shown';
}
if ($street == 'undefined' || $region == 'undefined' || $area == 'undefined') {
$error['geo'] = 'Please move the marker to display a proper address';
}
if (!$distance_from_postcode) {
$error['distance_from_postcode'] = 'Please indicate distance from place';
}
if ($available_date == 0 ) {
$error['available_date'] = ' Please choose the date when accommodation is needed';
}
// If a min_term AND a max_term have been defined, make sure that max_term is after min_term
if ($min_term && $max_term != "999") {
if ($min_term > $max_term) {
$error['term'] = 'Maximum term must be larger than the minimum term';
}
}
// If we've encountered no error
if (!$error) {
$sql = "UPDATE cf_wanted SET
last_updated_date = now(),
location = :location,";
// if GB ensure that only the first half of the postcode is stored,
// otherwise this causes problems with jibble/seach
if ($appCountry['iso'] == 'GB') {
$sql .= "postcode = SUBSTRING_INDEX(:postcode,' ',1),";
} else {
$sql .= "postcode = :postcode,";
}
$sql .= "distance_from_postcode = :distance_from_postcode,
latitude = :latitude,
longitude = :longitude,
available_date = :available_date,
expiry_date = DATE_ADD(:available_date, interval 10 day),
min_term = :min_term,
max_term = :max_term,
street = :street,
area = :area,
region = :region,
country = :country,
postal_code = :postal_code
WHERE wanted_id = :id
AND user_id = :user_id";
$stmt = $connection->prepare($sql);
$stmt->bindValue("postcode", $postal_code);
$stmt->bindValue("distance_from_postcode", $distance_from_postcode);
$stmt->bindValue("latitude", $latitude);
$stmt->bindValue("longitude", $longitude);
$stmt->bindValue("available_date", $available_date);
$stmt->bindValue("min_term", $min_term);
$stmt->bindValue("max_term", $max_term);
$stmt->bindValue("street", $street);
$stmt->bindValue("area", $area);
$stmt->bindValue("region", $region);
$stmt->bindValue("country", $country);
$stmt->bindValue("postal_code", $postal_code);
$stmt->bindValue("id", $id);
$stmt->bindValue("location", $street . ', ' . $area);
$stmt->bindValue("user_id", getUserIdFromSession());
$result = $stmt->execute();
if (!$result) {
$update_error = '<p style="margin-bottom:0px;" class="error">ERROR: An error occured when inserting wanted ad in the ChristianFlatshare database. Please contact '.TECH_EMAIL.'</p>';
}
}
break;
// STEP 2: Accommodation details
case 2:
$debug .= debugEvent("Step 1 POST",print_r($_POST,true));
// Validate only if going forward
if ($direction != "prev_step") {
// Make sure at least one accommodation_type was picked
if (!$accommodation_type_flat_share && !$accommodation_type_family_share && !$accommodation_type_whole_place && !$accommodation_type_room_share) {
$error['accommodation_type'] = 'Please select one or more accommodation types';
}
// Make sure at least one building_type was picked
if (!$building_type_house && !$building_type_flat) {
$error['building_type'] = 'Please indicate a building type';
}
if (!preg_match('/^([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$/',trim(preg_replace(REGEXP_CURRENCY,'', str_replace('\\','',$price_pcm))))) {
$error['price_pcm'] = 'Please enter a monthly price (e.g. "250", without a "<html>£ </html>" sign)';
}
if ($price_pcm == 0 ) { $error['price_pcm'] = 'Enter an amount, e.g. "250", without a "<html>£ </html>" sign'; }
}
// If we've encountered no error
if (!$error) {
// Update DB
$query = "
UPDATE cf_wanted SET
accommodation_type_flat_share = '".intval($accommodation_type_flat_share)."',
accommodation_type_family_share = '".intval($accommodation_type_family_share)."',
accommodation_type_whole_place = '".intval($accommodation_type_whole_place)."',
accommodation_type_room_share = '".intval($accommodation_type_room_share)."',
building_type_flat = '".intval($building_type_flat)."',
building_type_house = '".intval($building_type_house)."',
bedrooms_required = '".$bedrooms_required."',
price_pcm = '".$price_pcm."',
furnished = '".$furnished."',
shared_lounge_area = '".intval($shared_lounge_area) ."',
central_heating = '".intval($central_heating)."',
washing_machine = '".intval($washing_machine)."',
garden_or_terrace = '".intval($garden_or_terrace)."',
bicycle_store = '".intval($bicycle_store)."',
dish_washer = '".intval($dish_washer)."',
tumble_dryer = '".intval($tumble_dryer)."',
ensuite_bathroom = '".intval($ensuite_bathroom)."',
parking = '".intval($parking)."'
WHERE wanted_id = '".$id."'
AND user_id = '".$_SESSION['u_id']."';
";
$debug .= debugEvent("Step 2 query:",$query);
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
if (!$result) {
$update_error = '<p style="margin-bottom:0px;" class="error">ERROR: An error occured when inserting wanted ad in the ChristianFlatshare database. Please contact '.TECH_EMAIL.'</p>';
}
}
break;
case 3:
// Validate only if going forward
if ($direction != "prev_step") {
if (!$current_num_males && !$current_num_females) {
$error['current_num_males'] = 'Please select how many males / females are looking for accommodation';
}
// CONVERT AGES
list($min, $max) = explode('-', $currentAgeRange);
$minimum_age = ageConvert($min, 'min_age');
$maximum_age = ageConvert($max, 'max_age');
if (!$current_occupation) {
$error['current_occupation'] = 'Please choose your occupation';
}
if (!$church_attended) {
$error['church_attended'] = "Please enter church name<br/>";
}
if (strlen($accommodation_situation) < 150) {
$error['accommodation_situation'] = 'Please enter at least 150 characters';
}
} // prev step if
// If we've encountered no error
if (!$error) {
// Update DB
$query = "
UPDATE cf_wanted SET
current_num_males = '".$current_num_males."',
current_num_females = '".$current_num_females."',
current_min_age = '".$minimum_age."',
current_max_age = '".$maximum_age."',
current_occupation = '".$current_occupation."',
current_is_couple = '".intval($current_is_couple)."',
current_is_family = '".intval($current_is_family)."',
church_reference = '".intval($church_reference)."',
church_attended = '".$church_attended."',
church_url = '".$church_url."',
accommodation_situation = '".$accommodation_situation."'
WHERE wanted_id = '".$id."'
AND user_id = '".$_SESSION['u_id']."';
";
$debug .= debugEvent("Step 3 query:",$query);
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
if (!$result) {
$update_error = '<p style="margin-bottom:0px;" class="error">ERROR: An error occured when inserting wanted ad in the ChristianFlatshare database. Please contact '.TECH_EMAIL.'</p>';
}
}
break;
case 4:
if (!$whole_place) {
// Validate only if going forward
if ($direction != "prev_step") {
if (!$shared_adult_members && ($accommodation_type_family_share || $accommodation_type_flat_share || $accommodation_type_room_share)) {
$error['shared_adult_members'] = "Please select the maximum number of adult household members<br/>";
}
if (
!$shared_males &&
!$shared_females &&
!$shared_mixed ) {
$error['shared_gender'] = "Please choose your household preference";
}
if ($shared_min_age && $shared_max_age && ($shared_min_age > $shared_max_age)) {
$error['shared_age'] = "Min age cannot be larger than max";
}
}
// CONVERT AGES
list($min, $max) = explode('-', $suitAgeRange);
$minimum_age = ageConvert($min, 'min_age');
$maximum_age = ageConvert($max, 'max_age');
// If we've encountered no error
if (!$error) {
// Update DB
$query = "
UPDATE cf_wanted SET
last_updated_date = now(),
shared_adult_members = '".$shared_adult_members."',
shared_males = '".intval($shared_males)."',
shared_females = '".intval($shared_females)."',
shared_mixed = '".intval($shared_mixed)."',
shared_min_age = '".$minimum_age."',
shared_max_age = '".$maximum_age."',
shared_student = '".intval($shared_student)."',
shared_mature_student = '".intval($shared_mature_student)."',
shared_professional = '".intval($shared_professional)."',
shared_owner_lives_in = '".intval($shared_owner_lives_in)."',
shared_married_couple = '".intval($shared_married_couple)."',
shared_family = '".intval($shared_family)."'
WHERE wanted_id = '".$id."'
AND user_id = '".$_SESSION['u_id']."';
";
$debug .= debugEvent("Step 4 query:",$query);
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
if (!$result) {
$update_error = '<p style="margin-bottom:0px;" class="error">ERROR: An error occured when inserting wanted ad in the ChristianFlatshare database. Please contact '.TECH_EMAIL.'</p>';
}
}
}
break;
case 5:
// Validate only if going forward
if ($direction != "prev_step") {
if (!$contact_name) {
$error['contact_name'] = 'Please enter your contact name<br/>';
}
if (!$picture) {
$error['picture'] = 'Please choose an advert picture for your ad';
}
}
// If we've encountered no error
if (!$error) {
// Update DB
$query = "
UPDATE cf_wanted SET
last_updated_date = now(),
picture = '".$picture."',
contact_name = '".$contact_name."',
contact_phone = '".$contact_phone."',
flatmatch = '".intval($flatmatch)."',
palup = '".intval($palup)."'
WHERE wanted_id = '".$id."'
AND user_id = '".$_SESSION['u_id']."';
";
$debug .= debugEvent("Step 5 query:",$query);
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
if (!$result) {
$update_error = '<p style="margin-bottom:0px;" class="error">ERROR: An error occured when inserting wanted ad in the ChristianFlatshare database. Please contact '.TECH_EMAIL.'</p>';
}
}
break;
case "6":
/*
The $direction variable usually gets "prev_step" or "next_step" as values,
depending on which button was clicked (previous or next respectively).
In step 6, we're handling image uploads so the direction variable will also get
some other values (namely the names of each of the extra submit buttons we have
appart from "prev_step" and "next_step".
Specifically:
"Upload photo" -> User has associated a file and is uploading it
*/
if ($direction == "Upload photo") {
// Get any potentially uploaded file
if (isset($_FILES['userfile']['tmp_name']) && is_readable($_FILES['userfile']['tmp_name'])) {
$file = new SplFileInfo($_FILES['userfile']['tmp_name']);
$image = new CFSImage($file);
}
else {
$image = NULL;
}
if ($image !== NULL) {
try {
// Validate file type
$image->validateFileExtension(array('jpg'));
// Validate file size (in MB)
$image->validateFileSize(20);
// Validate image size (in px)
$image->validateImageSize(480, 480);
} catch(Exception $e) {
$error['upload'] = $e->getMessage();
}
if (!isset($error['upload'])) {
// Attempt scale and save
try {
// Assume portrait
$filename = $image->scaleAndSave($id, 'wanted', 640, 480);
} catch(Exception $e) {
$error['upload'] = $e->getMessage();
}
if (isset($filename)) {
print $filename;
}
}
}
}
// If we're updating the captions of the photos
if ($photo_update_captions) {
/*
The post data will contain something along the lines of:
[id] => 1622
[post_type] => wanted
[ad_caption_1204] => (enter caption)
[ad_caption_1198] => (enter caption)
[ad_caption_1205] => (enter caption)
*/
foreach($_POST as $key => $value) {
if (preg_match('/^ad_caption_(\d+)$/',$key,$matches) && trim($value) != "(enter caption)") {
$query = "update cf_photos set caption = '".trim($value)."' where post_type = 'wanted' and photo_id = '".$matches[1]."'";
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
}
}
}
// If we're deleting photos
if ($photo_delete) {
// First, find out all details for the selected images
$sqlWhere = "";
foreach($_POST['ad'] as $ad_id) { $sqlWhere .= $ad_id.","; }
$sqlWhere = substr($sqlWhere,0,-1);
$query = "select * from cf_photos where post_type = 'wanted' and photo_id in (".$sqlWhere.");";
$debug .= debugEvent("Delete query #1",$query);
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
// Now delete all the image files
while ($row = mysqli_fetch_assoc($result)) {
if (is_readable("images/photos/" . $row['photo_filename'])) {
$file = new SplFileInfo("images/photos/" . $row['photo_filename']);
$image = new CFSImage($file);
$image->removeImage();
}
}
$query = "delete from cf_photos where post_type = 'wanted' and photo_id in (".$sqlWhere.");";
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
if ($result) {
$msg = '<p class="success">Photo(s) deleted</p>';
} else {
$msg = '<p class="error">There was an error deleting photo(s). Please contact '.TECH_EMAIL.'</p>';
}
break;
}
// IF WE'RE PUBLISHING
if ($direction == "publish") {
// Calculate the expiry date
// By default set it to 10 days after $available_date
//$temp = new Date($available_date);
//$temp->addSeconds(86400 * 10);
//$expiry_date = $temp->format("%Y-%m-%d");
// If scammer, with suppresed replies, set PUBLISHED to 0
$query = 'SELECT "X" FROM cf_users WHERE (suppressed_replies = 1 OR suppressed_replies = NULL) ';
$query .= 'AND user_id = '.$_SESSION['u_id'];
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
$num_results = mysqli_num_rows($result);
if ($num_results == 1) { $published = 0; } else { $published = DEFAULT_PUBLISH_STATUS; };
// If we're dealing with a new ad (i.e. "e" token is not present)
// then set published to 1 and redirect
$query = "
UPDATE cf_wanted SET
expiry_date = date_add('".$ad['available_date']."',interval 10 day),
published = '".$published."'
WHERE wanted_id = '".$id."'
";
$result = mysqli_query($GLOBALS['mysql_conn'], $query) or die(mysqli_error());
if ($result) {
header("Location: details.php?post_type=wanted&id=".$id."&new_ad=1");
exit;
} else {
$update_error = '<p style="margin-bottom:0px;" class="error">ERROR: An error occured when inserting wanted ad in the ChristianFlatshare database. Please contact '.TECH_EMAIL.'</p>';
}
}
break;
} // End big $step switch
// If we don't have any errors, proceed to next or previous step
if (!$error && !$update_error) {
// Proceed to the necessary step
if ($direction == "next_step") {
$step = $step + 1;
if ($step > 6) { $step = 6; }
}
if ($direction == "prev_step") {
$step = $step - 1;
if ($step < 1) { $step = 1; }
}
// If we're redirecting to a specific step
if ($step_request) { $step = $step_request; }
// STEP 4 EXCEPTION:
// If "whole place" only, we need to give step 4 a miss
if ($whole_place && $step == 4) {
if ($direction == "next_step") { $step = 5; }
if ($direction == "prev_step") { $step = 3; }
}
// Redirect: We'll either redirect to the next / previous step OR
// if the "save changes" button was pressed ($direction == "save"), back to the
// "Your ads"
if ($direction == "save_changes") {
header("Location: your-account-manage-posts.php");
exit;
}
// At this stage we're ready to show next step
// Redirect using $_GET to enable browser back + forward buttons
header("Location: ".$_SERVER['PHP_SELF']."?id=".$id."&step=".$step."#m");
exit;
}
} else {
// ********************************************************************************
// Load data from database
// When displaying each step, we need to load existing data
// ********************************************************************************
$debug .= debugEvent('AD data:',print_r($ad,true));
// We'll iterate through the $ad variable and create the necessary variables
// which will be automatically picked up by our form
foreach($ad as $k => $v) {
// Cerntain data from the ad are not needed
if (
$k != "wanted_id" &&
$k != "user_id" &&
$k != "created_date" &&
$k != "last_updated_date" &&
$k != "published" &&
$k != "times_viewed" &&
$k != "paid_for" &&
$k != "approved" &&
$k != "suspended" &&
$k != "recommendations"
) {
if ($v !== NULL) {
${$k} = $v;
}
if ($k == 'flatmatch' && $v === NULL) {
${$k} = 1;
}
if ($k == 'palup' && $v === NULL) {
${$k} = 1;
}
if ($k == 'distance_from_postcode' && $v == 0) {
${$k} = 5;
}
}
}
// A few quick mods where column names DON'T match field names
// #################################################
}
// If we're on step 5, we need to create the list of picture thumbnails
if ($step == 5) {
$displayPicCanvas = "";
$pics = parseDirectory("images/pictures");
shuffle($pics);
// Load the pic filename -> title details from db into the custom_titles array
$custom_titles = array();
$query = "SELECT filename,display_title FROM cf_picture_titles WHERE display_title != '';";
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$custom_titles[$row['filename']] = $row['display_title'];
}
}
foreach($pics as $pic) {
// Get the name of the file (replace hyphens with spaces, uppercase words)
$name = removeExtension(ucwords(preg_replace('/-/',' ',$pic)));
// Substitute with custom title if needed
if (isset($custom_titles[$pic])) { $name = $custom_titles[$pic]; }
$displayPicCanvas .= '<div class="displayPicContainer">'."\n";
$displayPicCanvas .= '<a href="#" onclick="javascript: $(\'picture_'.$pic.'\').checked = true; return false;">';
$displayPicCanvas .= '<div class="thumbnailContainer"><img src="images/pictures/'.urldecode($pic).'" border="0"/></div>';
$displayPicCanvas .= '</a>'."\n";
$displayPicCanvas .= '<p><input type="radio" name="picture" id="picture_'.$pic.'" value="'.$pic.'" ';
if ($picture == $pic) { $displayPicCanvas .= 'checked="checked"'; }
$displayPicCanvas .= '/>'.$name.'</p>'."\n";
$displayPicCanvas .= '</div>'."\n";
}
}
// If we're on step 6, we need to load all photos for this ad
if ($step == 6) {
$query = "select * from cf_photos where ad_id = '".$id."' and post_type = 'wanted' order by photo_sort asc;";
$result = mysqli_query($GLOBALS['mysql_conn'], $query);
$photoCount = mysqli_num_rows($result);
if (!$photoCount) {
$photos = '<p>You have not added any photographs yet</p>';
} else {
$photos = "\n\n";
$photos .= '<p class="mt0">You have added '.mysqli_num_rows($result);
$photos .= (mysqli_num_rows($result)!=1)? ' photos' : ' photo';
$photos .= ' to this advert. Use the buttons below to update captions and rotate or delete photos.<br />Click on a photo below to see it enlarged with your caption.</p> ';
while($row = mysqli_fetch_assoc($result)) {
// The image must have a max height of 90px and must fit on a 120 * 90 area
list($w,$h) = getImgRatio("images/photos/".$row['photo_filename'],"",90,120,90);
$photos .= '<div class="uploadPhotoContainer" id="photo_'.$row['photo_id'].'">'."\n";
$photos .= '<a href="images/photos/'.$row['photo_filename'].'" rel="lightbox[photos]" '.($row['caption']? 'title="'.$row['caption'].'"':'').'>'."\n";
$photos .= '<img src="thumbnailer.php?img=images/photos/'.$row['photo_filename'].'&w='.$w.'&h='.$h.'&rnd='.rand(1,100000).'" border="0"/>';
$photos .= '</a>';
// The caption box
if ($row['caption']) { $tempValue = $row['caption']; } else { $tempValue = "(enter caption)"; }
$photos .= '<div class="uploadPhotoCaption"><input type="text" name="ad_caption_'.$row['photo_id'].'" id="ad_caption_'.$row['photo_id'].'" value="'.$tempValue.'" maxlength="120" onfocus="fieldFocus(this.id);" onblur="fieldBlur(this.id);" /></div>'."\n";
// The selection checkbox
$photos .= '<div><input type="checkbox" name="ad[]" value="'.$row['photo_id'].'" onclick="return toggle(this);"/></div>'."\n";
$photos .= '</div>'."\n";
}
$photos .= '<div class="clear" style="height:0px;"><!----></div>'."\n";
$photos .= '<p class="m0">';
$photos .= '<input type="submit" name="photo_update_captions" value="Save all captions" /> ';
$photos .= '<br/></p><br/>Select a photograph from above and use the buttons below to rotate it 90o or delete it:';
// $photos .= '<input type="submit" name="photo_delete" value="Delete photo(s)" onclick="return validateDeletion();" />';
$photos .= '</p>'."\n";
}
}
// Format error
if ($error) { array_walk($error,'formatError'); }
$debug .= debugEvent('Current step:',$step);
$debug .= debugEvent('Session vars:',print_r($_SESSION,true));
// First time creation? Or editing existing ad?
// If ad['published'] == 2 it means we are dealing with an
// ad that's newly created, in which case we'll disable the
// tab navigation (jumping steps etc.) Alternatively, (published == 0 || 1)
// we assume that user is "editing" an existing ad so we'll allow navigation
$tab_links = array();
for ($i = 1; $i <= 6; $i++) {
$tab_links[$i] = ($ad['published'] == 2)? '#' : $_SERVER['PHP_SELF'].'?id='.$id.'&step='.$i;
}
$debug .= debugEvent("tablinks:",print_r($tab_links,true));
// Setup Rotue
if (isset($street) && !empty($street)) {
$address = $street . ', ' . $area . ', ' . $region;
}
else if (isset($street_name) && !empty($street_name)) {
$address = $street_name . ', ' . $town . ' (' . $postcode . ')';
}
else {
$address = '-';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Wanted Accommodation ad - Christian Flatshare</title>
<link href="styles/web.css" rel="stylesheet" type="text/css" />
<link href="styles/headers.css" rel="stylesheet" type="text/css" />
<link href="css/global.css?v=2" rel="stylesheet" type="text/css" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<!-- jQUERY -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/scripts/jquery.nouislider.min.js?v=2"></script>
<script type="text/javascript">
//no conflict jquery
jQuery.noConflict();
</script>
<!-- MooTools -->
<script language="javascript" type="text/javascript" src="includes/mootools-1.2-core.js"></script>
<script language="javascript" type="text/javascript" src="includes/mootools-1.2-more.js"></script>
<script language="javascript" type="text/javascript" src="includes/icons.js"></script>
<!-- GOOGLE MAPS API v3 -->
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&key=<?php print GOOGLE_CLOUD_BROWSER_KEY?>"></script>
<script src="scripts/chooser.js?v=3"></script>
<!-- InstanceBeginEditable name="head" -->
<script language="javascript" type="text/javascript">
<?php
if ($error) {
// If validation has failed we want the dialog box to appear
echo 'var unsaved_changes = true;';
} else {
// By default we assume that no unsaved changes exist
echo 'var unsaved_changes = false;';
}
?>
var ignore_url = false;
var step_request = "";
window.addEvent('domready',function() {
//tip_pricing
//<p><strong>Pricing</strong></p>
var myTips = new Tips('.tooltip');
if ($('tip_bedrooms_required')) {
$('tip_bedrooms_required').store('tip:title', 'Bedrooms required');
$('tip_bedrooms_required').store('tip:text', 'Please choose the number of rooms you require to rent.');
}
if ($('tip_price_pcm')) {
$('tip_price_pcm').store('tip:title', 'Price per Bedroom');
$('tip_price_pcm').store('tip:text', 'Please enter the maximum price per bedroom for the accommodation you would.<br /><br />For \"Whole Place\" ads, specifying <strong>3 bedrooms</strong> and <strong>£300 per bedroom</strong><br />would indicate that you are looking to pay up to <strong>£900 per month</strong> for a whole place.<br />The Whole Place price is show below.');
}
if ($('tip_church_attended')) {
$('tip_church_attended').store('tip:title', 'Church attended');
$('tip_church_attended').store('tip:text', 'If moving to a new area it can be of interest for others to see your<br />home church (or most recently attended), or just "looking for a church"<br />if you are looking to make a church connection for the first time.<br /><br />It is helpful to put the name of a church and its location, as there can<br />be many churches of the same name.');
}
if ($('tip_professional')) {
$('tip_professional').store('tip:title', 'Occupation');
$('tip_professional').store('tip:text', 'We use "professional" to indicate that someone is at the stage of life when they are<br /> working in some capacity (butler, butcher, or banker), or looking for work. It can also<br /> describe someone who is retired. <br /><br />Please use your advert\'s text boxes to add more details accommodation seeker(s).');
}
if ($('tip_sliders')) {
$('tip_sliders').store('tip:title', 'Sliders');
$('tip_sliders').store('tip:text', 'Click on the sliders to move them to indicate the age range. <br /><br /><b>Note</b> - if both sliders are on the same value, one will be on top of the other. If you have difficulty<br /> moving the sliders at this point, please try moving the slider shown in one direction a few times.');
}
// Preload all necessary buttons
var myImages = new Asset.images([
'/images/buttons/form_button_next_over.gif',
'/images/buttons/form_button_previous_over.gif',
'/images/buttons/form_button_publish_over.gif',
'/images/buttons/form_button_save_changes_over.gif'],{
onComplete: function(){
if ($('button_next_step')) {
$('button_next_step').addEvent('mouseenter',function() {
this.src = '/images/buttons/form_button_next_over.gif';
});
$('button_next_step').addEvent('mouseleave',function() {
this.src = '/images/buttons/form_button_next.gif';
});
}
if ($('button_prev_step')) {
$('button_prev_step').addEvent('mouseenter',function() {
this.src = '/images/buttons/form_button_back_over.gif';
});
$('button_prev_step').addEvent('mouseleave',function() {
this.src = '/images/buttons/form_button_back.gif';
});
}
if ($('button_publish')) {
$('button_publish').addEvent('mouseenter',function() {
this.src = '/images/buttons/form_button_publish_over.gif';
});
$('button_publish').addEvent('mouseleave',function() {
this.src = '/images/buttons/form_button_publish.gif';
});
}
if ($('button_save_changes')) {
$('button_save_changes').addEvent('mouseenter',function() {
this.src = '/images/buttons/form_button_save_changes_over.gif';
});
$('button_save_changes').addEvent('mouseleave',function() {