summaryrefslogtreecommitdiff
path: root/conference_proposal.module
blob: 3c7cf22b46fea8a61ba1a561a16c733c5c4a19e3 (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
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
<?php 
    function conference_proposal_permission() {
        return array(
            "access conference_proposal" => array(
                "title" => t("Access proposal Portal"),
                "description" => t("Allows users to view proposal postings.")
            ),
            "manage conference_proposal" => array(
                "title" => t("Manage proposal Portal"),
                "description" => t("Allows users to manage proposal postings.")
            ),
            "manage download_application" => array(
                "title" => t("Manage Download Portal"),
                "description" => t("Allows users to download proposal applications.")
            ),
        );
    }

    function conference_proposal_menu() {
        $items = array();
        $items["conference_proposal"] = array(
            "title" => "Abstracts",
            "page callback" => "conference_proposal_page",
            "access arguments" => array("access conference_proposal"),
            "type" => MENU_NORMAL_ITEM,
        );
        $items["conference_proposal/apply"] = array(
            "title" => "Abstract Submission Form",
            "page callback" => "conference_proposal_application_page",
            "access arguments" => array("access conference_proposal"),
            "type" => MENU_CALLBACK,
        );
        // $items["conference_proposal/edit"] = array(
        //     "title" => "Edit Proposals form",
        //     "page callback" => "conference_proposal_application_edit_page",
        //     "access arguments" => array("access conference_proposal"),
        //     "type" => MENU_CALLBACK,
        // );        
        $items["conference_proposal/view-applications"] = array(
            "title" => "View Applications",
            "page callback" => "conference_proposal_view_application_page",
            "access arguments" => array("manage conference_proposal"),
            "type" => MENU_CALLBACK,
        ); 
        // $items["conference_proposal/Edit-proposals"] = array(
        //     "title" => "Edit Proposals",
        //     "page callback" => "edit_conference_proposal_page",
        //     "access arguments" => array("access conference_proposal"),
        //     "type" => MENU_CALLBACK
        // );               
        // $items["conference_proposal/downloads_proposals"] = array(
        //     "title" => "Application Download",
        //     "description" => "Applications Download", 
        //     "page callback" => "proposals_downloads_page",
        //     "access arguments" => array("access proposal_portal"),
        //     "type" => MENU_CALLBACK            
        // );
        $items["conference_proposal/ajax"] = array(
            "title" => "Ajax callbacks",
            "page callback" => "conference_proposal_ajax",
            "access arguments" => array("access conference_proposal"),
            "type" => MENU_CALLBACK
        );
        
        return $items;
    }

    function conference_proposal_application_form($form, &$form_state, $proposal_id = 0) {
        global $user;   
           
        $form = array();
        $form["wrapper"] = array(
            "#type" => "lable",
            "#title" => t("Form"), 
        );        
        $form["wrapper"]["fname"] = array(
            "#type" => "textfield",
            "#title" => t("First Name"),                       
            "#required" => TRUE,
            '#attributes' => array(
            "placeholder" => "Please enter your first name"
            )           
        );
        $form["wrapper"]["lname"] = array(
            "#type" => "textfield",
            "#title" => t("Last Name"),                       
            "#required" => TRUE,
            '#attributes' => array(
            "placeholder" => "Please enter your last name"
            )
        );
        $form["wrapper"]["contributer"] = array(
            "#type" => "textfield",
            "#title" => t("Any Contributer"),  
            '#attributes' => array(
            "placeholder" => "Please enter contributers name"
            )
        );
            
        $form["wrapper"]["contact"] = array(
            "#type" => "textfield",
            "#title" => t("Contact"),          
            "#required" => TRUE,
            '#attributes' => array(
            "placeholder" => "Please enter your contact number"
            )           
        );
        $form["wrapper"]["email"] = array(
            "#type" => "textfield",
            "#title" => t("Email"),            
            "#required" => TRUE,
            '#default_value' => $user->mail,
            '#attributes' => array(
            "placeholder" => "Please enter your email"
            )
        );
        $form["wrapper"]["proposal_id"] = array(
          '#type' => 'select',
          '#title' => t('Proposal Type'),
          '#options' => get_conference_proposal_available("options"),
          //'#description' => t(' you like to do'),
          '#attributes' => array('disabled' => 'disabled'),
          '#default_value' => $proposal_id,
        );
        $form["wrapper"]["title"] = array(
            "#type" => "textfield", 
            "#title" => t("Title"),                            
            "#required" => TRUE,
            '#attributes' => array(
            "placeholder" => "Please enter your title"
            )
        );        
        // $form["wrapper"]["abstract"] = array(
        //     "#type" => "textarea",
        //     "#title" => t("Abstract"),            
        //     "#required" => TRUE,
        //     '#attributes' => array(
        //     "placeholder" => "Please enter your abstract"
        //     )
        // );
        $form["wrapper"]["bio"] = array(
            "#type" => "textarea",
            "#title" => t("Bio"),
            "#required" => TRUE,
            '#attributes' => array(
            "placeholder" => "Please enter your bio"
            )
        );
        $form["wrapper"]["link"] = array(
            "#type" => "textfield",
            "#title" => t("Link"),            
            '#attributes' => array(
            "placeholder" => "Please enter proposal related link"
            )           
        );
        $form["wrapper"]["abstract_file"] = array(
            "#type" => "file",
            "#title" => t("Abstract File" . required_star()),
            "#description" => t("Please upload your abstract file in [.pdf] format"),
        );
        $form["wrapper"]["supported_file"] = array(
            "#type" => "file",
            "#title" => t("Supporting File"),
            "#description" => t("Please upload your suporting file in [.zip] format"),
        );
        $form["wrapper"]["user_id"] = array(
            "#type" => "hidden",
            "#default_value" => $user->uid,
          );
        $form["wrapper"]["submit"] = array(
            "#type" => "submit",
            "#value" => "Submit Abstract"
        );
        return $form;    
    }

    function  conference_proposal_application_form_validate($form, &$form_state) {
        $abstractfile = file_save_upload('abstract_file', array(
            // Validate extensions.
             
            'file_validate_extensions' => array('pdf'), 
                    
        ));        
        // If the file passed validation:
        if ($abstractfile) {

            // Move the file into the Drupal file system.
            if ($abstractfile = file_move($abstractfile, 'public://')) {
              // Save the file for use in the submit handler.
              $form_state['storage']['abstract_file'] = $abstractfile;            
            }
            else {
              form_set_error('abstract_file', t("Failed to uploaded file to the site's file folder."));
            }
        }
        else {
            form_set_error('abstract_file', t('No file was uploaded.'));
        }
        
        /* validating email field */
        if(!valid_email_address($form_state["values"]["email"])) {
            form_set_error('email', t('Please enter an valid email address.'));
        }

        $supportedfile = file_save_upload('supported_file', array(
            // Validate extensions.
            'file_validate_extensions' => array('zip'),
        ));        
        // If the file passed validation:
        if ($supportedfile) {
            // Move the file into the Drupal file system.
            if ($supportedfile = file_move($supportedfile, 'public://')) {
              // Save the file for use in the submit handler.
              $form_state['storage']['supported_file'] = $supportedfile;             
            }
            else {
              form_set_error('supported_file', t("Failed to uploaded file to the site's file folder."));
            }
        }
        else {
            form_set_error('supported_file', t('No file was uploaded.'));
        }
    }

    function conference_proposal_application_form_submit($form, &$form_state) {
        $v = $form_state["values"];        
        $abstract_file = $form_state["storage"]["abstract_file"];
        
        $supported_file = $form_state["storage"]["supported_file"];
        $query = "
            INSERT INTO proposal_applications
            (first_name, last_name,  contributer, contact, email, title, bio, link, proposal_file, supported_file, proposal_id, uid)
            VALUES
            (:fname, :lname, :contributer, :contact, :email, :title, :bio, :link, :abstract_file, :supported_file, :proposal_id, :user_id)
        ";
        $args = array(
           
            ":fname" => $v["fname"], 
            ":lname" => $v["lname"], 
            ":contact" => $v["contact"],
            ":contributer" => $v["contributer"],
            ":email" => $v["email"], 
            ":title" => $v["title"],              
            //":abstract" => $v["abstract"], 
            ":bio" => $v["bio"], 
            ":link" => $v["link"], 
            ":abstract_file" => $abstract_file->filename,
            ":supported_file" => $supported_file->filename,
            ":proposal_id" => $v["proposal_id"],
            ":user_id" => $v["user_id"],
        );
        /* storing the row id in $result */
        $result = db_query($query, $args, array('return' => Database::RETURN_INSERT_ID));
        dpm($result);
        
        /* moving the file to uploads */
        $base_path = $_SERVER['DOCUMENT_ROOT'] . base_path();
        $uploads_dir = $base_path  . "uploads/proposal_file";
        if(!file_exists($uploads_dir . "/{$result}/")) {
            mkdir($uploads_dir . "/{$result}/", 0755, TRUE);
        }


 //        $from = "abstarct@scilab.in";
 //        $to = $v["email"];
 //       // $cc = $emails;
 //        $bcc = "rush2jrp@gmail.com";
 //        $subject = "Test Mail";
 //        $message = "
           
 //            <p><em>Please check the abstract to this mail.</em></p>
 //        ";
 //        $file = $uploads_dir . "/{$result}/" . $abstract_file->filename;
	// dpm($file);
 //        $mail_status = send_mail_attachment($from, $to, "", $bcc, $subject, $message, $file);
 //        if(!$mail_status) {
 //            drupal_set_message("An error occurred while sending mail.", "error");
 //        } else {
 //            drupal_set_message("We have received your application. Thank you!", "status");
 //        }

        file_unmanaged_move($abstract_file->uri, $uploads_dir . "/{$result}/{$abstract_file->filename}");
        drupal_set_message("Abstract Uploaded Succesfully");
        file_unmanaged_move($supported_file->uri, $uploads_dir . "/{$result}/{$supported_file->filename}"); 
        drupal_set_message("Supported files Uploaded Succesfully");          
         /* cleaning up temporary storage */
        file_delete($abstract_file);
        unset($form["storage"]["abstract_file"]);
        file_delete($supported_file);
        unset($form["storage"]["supported_file"]); 

        drupal_set_message("Thank you for submitting your abstract");
       

        /* sending notification to the applicant */
        $subject = "Abstract received";
        $message = "
        Dear {$v['fname']},

        We have received your abstract
        We will get back to you shortly.

        Regards,
        FOSSEE Team
                ";
                send_mail("contact@fossee.in", $v["email"], $subject, $message);

    }

    function conference_proposal_page(){        
       $output = ""; 
        $i = 1;        
        
        $result = get_conference_proposal_available();
        foreach($result as $row) {           
            $output .= "
                <strong>Proposal</strong>: {$row->proposal_name}<br>
                <strong>Subbmission Guidelines</strong>: <br>{$row->proposal_requirement}<br>
            ";
            $output .= "<center>"; 
            $output .= l("Submit Now ", "conference_proposal/apply/{$row->id}" , array(
                "attributes" => array("class" => "btn btn-primary"),
                "html" => TRUE,
            ));
            $output .= "</center><br>";          
           
            $i++;
        }               
        return $output;   
        
    }

    function conference_proposal_view_application_page($proposal_id=0) {
        $markup = "";
        if(array_key_exists("saved", $_GET)){
            drupal_set_message("Selections saved successfully.", "success");
        }               
        if($proposal_id) {
            $result = db_select("proposal_applications")
                ->fields("proposal_applications")
                ->condition("proposal_id",$proposal_id)
                ->execute()->fetchAll();
                
            $headers = array(
                "#", "Name", "Bio" , "View abstract", "Download abstract", "Selected",
            );
            $rows = array();
            $i = 1;
            foreach($result as $row) {
                $item = array(
                    $i,
                    $row->first_name.' '.$row->last_name,
                     $row->bio,                    
                    l(str_replace('.', '_',str_replace(' ', '_',strtolower("{$row->id}_{$row->first_name}_{$row->last_name}"))) . '.pdf', "uploads/proposal_file/{$row->id}/{$row->proposal_file}", array("attributes" => array("target" => "_blank",))),
                    l(str_replace('.', '_',str_replace(' ', '_',strtolower("{$row->id}_{$row->first_name}_{$row->last_name}"))) . '.zip', "uploads/proposal_file/{$row->id}/{$row->supported_file}", array("attributes" => array("target" => "_blank",))),
                );
                if($row->selected) {
                    $check = "<input class='shortlist' type='checkbox' data-aid='{$row->id}' checked>";
                } else {
                    $check = "<input class='shortlist' type='checkbox' data-aid='{$row->id}'>";
                }
                array_push($item, $check);   
                // $proposal = db_select("proposal_type")
                //     ->fields("proposal_type")
                //     ->condition("id", $proposal_id)
                //     ->execute()->fetchObject();
                
                // $base_path= $_SERVER['DOCUMENT_ROOT'] . base_path(); 
                // $downloads_dir = "uploads/proposal_file/{$row->id}/{$row->proposal_file}";
                // $files = $downloads_dir; 
                // $zipname = str_replace(' ','_',strtolower($proposal->proposal_name.'_'.'.zip'));
                // $zip = new ZipArchive;
                // $zip->open($zipname, ZipArchive::CREATE); 
                // $zip->addFile($files, str_replace('.','_',str_replace(' ','_',strtolower("{$row->id} {$row->first_name} {$row->last_name}" ))) . ".pdf");   
                // $zip->close();
  
                array_push($rows, $item);
                $i++;
            }

            $proposal = db_select("proposal_type")
                ->fields("proposal_type")
                ->condition("id", $proposal_id)
                ->execute()->fetchObject();
            $markup .= l("<< Back to the list of Proposals", "conference_proposal/view-applications");
            $markup .= "<div style='float:right'>";
            $markup .= l("Download all applications", "conference_proposal/downloads_applications/{$row->proposal_id}/{$row->id}");
            $markup .= "</div>";  
            $markup .= "<h5><u>{$proposal->proposal_name} - List of applications</u></h5>";
            $markup .= bootstrap_table($headers, $rows);
            $markup .= l("Save Selections", "conference_proposal/view-applications/",
                array(
                    "query" => array(
                        "saved" => "true"
                    ),
                    "attributes" => array(
                        "class" => "btn btn-primary"
                    )
                )
            );
        } else {
            /* List all the job positions.
             * Change the condition later based on end date.
            */
            $result = db_select("proposal_type")
                ->fields("proposal_type")
                ->condition("status", 0, '<>')
                ->execute()->fetchAll();
            $headers = array(
                "#", "Proposal", "Time", "Action",
            );
            $rows = array();
            foreach($result as $row) {
                $item = array(
                    $row->id,
                    "{$row->proposal_name}",
                    $row->time,
                    l("View applications", "conference_proposal/view-applications/{$row->id}")
                );
                array_push($rows, $item);
            }
            $markup .= "<h5><u>List of Proposals</u></h5>";
            $markup .= bootstrap_table($headers, $rows);
        }
        $output = array(
            "positions_list" => array(
                "#prefix" => "<div id='positions-list'>",
                "#markup" => $markup,
                "#suffix" => "</div>",
            ),
        );
        return $output;
    }

    function conference_proposal_application_page($proposal_id=0){
        $application_form = "";
        if($proposal_id) {
             $application_form = drupal_get_form("conference_proposal_application_form", $proposal_id);
             $application_form = drupal_render($application_form);            
        } else {
             $application_form = drupal_get_form("conference_proposal_application_form");
             $application_form = drupal_render($application_form);  
        }
        $output = array(
            "application_form" => array(
                "#prefix" => "",
                "#markup" => $application_form,
                "#suffix" => "",
            )
        );
        return $output;
    }

    function get_conference_proposal_available($key="") {
        $result = db_select("proposal_type")
            ->fields("proposal_type")
            ->condition("status", 1)
            ->execute()->fetchAll();        
        if($key == "options") {
            $options = array();
            foreach($result as $row) {
                $options[$row->id] = "{$row->proposal_name}";
            }
            return $options;
        } else {
            return $result;
               }
    }

     function conference_proposal_ajax($item="", $key="") {
        $data = "";
        if($item == "shortlist") {
            $query = "
                UPDATE proposal_applications
                SET selected = !selected
                WHERE id = aid
            ";
            $args = array(
                ":aid" => $key
            );
            db_query($query, $args);
            $data = "updated";
        }
        echo $data;
        exit();
    }
    function conference_proposal_init() {
        drupal_add_js("misc/form.js");
        drupal_add_js("misc/collapse.js");
        drupal_add_js(
            drupal_get_path('module', 'conference_proposal') . '/js/main.js',
            array(
                'group' => JS_THEME,
                'weight' => 20,
                'every_page' => TRUE,
                'cache' => TRUE,
                //'scope' => 'header',
            )
        );
    }
    function required_star() {
        return "<span class='form-required' title='This field is required.'> *</span>";
    }

     function bootstrap_table($headers, $rows) {
        $thead = "";
        $tbody = "";
        foreach($headers as $header) {
            $thead .= "<th>{$header}</th>";
        }
        foreach($rows as $row) {
            $tbody .= "<tr>";
            foreach($row as $data) {
                $tbody .= "<td>{$data}</td>";
            }
            $tbody .= "</tr>";
        }
        $table = "
            <table class='table table-bordered table-hover'>
                <thead>{$thead}</thead>
                <tbody>{$tbody}</tbody>
            </table>
        ";
        return $table;
    } 
    
    /**
     * Simple wrapper function for drupal_mail() to avoid extraneous code.
    */
    function send_mail($from, $to, $subject, $message) {
        $my_module = 'conference_proposal';
        $my_mail_token = microtime();
        $message = array(
            'id' => $my_module . '_' . $my_mail_token,
            'to' => $to,
            'subject' => $subject,
            'body' => array($message),
            'headers' => array(
                'From' => $from, 
                'Sender' => $from, 
                'Return-Path' => $from,
                'Bcc' => 'prashantsinalkar@gmail.com',
            ),
        );
        $system = drupal_mail_system($my_module, $my_mail_token);
        $message = $system->format($message);
        if ($system->mail($message)) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    function send_mail_attachment($from, $to, $cc, $bcc, $subject, $message, $file) {
        // $file should include path and filename
        $filename = basename($file);
        $file_size = filesize($file);
        $content = chunk_split(base64_encode(file_get_contents($file))); 
        $uid = md5(uniqid(time()));
        $from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
        $header = "From: ".$from."\r\n"
            ."Cc: ".$cc."\r\n"
            ."Bcc: ".$bcc."\r\n"
            ."MIME-Version: 1.0\r\n"
            ."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
            ."This is a multi-part message in MIME format.\r\n" 
            ."--".$uid."\r\n"
            ."Content-type: text/html; charset=UTF-8; format=flowed\r\n"
            ."Content-Transfer-Encoding: 7bit\r\n\r\n"
            .$message."\r\n\r\n"
            ."--".$uid."\r\n"
            ."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
            ."Content-Transfer-Encoding: base64\r\n"
            ."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
            .$content."\r\n\r\n"
            ."--".$uid."--"; 
        return mail($to, $subject, "", $header);
    }

    
    ?>