summaryrefslogtreecommitdiff
path: root/testimonials_add.inc
blob: 15c95f75290538310d18110ba5b491163cc3da4c (plain)
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
<?php
function fellowship_testimonials_add_all() {
        $page_content = "";
        $fellowship_testimonials_add_form = drupal_get_form("fellowship_testimonials_add_form");
        $page_content .= drupal_render($fellowship_testimonials_add_form);
        return $page_content;
    }
function fellowship_testimonials_add_form($form,$form_state, $no_js_use = FALSE, $testimonial_id = 0) {
         $testimonial_id = arg(2);
         //var_dump($testimonial_id);die;
         $query = db_select('fellowship_testimonials');
         $query->fields('fellowship_testimonials');
         $query->condition('id', $testimonial_id);
         $result = $query->execute();
         $row = $result->fetchObject();
        $form = array();
        $form["fellowship_year"] = array(
            "#type" => "textfield",
            "#title" => "Enter the fellowship year participated by the fellow(s)",
            '#attributes' => array(
				'placeholder' => t('For eg: 2019')
			),
            "#required" => TRUE
        );
        $form["fellowship_task"] = array(
            "#type" => "textfield",
            "#title" => "Enter the fellowship task done by the fellow(s)",
            '#attributes' => array(
				'placeholder' => t('For eg: Python, DWSIM, eSim etc')
			),
            "#required" => TRUE
        );
        $form["opt_text_or_video"] = array(
            "#type" => 'select',
            "#title" => t('Select the type of testimonial'),
            "#options" => array(
                'T' => 'Text',
                'V' => 'Video'
                ),
            "#required" => TRUE,
            '#ajax' => array(
                    'callback' => 'ajax_testimonial_type'
                )
        );
        /*$form["testimonial_text"] = array(
            "#type" => 'textfield',
            //'#format' => 'full_html',
            "#title" => t('Enter the testimonial by user'),
            "#required" => TRUE,
            '#states' => array(
                'visible' => array(
                    ':input[name="opt_text_or_video"]' => array(
                        'value' => 'T'
                    )
                )
            ),
            '#default_value' => $row->testimonial_text
        );*/
        $form["testimonial_text"] = array(
            "#type" => "text_format",
            '#format' => 'full_html',
            "#title" => "Testimonial text",
            '#prefix' => '<div id="testimonial-text">',
            '#suffix' => '</div>',
           // "#required" => TRUE,
            '#states' => array(
                'visible' => array(
                    ':input[name="opt_text_or_video"]' => array(
                        'value' => 'T'
                    )
                )
            ),
        );
        $form["testimonial_video"] = array(
            "#type" => "textfield",
            "#title" => "Testimonial Video",
            '#attributes' => array(
				'placeholder' => t('Copy paste the static url of the video, for eg: https://static.fossee.in/fossee/videos/FOSSEE_intern_Video/DSC_0006.m4v')
			),
            '#size' => 90,
            '#prefix' => '<div id="testimonial-video">',
            '#suffix' => '</div>',
            //"#required" => TRUE,
            '#states' => array(
                'visible' => array(
                    ':input[name="opt_text_or_video"]' => array(
                        'value' => 'V'
                    )
                )
            ),
        );

  $form['fellows_fieldset'] = array(
            '#type' => 'fieldset',
            '#tree' => TRUE,
            '#prefix' => '<div id="fellows-fieldset-wrapper">',
            '#suffix' => '</div>'
        ); 
            if (empty($form_state['num_fellows'])) {
                $form_state['num_fellows'] = 1;
            }
            $temp = 0;
            for ($i = 0; $i < $form_state['num_fellows']; $i++) {
                $temp = $i;
                $form['fellows_fieldset'][$i]["s_text"] = array(
                    "#type" => "item",
                    "#markup" => "<h4><label>fellows : " . ($temp + 1) . "</label></h4>"
                );
                $form['fellows_fieldset'][$i]["fellowname"] = array(
                    "#type" => "textfield",
                    "#title" => "Name",
                    "#default_value" => ""
                );
                $form['fellows_fieldset'][$i]["institute"] = array(
                    "#type" => "textfield",
                    "#title" => "University",
                    "#default_value" => ""
                );
                $form['fellows_fieldset'][$i]["place"] = array(
                    "#type" => "textfield",
                    "#title" => "Place",
                    "#default_value" => ""
                );
               
            }
            $form["fellows_count"] = array(
                "#type" => "hidden",
                "#value" => $temp
            );
            $form['fellows_fieldset']['add_fellows'] = array(
                '#type' => 'submit',
                '#value' => t('Add fellow'),
                '#limit_validation_errors' => array(),
                '#submit' => array(
                    'fellows_add_more_add_one'
                ),
                '#ajax' => array(
                    'callback' => 'fellows_add_more_callback',
                    'wrapper' => 'fellows-fieldset-wrapper'
                )
            );
            if ($form_state['num_fellows'] > 1) {
                $form['fellows_fieldset']['remove_fellows'] = array(
                    '#type' => 'submit',
                    '#value' => t('Remove'),
                    '#limit_validation_errors' => array(),
                    '#submit' => array(
                        'fellows_add_more_remove_one'
                    ),
                    '#ajax' => array(
                        'callback' => 'fellows_add_more_callback',
                        'wrapper' => 'fellows-fieldset-wrapper'
                    )
                );
            }
            if ($no_js_use) {
                if (!empty($form['fellows_fieldset']['remove_fellows']['#ajax'])) {
                    unset($form['fellows_fieldset']['remove_fellows']['#ajax']);
                }
                unset($form['fellows_fieldset']['add_fellows']['#ajax']);
            }
        $form["submit"] = array(
            "#type" => "submit",
            "#value" => "Submit"
        );
        return $form;
    }

