Advertisement
Guest User

general.php

a guest
Sep 21st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.09 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: rhyshah
  5. * Date: 7/27/16
  6. * Time: 6:50 PM
  7. */
  8. use Freemig\Social\Repositories\FriendRepository;
  9. use Freemig\Social\Models\Friend;
  10. use Freemig\Social\Models\FriendGroup;
  11. use Freemig\Social\Models\FriendRequest;
  12.  
  13. use Freemig\Profile\Models\User;
  14. use Freemig\Profile\Repositories\UserRepository;
  15.  
  16. if (!function_exists('getUserInfo')) {
  17. /**
  18. * Get / set the specified configuration value.
  19. *
  20. * If an array is passed as the key, we will assume you want to set an array of values.
  21. *
  22. * @param array|string $key
  23. * @param mixed $default
  24. * @return mixed
  25. */
  26. function getUserInfo($userId = null)
  27. {
  28. if (is_null($userId)) {
  29. return false;
  30. }
  31.  
  32.  
  33. $user = new \Freemig\Profile\Repositories\UserRepository(new \Freemig\Profile\Models\User());
  34. return $user->getUserById($userId);
  35. }
  36. }
  37.  
  38. if (!function_exists('thisUser')) {
  39. /**
  40. * Get / set the specified configuration value.
  41. *
  42. * If an array is passed as the key, we will assume you want to set an array of values.
  43. *
  44. * @param array|string $key
  45. * @param mixed $default
  46. * @return mixed
  47. */
  48. function thisUser()
  49. {
  50. $userInfo = [
  51. 'id' => session('userId'),
  52. 'username' => session('username'),
  53. 'avatar' => session('profileAvatar')
  54. ];
  55. return $userInfo;
  56. }
  57.  
  58. }
  59.  
  60. function getProfileCompleteness($userId = null)
  61. {
  62. if (is_null($userId)) {
  63. return false;
  64. }
  65. return 0;
  66. }
  67.  
  68.  
  69. function getTotalCredit($userId = null)
  70. {
  71. if (is_null($userId)) {
  72. return false;
  73. }
  74. return 0;
  75. }
  76.  
  77. function get_new_post_template_params($postId)
  78. {
  79.  
  80. $p = new \Freemig\Social\Repositories\PostRepository(new \Freemig\Social\Models\Post(), new \Freemig\Social\Models\Comment());
  81. $c = new \Freemig\Social\Repositories\CommentRepository(new \Freemig\Social\Models\Comment());
  82. $l = new \Freemig\Social\Repositories\LikeRepository(new \Freemig\Social\Models\Like());
  83. $s = new \Freemig\Social\Repositories\ShareRepository(new \Freemig\Social\Models\Share());
  84. $pf = new \Freemig\Profile\Repositories\UserRepository(new \Freemig\Profile\Models\User());
  85.  
  86. //$pg = new PageRepo(new page_category, new pageInfo,new freemigBlog,new freemigEvent,new pageSettings, new pageUser, new PageFriend, new pagesMediaFile());
  87.  
  88. $v = $p->getSinglePostByPostId($postId)->toArray();
  89.  
  90. $data['post'] = $p->formatPost($v);
  91. $data['post']['attributes'] = json_decode($v['attributes'], true);
  92. $data['post']['userInfo'] = $pf->getUserByIdMinimum($v['user_id']);
  93. /*if($data['post']['owner_type']=='1' && $data['post']['by_owner'] =='1') {
  94. $data['post']['userInfo'] = $pf->getUserByIdMinimum($v['user_id']);
  95. }
  96. else if($data['post']['owner_type']=='1' && $data['post']['by_owner'] =='0') {
  97. $data['post']['userInfo'] = $pf->getUserByIdMinimum($v['friends_id']);
  98. }
  99.  
  100. else if($data['post']['owner_type']=='2' && $data['post']['by_owner'] =='1') {
  101. $data['post']['userInfo'] = $pf->getUserByIdMinimum($v['user_id']);
  102. }
  103. else if($data['post']['owner_type']=='2' && $data['post']['by_owner'] =='0') {
  104. $data['post']['userInfo'] = $pg->singlePageInfoShow($v['friends_id']);
  105. }*/
  106.  
  107.  
  108. $created_at = '';
  109. $create_at_format = formatDateTime($data['post']['created_at']);
  110. if ($create_at_format['diff'] < 21) {
  111. $created_at = $create_at_format['diffForHumans'];
  112. } else {
  113. $created_at = $create_at_format['format'];
  114. }
  115. $updated_at = '';
  116. $updated_at_format = formatDateTime($data['post']['updated_at']);
  117. if ($updated_at_format['diff'] < 21) {
  118. $updated_at = $updated_at_format['diffForHumans'];
  119. } else {
  120. $updated_at = $updated_at_format['format'];
  121. }
  122. $data['post']['created_at'] = $created_at;
  123. $data['post']['updated_at'] = $updated_at;
  124.  
  125. $data['comments'] = $c->getPostComments($v['id']);
  126. $data['likes'] = $l->getPostLikes($v['id']);
  127.  
  128. $shares = $s->getPostSharesCustomized($v['id']); //dump($data['shares']);
  129. $data['shares'] = $shares[0];
  130. $data['share_more'] = ($shares[0] > 5) ? ($shares[0] - 5) : 0;
  131. $data['sharedProfiles'] = $shares[1];
  132.  
  133. return $data;
  134. }
  135.  
  136.  
  137. function get_comment_template_html($commentId)
  138. {
  139. $mComment = new \Freemig\Social\Models\Comment();
  140. $rUser = new \Freemig\Profile\Repositories\UserRepository(new \Freemig\Profile\Models\User());
  141. $rProfile = new \Freemig\Profile\Repositories\ProfileRepository(new \Freemig\Profile\Models\Profile());
  142. $contentInfo = $mComment->where(['id' => $commentId])->get()->toArray();
  143. $c = new \Freemig\Social\Repositories\CommentRepository(new \Freemig\Social\Models\Comment());
  144. $created_at = '';
  145. $create_at_format = formatDateTime($contentInfo[0]['created_at']);;
  146. if ($create_at_format['diff'] < 21) {
  147. $created_at = $create_at_format['diffForHumans'];
  148. } else {
  149. $created_at = $create_at_format['format'];
  150. }
  151.  
  152. $updated_at = '';
  153. $updated_at_format = formatDateTime($contentInfo[0]['updated_at']);
  154. if ($create_at_format['diff'] < 21) {
  155. $updated_at = $updated_at_format['diffForHumans'];
  156. } else {
  157. $updated_at = $updated_at_format['format'];
  158. }
  159.  
  160. $data['originator'] = $rUser->getUserByIdMinimum(Session::get('userId'));
  161. $data['oriProfileInfo'] = $rProfile->getProfileMinimum(Session::get('userId'));
  162. $data['content'] = $contentInfo[0];
  163. $data['content']['created_at'] = $created_at;
  164. $data['content']['updated_at'] = $updated_at;
  165. $data['content']['likeCount'] = 0;
  166. $data['content']['replyCount'] = 0;
  167. $data['content']['dislikeCount'] = 0;
  168. $data['session_user'] = session('userId');
  169.  
  170. if (count($contentInfo[0]['parent_post_id']) > 0 && !empty($contentInfo[0]['parent_post_id'])) {
  171. $html = array(
  172. 'data' => $data,
  173. 'type' => 'replyComment'
  174. );
  175. // $html = \View::make('svrs-handlebars.reply_comment_single',$data)->render();
  176. } else {
  177. $html = array(
  178. 'data' => $data,
  179. 'type' => 'comment'
  180. );
  181. // $html = \View::make('svrs-handlebars.comment_single',$data)->render();
  182. }
  183. return $html;
  184. }
  185.  
  186. function getChatWidget()
  187. {
  188. $rFriends = new \Freemig\Social\Repositories\FriendRepository(new \Freemig\Social\Models\Friend(), new \Freemig\Social\Models\FriendRequest(), new \Freemig\Social\Models\FriendGroup());
  189. $users = $rFriends->getFriendsDistinctbyGroups(\Session::get('userId'));
  190. $pf = new \Freemig\Profile\Repositories\UserRepository(new \Freemig\Profile\Models\User());
  191. $onlineUsers = [];
  192. foreach ($users as $k => $v) {
  193. $onlineUsers[] = $pf->getUserByIdMinimum($v['friend_id']);
  194. }
  195. $html = \View::make('common.gadget.onlineChat', ['users' => $onlineUsers, 'user' => $pf->getUserByIdMinimum(\Session::get('userId'))])->render();
  196. return $html;
  197. }
  198.  
  199. function formatDateTime($input_date)
  200. {
  201. $formated_date = [];
  202. $parsed_date = \Carbon\Carbon::parse($input_date);
  203. $formated_date['format'] = $parsed_date->format('d M Y H:i A');
  204. $formated_date['diff'] = $parsed_date->diffInHours(\Carbon\Carbon::now(), false);
  205. $formated_date['diffForHumans'] = $parsed_date->diffForHumans();
  206. return $formated_date;
  207. }
  208.  
  209. function formatDate($input_date)
  210. {
  211. $formated_date = [];
  212. $parsed_date = \Carbon\Carbon::parse($input_date);
  213. $formated_date['format'] = $parsed_date->format('d M, Y');
  214. $formated_date['diff'] = $parsed_date->diffInHours(\Carbon\Carbon::now(), false);
  215. $formated_date['diffForHumans'] = $parsed_date->diffForHumans();
  216. return $formated_date;
  217. }
  218.  
  219. function suggest($type)
  220. {
  221. switch ($type) {
  222. case 'user':
  223. $mUser = new \Freemig\Profile\Models\User();
  224. $users = $mUser->all()->toArray();
  225. $rv = [];
  226. foreach ($users as $user) {
  227. unset($user['password']);
  228. $pf = new \Freemig\Profile\Repositories\UserRepository(new \Freemig\Profile\Models\User());
  229.  
  230. $user['mprofile'] = $pf->getUserByIdMinimum($user['id']);
  231. $rv[] = $user;
  232.  
  233. }
  234. return $rv;
  235. break;
  236. case 'user':
  237. break;
  238.  
  239. }
  240. }
  241.  
  242. function suggest_mysql($type)
  243. {
  244. switch ($type) {
  245. case 'user':
  246. $rv = [];
  247. $friends = new \Freemig\Social\Models\Friend();
  248. $notIn = $friends->where('user_id', '=', \Session::get('userId'))->where('connection_type', 1)->get(['friend_id']);
  249. $notIn[] = \Session::get('userId');
  250. $users = \Freemig\Profile\Models\User::whereNotIn('id', $notIn)->where('is_active', '=', 1)->get()->toArray();
  251.  
  252. foreach ($users as $u) {
  253. unset($u['password']);
  254. $rv[] = $u;
  255. }
  256.  
  257. return $rv;
  258. break;
  259. case 'user':
  260. break;
  261. }
  262. }
  263.  
  264. function suggest_es($q)
  265. {
  266.  
  267. $input = $q;
  268. $searchParams['index'] = env('APP_ENV');
  269. $searchParams['type'] = $input['t'];
  270. $searchParams['size'] = 50;
  271.  
  272. if (isset($input['q'])) {
  273.  
  274. switch ($input['q']) {
  275. case 'all':
  276. $searchParams['body'] = json_decode('{"query":{"bool":{"must":[{"match_all":{}}],"must_not":[],"should":[]}},"from":0,"size":10}', true);
  277. break;
  278. default;
  279. $searchParams['body']['query']['multi_match']['query'] = $input['q'];
  280. if ($input['t'] == 'places') {
  281. $searchParams['body']['query']['multi_match']['fields'] = ['name', 'location'];
  282. } else {
  283. $searchParams['body']['query']['multi_match']['fields'] = ['username', 'first_name', 'last_name', 'email'];
  284. }
  285. break;
  286. }
  287. }
  288.  
  289. if (isset($input['e'])) {
  290. $e = explode(',', $input['e']);
  291. $searchParams['body'] = json_decode('{"query":{"bool":{"must":[],"must_not","should":[{"match_all":{}}]}},"from":0,"size":10,"sort":[],"aggs":{}}', true);
  292. $searchParams['body']['query']['bool']['must_not'][] = ['term' => ['id' => $e[0]]];
  293. //$searchParams['body']['query']['bool']['must_not'][] = ['term'=>$e[1]];
  294. }
  295. //dd($searchParams);
  296. $s = new \Freemig\Search\Facades\Search();
  297. $res = $s->find($searchParams);
  298. return $res['hits']['hits'];
  299.  
  300. }
  301.  
  302. function recommand($params)
  303. {
  304.  
  305. $input = $params;
  306. $searchParams['index'] = env('APP_ENV');
  307. $searchParams['size'] = 50;
  308.  
  309.  
  310. if (isset($input['t'])) {
  311.  
  312. switch ($input['t']) {
  313. case 'all':
  314.  
  315. break;
  316. default;
  317. $searchParams['type'] = $input['t'];
  318. break;
  319. }
  320.  
  321. }
  322. //dd($searchParams);
  323.  
  324. $searchParams['body'] = json_decode('
  325. {"query":{"bool":{"must":[{"match_all":{}}],"must_not":[],"should":[]}},"from":0,"size":10}'
  326. , true);
  327. // dd($searchParams['body']);
  328. $s = new \Freemig\Search\Facades\Search();
  329. $res = $s->find($searchParams);
  330. return $res['hits']['hits'];
  331. }
  332.  
  333. function notInYouMayKnowUser($params){
  334. $userId = session('userId');
  335. $friendsRepo = new FriendRepository(new Friend(),new FriendRequest(), new FriendGroup());
  336. $usersFriends = array_flatten($friendsRepo->getAllDobFriendsIdArray($userId));
  337. $ids = array();
  338. foreach ($usersFriends as $u){
  339. $ids[] = array(
  340. 'term' => array('id'=>$u),
  341. );
  342. }
  343. $ids = json_encode($ids,1);
  344. $input = $params;
  345. $searchParams['index'] = env('APP_ENV');
  346. $searchParams['size'] = 50;
  347.  
  348. if (isset($input['t'])) {
  349. switch ($input['t']) {
  350. case 'all':
  351. break;
  352. default;
  353. $searchParams['type'] = $input['t'];
  354. break;
  355. }
  356. }
  357. $searchParams['body'] = json_decode('{"query":{"bool":{"must":[{"match_all":{}}],"must_not":'.$ids.',"should":[]}},"from":0,"size":10}', true);
  358. $s = new \Freemig\Search\Facades\Search();
  359. $res = $s->find($searchParams);
  360. return $res['hits']['hits'];
  361. }
  362.  
  363. function getShareUsers($postId)
  364. {
  365.  
  366. }
  367.  
  368. function getFriendsForProfile($userId)
  369. {
  370. $friendsRepo = new FriendRepository(new Friend(), new FriendRequest(), new FriendGroup());
  371. $userRepo = new UserRepository(new User());
  372. $sessionUserFriends = array_flatten($friendsRepo->getSessionUserFriendsId());
  373. $profileUsersFriends = array_flatten($friendsRepo->getAllFriendsIdArray($userId));
  374. // $intersect = array_intersect($sessionUserFriends,$profileUsersFriends);
  375. $connections = [];
  376. $common_friends = [];
  377. foreach ($profileUsersFriends as $f) {
  378. $connections[] = $userRepo->getUserByIdMinimum($f);
  379. if (in_array($f, $sessionUserFriends)) {
  380. $common_friends[] = $userRepo->getUserByIdMinimum($f);
  381. }
  382. }
  383. $total_friend = count($connections);
  384. $total_common = count($common_friends);
  385. $data['connection'] = [
  386. 'friends' => ($total_friend > 8) ? array_slice($connections, 0, 8) : $connections,
  387. 'common_friends' => ($total_common > 8) ? array_slice($common_friends, 0, 8) : $common_friends,
  388. 'follower' => $friendsRepo->getFollowers($userId),
  389. 'following' => $friendsRepo->getFollowing($userId),
  390. ];
  391. $data['total_common'] = $total_common;
  392. $data['total_friend'] = $total_friend;
  393. return $data['total_friend'];
  394. }
  395.  
  396. function simpleAuth($params, $req)
  397. {
  398.  
  399.  
  400. $validator = \Validator::make($params, [
  401. 'username' => 'required',
  402. 'password' => 'required'
  403. ], trans('auth.errors.messages'));
  404. if ($validator->fails()) {
  405. if($params['value_pass_for_capcha']>2){
  406. return $this->renderResponse(['status' => '6000', 'errors' => $validator->messages()]);
  407. }else{
  408. return $this->renderResponse(['status' => '5000', 'errors' => $validator->messages()]);
  409. }
  410.  
  411. } else {
  412. $password = $params['password'];
  413. $cred = ['username' => $params['username'], 'password' => $password, 'is_active' => '1'];
  414.  
  415. $AuthProvider = new \App\Authentication\AuthProvider();
  416. $isUser = $AuthProvider->isUser($cred);
  417.  
  418. if ($isUser) {
  419. $isActiveUser = $AuthProvider->isActiveUser($cred);
  420. if (count($isActiveUser) > 0) {
  421. $userInfo = $AuthProvider->retrieveByCredentials($cred);
  422. if (count($userInfo) > 0) {
  423. if ($AuthProvider->validateCredentials($userInfo, $cred)) {
  424. $req->session()->set('isAuth', true);
  425. $req->session()->set('userId', $userInfo->id);
  426. $req->session()->set('username', $userInfo->username);
  427. $req->session()->set('userType', $userInfo->user_type);
  428. $req->session()->set('profileAvatar', $userInfo->profile_avatar);
  429. $req->session()->set('full_name', $userInfo->first_name . ' ' . $userInfo->last_name);
  430. $req->session()->set('totalFriend',getFriendsForProfile($userInfo->id));
  431.  
  432. //subscribe to push notification channel
  433. $url = env('PN_SERVER') . '?&';
  434. curlReq($url, 'GET', ['c' => $req->session()->get('username'), 'm' => 'subscribe']);
  435. return ['status' => '2000', 'data' => ['userId' => $userInfo->id, 'username' => $userInfo->username]];
  436. } else {
  437. return ['status' => '5000', 'errors' => ['global' => trans('auth.errors.invalid')]];
  438. }
  439. } else {
  440. return ['status' => '5000', 'errors' => ['global' => trans('auth.errors.invalid')]];
  441. }
  442. } else {
  443. $link = \App::getLocale() . '/signup-successful';
  444. return ['status' => '5000', 'errors' => ['notActiveUser' => trans('auth.errors.inactive'), 'link'=> $link]];
  445. }
  446. }
  447. else {
  448. return ['status' => '5000', 'errors' => ['global' => trans('auth.errors.invalid')]];
  449. }
  450. }
  451. }
  452.  
  453. function curlReq($url,$method='GET',$data=[]){
  454.  
  455. $query = http_build_query($data);
  456.  
  457. $ch = curl_init();
  458.  
  459. if($method=='GET') {
  460. curl_setopt($ch, CURLOPT_URL, $url.$query);
  461. } else {
  462. curl_setopt($ch, CURLOPT_URL, $url);
  463. }
  464. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  465.  
  466. if($method=='POST') {
  467. curl_setopt($ch,CURLOPT_POST, count($data));
  468. curl_setopt($ch,CURLOPT_POSTFIELDS, $query);
  469. }
  470.  
  471. //execute post
  472. $result = curl_exec($ch);
  473.  
  474. //close connection
  475. curl_close($ch);
  476. return $result;
  477. }
  478.  
  479. function pn($type,$originator,$content)
  480. {
  481.  
  482. // insert into redis as notification
  483. $data['pnType'] = $type;
  484. $data['originator'] = $originator;
  485. $data['content'] = $content;
  486. //Push Notification
  487. $pn = new Notification();
  488. $pn->insert(\Session::get('userId'), $data);
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement