1
+ <?php
2
+ namespace Unrulynatives \Helpers ;
3
+
4
+ use App \Models \Reminder ;
5
+ use App \Models \Userattitude ;
6
+ use App \Models \Entitystandpoint ;
7
+ use App \Models \Usersetting ;
8
+ use App \Models \Exemplar ;
9
+ use App \User ;
10
+ use DB ;
11
+ use Auth ;
12
+ use URL ;
13
+
14
+ class UNStarterSitewideHelper {
15
+
16
+
17
+
18
+
19
+ public static function getDomainName ($ url , $ length ) {
20
+ $ domain = parse_url ($ url , PHP_URL_HOST );
21
+ return $ domain ;
22
+
23
+
24
+ }
25
+
26
+ public static function creatorAvatar ($ userid ) {
27
+ $ user = User::find ($ userid );
28
+ $ hash = md5 (strtolower ($ user ->email ));
29
+ return "http://www.gravatar.com/avatar/ $ hash " ;
30
+
31
+
32
+ }
33
+
34
+ public static function creatorName ($ userid ) {
35
+ $ user = User::find ($ userid );
36
+
37
+ return $ user ->name ;
38
+ // return 'xx';
39
+
40
+ }
41
+
42
+
43
+
44
+ public static function setTopbar () {
45
+
46
+ if (Auth::check ()) {
47
+ $ chosen_topbar = Auth::user ()->settings ->option_show_topbar_type ;
48
+
49
+ } else {
50
+ $ chosen_topbar = request ()->cookie ('topbar ' ,'2 ' );
51
+ if (!$ chosen_topbar ) {
52
+ $ chosen_topbar = request ()->cookie ('topbar ' ) ? request ()->cookie ('topbar ' ) : '2 ' ;
53
+ return $ chosen_topbar ;
54
+ return response ();
55
+ // ->withCookie('topbar', $chosen_topbar, 10000);
56
+ }
57
+ }
58
+
59
+ return $ chosen_topbar ;
60
+ return response ();
61
+ // return response();
62
+
63
+ }
64
+
65
+ public static function getActiveExemplar () {
66
+
67
+ // $active_exemplar_id = request()->cookie('active_exemplar', '8');
68
+
69
+ $ active_exemplar_id = request ()->cookie ('active_exemplar ' ) ? request ()->cookie ('active_exemplar ' ) : '8 ' ;
70
+
71
+ if (!$ active_exemplar_id ) {
72
+ //create the active_exemplar cookie with value 8
73
+ $ active_exemplar_id = '8 ' ;
74
+ // $active_exemplar->withCookie('active_exemplar', '11', 2628000);
75
+ }
76
+
77
+ $ active_exemplar = Exemplar::find ($ active_exemplar_id );
78
+
79
+ return $ active_exemplar ;
80
+
81
+ }
82
+
83
+
84
+
85
+ public static function optionValue ($ option ) {
86
+
87
+ if (Auth::check ()) {
88
+ $ object = Usersetting::where ('user_id ' ,Auth::id ())->first ();
89
+ $ valname = 'option_ ' .$ option ;
90
+ $ output = $ object ->$ valname ;
91
+ return $ output ;
92
+ } else {
93
+
94
+ }
95
+
96
+
97
+ // return $output;
98
+
99
+ }
100
+
101
+ public static function orgFramework ($ themename ) {
102
+
103
+ if ($ themename == 'bootstrap ' ) {
104
+ return "bootstrap " ;
105
+ }
106
+ if ($ themename == 'semantic ' ) {
107
+ return "semantic " ;
108
+ }
109
+
110
+ return "semantic " ;
111
+
112
+
113
+ }
114
+ public static function optionSwitch ($ optionname ) {
115
+
116
+
117
+ return "<button data-load=' " . URL ::to ('option_switch/ ' .$ optionname ) . "' id='os_111' data-puthere='#os_111' title='Przełączenie opcji " .$ optionname ."'>Switch option </button> " ;
118
+
119
+ // return $output;
120
+
121
+ }
122
+
123
+ // used in /sprawdz-spojnosc-wlasnych-pogladow (percept 09.07)
124
+ // sitewideHelper::listAttitudes('41', '5');
125
+ public static function listAttitudes ($ questionid , $ user ) {
126
+
127
+
128
+ // task: Userattitude list
129
+ $ user_attitudes = Userattitude::join ('entitystandpoints ' , function ($ q ) use ($ questionid ,$ user ) {
130
+ $ q ->where ('user_attitudes.item_type ' , '= ' , 'entitystandpoint ' );
131
+ $ q ->on ('user_attitudes.item_id ' , '= ' , 'entitystandpoints.id ' );
132
+ $ q ->where ('entitystandpoints.question_id ' , '= ' , $ questionid );
133
+ $ q ->where ('user_attitudes.creator_id ' ,'= ' , $ user );
134
+ })
135
+ // ->select('questions.id AS id')
136
+ ->select ('user_attitudes.* ' )
137
+ // ->selectRaw('SUM(user_attitudes.importance) AS importranking')
138
+ // ->groupBy('questions.id')
139
+ // ->orderBy('importranking', 'desc')
140
+ ->get ();
141
+
142
+ return $ user_attitudes ;
143
+
144
+ }
145
+
146
+
147
+ public static function parseTitle ($ url ) {
148
+ $ default = '(no title) ' ;
149
+ $ req_headers = ["Accept-Language: en-US,en;q=0.8 " , "Accept-Charset: UTF-8,*;q=0.5 " ];
150
+ $ useragent = "Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4 " ;
151
+ $ timeout = 5 ;
152
+
153
+ $ ch = curl_init ($ url );
154
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
155
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
156
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , false );
157
+ curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , $ timeout );
158
+ curl_setopt ($ ch , CURLOPT_TIMEOUT , $ timeout );
159
+ curl_setopt ($ ch , CURLOPT_FRESH_CONNECT , 1 );
160
+ curl_setopt ($ ch , CURLOPT_FORBID_REUSE , 1 );
161
+ curl_setopt ($ ch , CURLOPT_AUTOREFERER , 1 );
162
+ curl_setopt ($ ch , CURLOPT_USERAGENT , $ useragent );
163
+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ req_headers );
164
+ curl_setopt ($ ch , CURLOPT_ENCODING , '' );
165
+ curl_setopt ($ ch , CURLOPT_MAXREDIRS , 3 );
166
+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
167
+ $ headers = '' ;
168
+ curl_setopt ($ ch , CURLOPT_HEADERFUNCTION , function ($ ch , $ h ) use (&$ headers ) {
169
+ $ headers .= $ h ;
170
+ return strlen ($ h );
171
+ });
172
+ $ content = curl_exec ($ ch );
173
+ $ code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
174
+ curl_close ($ ch );
175
+
176
+ if ($ content === FALSE || $ code >= 400 ) {
177
+ return $ default ;
178
+ }
179
+
180
+ preg_match ("/Content-Type:.*charset\s*=\s*(.*)[\s;]*/i " , $ headers , $ enc );
181
+ if (!isset ($ enc [1 ])) {
182
+ preg_match ('/<meta\s*http-equiv\s*=\s*"\s*content-type\s*"\s*content\s*=\s*".*;\s*charset\s*=\s*(.*?)\s*"\s*\/?>/i ' , $ content , $ enc );
183
+ }
184
+ if (!isset ($ enc [1 ])) {
185
+ preg_match ('/<meta\s*charset\s*=\s*"?\s*(.*?)\s*"?\s*\/?>/i ' , $ content , $ enc );
186
+ }
187
+ if (isset ($ enc [1 ])) {
188
+ $ pageenc = strtolower (trim ($ enc [1 ]));
189
+ if ($ pageenc !== 'utf-8 ' ) {
190
+ $ errh = set_error_handler (NULL );
191
+ // unfortunately.. is there a better way to stop laravel from throwing exceptions here when iconv has a problem?
192
+ $ content = @iconv ($ pageenc , 'utf-8 ' , $ content );
193
+ set_error_handler ($ errh );
194
+ }
195
+ }
196
+
197
+ preg_match ("/<title.*?>\s*(.*?)\s*<\/title>/is " , $ content , $ page_title );
198
+ $ page_title = isset ($ page_title [1 ]) ? $ page_title [1 ] : $ default ;
199
+ $ page_title = preg_replace_callback ("/(&#[0-9]+;)/ " , function ($ m ) {
200
+ return mb_convert_encoding ($ m [1 ], "UTF-8 " , "HTML-ENTITIES " );
201
+ }, $ page_title );
202
+ $ page_title = preg_replace ('/\s+/ ' , ' ' , $ page_title );
203
+ return trim ($ page_title );
204
+ }
205
+
206
+
207
+ public static function listStandpoints ($ questionid , $ user , $ judged ) {
208
+
209
+
210
+ // zadanie: lista Userattitude wobec stanowisk postaci wyrażonych w konkretnym pytaniu
211
+ // (Entitystandpoint where question_id = $questionid)
212
+ // $standpoints = Entitystandpoint::join('user_attitudes', function ($q) use($questionid,$user) {
213
+ // $q->where('user_attitudes.item_type', '=', 'entitystandpoint');
214
+ // $q->on('entitystandpoints.id', '=', 'user_attitudes.item_id');
215
+ // $q->where('entitystandpoints.question_id', '=', $questionid);
216
+ // $q->where('user_attitudes.creator_id','=', $user);
217
+ // $q->whereNull('user_attitudes.id');
218
+ // })
219
+ // // ->select('questions.id AS id')
220
+ // ->select('user_attitudes.*')
221
+ // // ->whereNull('user_attitudes.id')
222
+ // // ->selectRaw('SUM(user_attitudes.importance) AS importranking')
223
+ // // ->groupBy('questions.id')
224
+ // // ->orderBy('importranking', 'desc')
225
+ // ->count();
226
+
227
+
228
+ // $standpoints = Userattitude::leftJoin('entitystandpoints', function ($q) use($questionid,$user) {
229
+ // $q->where('user_attitudes.item_type', '=', 'entitystandpoint');
230
+ // $q->on('user_attitudes.item_id', '=', 'entitystandpoints.id');
231
+ // $q->where('entitystandpoints.question_id', '=', $questionid);
232
+ // $q->where('user_attitudes.creator_id','=', $user);
233
+ // })
234
+ // ->whereNull('entitystandpoints.id')
235
+ // ->select('user_attitudes.*')
236
+ // ->get();
237
+
238
+
239
+
240
+
241
+
242
+
243
+ // return $standpoints;
244
+
245
+ }
246
+
247
+
248
+
249
+
250
+ }
251
+
252
+
253
+ // if($judged=0) {
254
+ // // te standpointy, których user jeszcze nie ocenił
255
+
256
+ // }
257
+ // if($judged=1) {
258
+ // // standpointy, już ocenione
259
+ // } else {
260
+ // // wszystkie standpointy dla tego pytania
261
+ // }
0 commit comments