function ajax_testimonial_type($form, $form_state){
    $type = $form_state['values']['opt_text_or_video'];
    if($type == 'V'){
        $commands[] = ajax_command_replace('#testimonial-video', drupal_render($form['testimonial_video']));
        $commands[] = ajax_command_html('#testimonial-text', '');
    }
    else{
        $commands[] = ajax_command_replace('#testimonial-text', drupal_render($form['testimonial_text']));
        $commands[] = ajax_command_html('#testimonial-video', '');
    }
    return array(
        '#type' => 'ajax',
        '#commands' => $commands
    );
}

    function fellows_add_more_callback($form, $form_state) {
    return $form['fellows_fieldset'];
}


function fellows_add_more_add_one($form, &$form_state) {
    $form_state['num_fellows']++;
    $form_state['rebuild'] = TRUE;
    //$form_state['no_redirect'] = TRUE;
}


function fellows_add_more_remove_one($form, &$form_state) {
    if ($form_state['num_fellows'] > 1) {
        $form_state['num_fellows']--;
    }
    $form_state['rebuild'] = TRUE;
}

    function fellowship_testimonials_add_form_validate($form, &$form_state) {
        // for future use
    }

    function fellowship_testimonials_add_form_submit($form, &$form_state) {
        $v = $form_state["values"];
       
            if($v['opt_text_or_video'] == 'V'){
            $query = "
                INSERT INTO fellowship_testimonials(year, fellowship_task, testimonial_video, testimonial_type)
                VALUES (:year, :fellowship_task, :testimonial_video, :testimonial_type)";
            $args = array( 
                ':year' => $v["fellowship_year"],
                ':fellowship_task' => $v["fellowship_task"],
                ':testimonial_video' => $v["testimonial_video"],
                ':testimonial_type' => 'V'
                );
            }
            else if($v['opt_text_or_video'] == 'T'){
                $query = "
                INSERT INTO fellowship_testimonials(year, fellowship_task, testimonial_text, testimonial_text_format, testimonial_type)
                VALUES (:year, :fellowship_task, :testimonial_text, :testimonial_text_format, :testimonial_type)";
            $args = array( 
                ':year' => $v["fellowship_year"],
                ':fellowship_task' => $v["fellowship_task"],
                ':testimonial_text' => $v["testimonial_text"]["value"],
                ':testimonial_text_format' => $v["testimonial_text"]["format"],
                ':testimonial_type' => 'T'
                );
            }
            $result = db_query($query,$args, array(
        'return' => Database::RETURN_INSERT_ID
    ));
            $fellowsupload = 0;
            for ($i = 0; $i <= $v["fellows_count"]; $i++) {
                //$f_id=$v['fellows_fieldset'][$i]["f_id"];
                if ($v['fellows_fieldset'][$i]["fellowname"] != "") {
                    $fellowsquery = "INSERT INTO fellows (t_id,name,institute,place) VALUES (:t_id,:name,:institute,:place)";
                    $fellowsargs = array(
                        ":t_id" => $result,
                        ":name" => trim($v['fellows_fieldset'][$i]["fellowname"]),
                        ":institute" => trim($v['fellows_fieldset'][$i]["institute"]),
                        ":place" => trim($v['fellows_fieldset'][$i]["place"])
                    );
                    /* storing the row id in $result */
                    $fellowsresult = db_query($fellowsquery, $fellowsargs, array(
                        'return' => Database::RETURN_INSERT_ID
                    ));
                    if ($fellowsresult != 0) {
                        $fellowsupload++;
                    }
                }
            }
        //var_dump($result->name);die;
        if(!$result) {
            drupal_set_message("Something went wrong, please try again.", "error");
        } else {
            drupal_set_message("Testimonial added successfully", "status");
        }
        drupal_goto('fellowship-testimonials/edit');
    }