-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_to_fav.php
167 lines (144 loc) · 4.79 KB
/
add_to_fav.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
<?php
/*
* This is used to perform the favourite auction
* functionality implementation
*/
/**
*
* @global type $user used to get the current user's details
* @param type $form is the form type name
* @param type $form_state is the current values of the forms
* @param type $my_id is the my id inputed by the user
* @return string
* This function is used to create a simple form with elements
* add to auction and remove from auction
*/
function my_fav_form($form, &$form_state ) {
$form['add_to_fav'] = array(
'#type' => 'submit',
'#id' => 'fav-button',
'#value' => t('Add to favourite'),
'#validate' => array('my_add_fav_validate'),
'#ajax' => array(
'callback' => 'my_add_fav_submit',
'wrapper' => 'my-fav-form',
'method' => 'replace',
'effect' => 'fade',
)
);
}
else {
$form['remove_fav'] = array(
'#type' => 'submit',
'#id' => 'fav-button',
'#value' => t('Remove from favourite'),
'#validate' => array('my_remove_fav_validate'),
'#ajax' => array(
'callback' => 'my_remove_fav_submit',
'wrapper' => 'my-fav-form',
'method' => 'replace',
'effect' => 'fade',
)
);
}
return $form;
}
/**
*
* @param type $form contain the forms
* @param array $form_state conatain the values of the forms
* @return array
* This function is used to add a new table row to make
* the current running auction to a favourite auction of
* this current user
*/
function my_add_fav_submit($form, &$form_state) {
global $user;
if (form_get_errors('added_to_fav')) {
$form_state['rebuild'] = TRUE;
$commands = array();
$commands[] = ajax_command_prepend(NULL, theme('status_messages'));
return array('#type' => 'ajax', '#commands' => $commands);
}
$fav_id = db_insert('my_fav_table')
->fields(array(
'user_id' => $user->uid,
'fav_status' => 1,
'created_at' => date('Y-m-d H:i:s', time()),
'modified_at' => date('Y-m-d H:i:s', time())
))
->execute();
$form_state['rebuild'] = TRUE;
$command = array();
$commands[] = ajax_command_replace('#fav-button' . $my_id, drupal_render(drupal_build_form('my_fav_form', $form_state)));
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
*
* @param type $form contain the forms
* @param array $form_state conatain the values of the forms at this state
* @return array
* This function is used to update a table row to remove
* the current favourite auction for a current user
*/
function my_remove_fav_submit($form, &$form_state) {
global $user;
if (form_get_errors('removed_from_fav')) {
$form_state['rebuild'] = TRUE;
$commands = array();
$commands[] = ajax_command_prepend(NULL, theme('status_messages'));
return array('#type' => 'ajax', '#commands' => $commands);
}
$my_id = $form_state['my']['my_id'];
db_update('my_fav_table')
->fields(array('fav_status' => 0, 'modified_at' => date('Y-m-d H:i:s', time())))
->condition('user_id', $user->uid, '=')
->condition('fav_status', 1, '=')
->execute();
$form_state['rebuild'] = TRUE;
$command = array();
$commands[] = ajax_command_replace('#fav-button' . $my_id, drupal_render(drupal_build_form('my_fav_form', $form_state)));
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
*
* @global type $user contain current user information
* @param type $form contain the form
* @param type $form_state conatain the values of the forms at this state
* This fucntion check if the auction is already in the favourite table
*/
function my_add_fav_validate($form, &$form_state) {
//validation codes here
global $user;
$query = db_select('my_fav_table', 'aft');
$result = $query->fields('aft', array('fav_status'))
->condition('aft.user_id', $user->uid, '=')
->orderBy('aft.modified_at', 'DESC')
->range(0,1)
->execute()
->fetchAll();
if ($result[0]->fav_status == 1) {
form_set_error('added_to_fav', t('This auction is already mark as favourite,<br>Refresh the browser'));
}
}
/**
*
* @global type $user contain current user information
* @param type $form contain the form
* @param type $form_state conatain the values of the forms at this state
* This fucntion check if the auction not a favourite
*/
function my_remove_fav_validate($form, &$form_state) {
//validation codes here
global $user;
$query = db_select('my_fav_table', 'aft');
$result = $query->fields('aft', array('fav_status'))
->condition('aft.user_id', $user->uid, '=')
->orderBy('aft.modified_at', 'DESC')
->range(0,1)
->execute()
->fetchAll();
if ($result[0]->fav_status == 0 ) {
form_set_error('removed_from_fav', t('This not favourire now, Please add to favourite<br>Refresh the browser'));
}
}