-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-pledge.php
124 lines (107 loc) · 3.34 KB
/
make-pledge.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
<?php if ( !is_user_logged_in() ) { auth_redirect(); } ?>
<?php
/**
* The template for creating new pledges for a user
*
Template Name: Make Pledge Template
*/
function my_styles() {
wp_enqueue_style('pure-forms');
wp_enqueue_style('pure-buttons');
wp_enqueue_script('pledge-change');
}
add_action( 'wp_enqueue_scripts', 'my_styles', 20);
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="entry-content">
<?php
/* find the current user information */
global $current_user;
get_currentuserinfo();
$current_year = date('Y');
/* setup the template engine */
define('SMARTY_CHILDTHEMES', true);
$smarty = smarty_get_instance();
/* setup the ORM engine */
R::setup('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
/*
* Fetch the user and profile information from the DB
*/
$user = R::load('user',$current_user->ID);
$res = $user->withCondition('year = ?', array($current_year))->ownProfile;
if (count($res) == 1) {
$profile = array_pop($res);
} else {
echo "<p class='error'>Bad Profile Count: " . count($res) . "</p>";
}
$res = $user->withCondition('year = ?', array($current_year))->ownPledge;
if (count($res) == 1) {
$pledge = array_pop($res);
}
/*
* Defaults
*/
$income = 0;
$cons_amount = 0;
$sav_amount = 0;
$edit_profile_url = get_bloginfo('wpurl') . "/edit-profile";
/*
* Handle a form submission
*/
if(count($_POST) > 0) {
if (isSet($pledge)) {
// can't update the pledge year
} else {
$pledge = R::dispense('pledge');
$user->ownPledge[] = $pledge;
$pledge->year = $current_year;
}
$pledge->sav_amount = $_POST['sav_amount'];
$pledge->cons_amount = $_POST['cons_amount'];
$pledge->charity = $_POST['charity'];
try {
R::store($user);
} catch(Exception $ex) {
echo "<p class='error'>ERROR: " . $ex->getMessage() . "</p>";
}
$cons_amount = $_POST['cons_amount'];
$sav_amount = $_POST['sav_amount'];
$charity = $_POST['charity'];
} else {
if (isSet($pledge)) {
$cons_amount = $pledge->cons_amount;
$sav_amount = $pledge->sav_amount;
$charity = $pledge->charity;
}
}
if(isSet($profile)){
$income = $profile->income;
}
// decide if we're updating or registering a new pledge
if(isSet($cons_amount)) {
$pledge_text = "You have already made a pledge for " . $current_year . ".";
$submit = 'Update My Pledge';
} else {
$pledge_text = "How much do you want to pledge for " . $current_year . "?";
$submit = 'Make a Pledge';
}
/*
* Configure and display the form using the template engine
*/
$smarty->assign('name', $current_user->display_name);
$smarty->assign('current_year',$current_year);
$smarty->assign('edit_profile_url',$edit_profile_url);
$smarty->assign('income',$income);
$smarty->assign('cons_amount', $cons_amount);
$smarty->assign('sav_amount', $sav_amount);
$smarty->assign('charity', $charity);
$smarty->assign('pledge_text', $pledge_text);
$smarty->assign('submit', $submit);
$smarty->display("pledge-form.html");
?>
</div><!-- .entry-content -->
</div><!-- #content -->
</div><!-- #primary -->
<?php /* get_sidebar(); */ ?>
<?php get_footer('mwm'); ?>