-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
133 lines (109 loc) · 3.94 KB
/
controller.php
File metadata and controls
133 lines (109 loc) · 3.94 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
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/lib/handler.php';
function p($content){
echo '<pre>';
var_dump($content);
}
/**
* Adds some common functionality to all of the controllers
*/
class BaseHandler extends Handler{
protected $_app_config = [];
public function __construct(){
// appConfig is a simple function that returns an array
// it is defined in the config.php file
$this->_app_config = appConfig();
$this->prepare();
}
// method that is called when the handler is initialized
public function prepare(){}
public function page($content, $title = false, array $args = array(), $status_code=200){
$args['content'] = $content;
$args['title'] = $title;
$content = $this->load('base.html', $args);
return $this->write($content, 'text/html', $status_code);
}
public function getConfig($setting){
if(isset($this->_app_config, $setting)){
return $this->_app_config[$setting];
}
return null;
}
public function ensureSettingOr500($setting){
if(!isset($this->_app_config[$setting]) || !$this->_app_config[$setting]){
$msg = sprintf('%s does not exist in the $app_config', $setting);
throw new Exception($msg);
}
}
public function postToSlack(array $data = array()){
$headers = [
'Content-type' => 'application/json',
];
$post = [
'text' => 'New Member Request',
'attachments' => [
[
'pretext' => 'Someone new is looking to join DATCODE!',
'title' => $data['first_name'] .' '. $data['last_name'],
'fields' => [
[
'title' => 'Email Address',
'value' => $data['email'],
'short' => false
],
[
'title' => 'Socail Media Name',
'value' => $data['social'],
'short' => false
],
[
'title' => 'How They Know DATCODE',
'value' => $data['hear'],
'short' => false
],
[
'title' => 'More about '. $data['first_name'],
'value' => $data['about'],
'short' => false
],
]
]
]
];
$url = sprintf('https://hooks.slack.com/services/%s', $this->getConfig('slack_signup_webhook'));
$post = json_encode($post);
$this->callAPI('POST', $url, $post, $headers);
}
function callAPI($method, $url, $data = false, array $headers = []){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data){
if(is_array($data)){
$data = http_build_query($data);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data){
$url = sprintf("%s?%s", $url, http_build_query($data));
}
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return [
'result' => $result,
'status_code' => $http_status,
];
}
}