KSocialLib.php 5.81 KB
<?php namespace App\Jobs;

use App\Jobs\KFriendLib;

use App\Models\Communitie;
use App\Models\Friend;
use App\Models\User;
use App\Models\Post;
use App\Models\Role;
use Auth;

class KSocialLib
{
  private $AllFriends=array();
  private $AllCommunities=array();
  
  public function __construct()
  {
   
  }

  public function wall($id,$wall,$filter,$lastdate,$hashtagstr,$postcnt)
  {
		$admin_post_id=array("X");
		$tusers=Role::userByModuleACL("Social",true,true,true);
		foreach($tusers as $tuser)$admin_post_id[]=$tuser->id;
		
	  $frndlist=array();
	  $foleList=array();
	  $comfList=array();
	  $posttypes=array();
	  $mycommnames=array();
	  $courseid="";$courseusrarray=array();

	  $hashtags=array();if(trim($hashtagstr)!="")$hashtags=array_unique(explode(" ",$hashtagstr));

		$posts=array();
		$frndlist_arr=array();
		
		$frnd=new KFriendLib();
		if($filter=="Only_Me") $frndlist_arr[]=$id;
		else
		{
		  if($filter=="All_Posts"){$frndlist_arr[]=$id;$frndlist_arr=array_merge($frndlist_arr,$admin_post_id);$meingroups=$frnd->meInFGroups($id);}
		  if($filter=="All_Posts" || $filter=="Friends" || $filter=="Circles" ) $frndlist=$frnd->myFrndList($id);
		  if($filter=="All_Posts" || $filter=="Following" || $filter=="Circles" ) $foleList=$frnd->myFoleList($id);
		  if($filter=="All_Posts" || $filter=="Communities") {$comfList=$frnd->myCommFrndList($id);$mycommnames=$frnd->myCommunityIDs($id);}
		  
		  if(strstr($filter,"f_")) $frndlist[]=substr($filter,2);
		  if(strstr($filter,"g_")) $frndlist=$frnd->circleFriends($filter, $id);
		  if(strstr($filter,"c_"))
		  {
				$comfList=$frnd->communityFriends(substr($filter,2));$mycommnames=array(substr($filter,2));
			}
		  if(strstr($filter,"u_")) 
		  {
		    if(in_array(substr($filter,2),$frnd->myFrndList($id)))$frndlist[]=substr($filter,2);
		    else if(in_array(substr($filter,2),$frnd->myFoleList($id)))$foleList[]=substr($filter,2);
		    else $frndlist_arr[]=substr($filter,2);
		  }
		  if(strstr($filter,"xs_"))
		  {
				$courseid=substr($filter,3);$courseusrarray=$frnd->courseUserIds($courseid);
			}
		  if(strstr($filter,"xu_"))
		  {
				$courseid=substr($filter,3);$courseusrarray=$frnd->contentUserIds($courseid);
			}
		}
		$frndlist_arr=array_unique(array_merge($frndlist_arr,$frndlist,$foleList,$comfList,$courseusrarray));
		if(!empty($frndlist_arr))
		{
			while(sizeof($posts)<$postcnt)
			{
			
				if(in_array($id,$admin_post_id))
				{
					$allposts=Post::where('fromuser','>=','0');
					if($wall!='activity')$allposts=$allposts->where("type","=",$wall);
					if(!empty($lastdate))$allposts=$allposts->where("updated_at","<",$lastdate);
					$allposts=$allposts->where('status','!=','deleted')->orderBy("updated_at","DESC")->take(10)->get();
				}
				else
				{
					$allposts=Post::whereIn('fromuser',$frndlist_arr)->where('spam','<',2);
					if($wall!='activity')$allposts=$allposts->where("type","=",$wall);
					if(!empty($lastdate))$allposts=$allposts->where("updated_at","<",$lastdate);
					$allposts=$allposts->where('status','!=','deleted')->orderBy("updated_at","DESC")->take(10)->get();
				}
				if(!$allposts||count($allposts)<1)break;
				
				foreach($allposts as $eachpost)
				{
					//match ACLs default fail
					$pass=0;
					
					$lastdate=$eachpost->updated_at;
					$eachpostdata=json_decode($eachpost->data,true);
					
					
					//positive list for passing
					if(in_array($eachpost->fromuser,$admin_post_id)&&$filter=="All_Posts")$pass=1;
					if($eachpost->fromuser==$id&&($filter=="All_Posts"||$filter=="Only_Me"))$pass=1;
					if(in_array($eachpost->fromuser,$frndlist_arr)&&strstr($eachpost->acl,",Public,")&&($filter=="All_Posts"||$filter=="u_".$eachpost->fromuser))$pass=1;
					
					if(in_array($eachpost->fromuser,$frndlist))
					{
						if(strstr($eachpost->acl,",Friends,")||strstr($eachpost->acl,",Public,")||strstr($eachpost->acl,",f_$id,"))$pass=1;
						else
						{
							if(isset($meingroups[$eachpost->fromuser]))
							{
								foreach($meingroups[$eachpost->fromuser] as $tgrp)
								{
								if(strstr($eachpost->acl,",g_$tgrp,")){$pass=1;break;}
								}
							}
						}
					}
					if(in_array($eachpost->fromuser,$foleList))
					{
						if(strstr($eachpost->acl,",Followers,")||strstr($eachpost->acl,",Public,")||strstr($eachpost->acl,",f_$id,"))$pass=1;
						else
						{
							if(isset($meingroups[$eachpost->fromuser]))
							{
								foreach($meingroups[$eachpost->fromuser] as $tgrp)
								{
								if(strstr($eachpost->acl,",g_$tgrp,")){$pass=1;break;}
								}
							}
						}
					}
					if(in_array($eachpost->fromuser,$comfList))
					{
						foreach($mycommnames as $tcomm)
						{
							if(strstr($eachpost->acl,",c_$tcomm,"))
							{
								$pass=1;
								break;
							}
						}
					}
					if(in_array($eachpost->fromuser,$courseusrarray))
					{
						if(strstr($eachpost->acl,",xs_$courseid,"))
						{
							$pass=1;
						}
					}
					if(in_array($eachpost->fromuser,$courseusrarray))
					{
						if(strstr($eachpost->acl,",xu_$courseid,"))
						{
							$pass=1;
						}
					}
					
					
					//negative checks after passing
					if($pass==1)
					{
						
						if(!empty($hashtags))
						{
							foreach($hashtags as $hashtag)
							{
								if(trim($hashtag)!='')if(!stristr($eachpost->data,trim($hashtag))){$pass=0;break;}
							}
						}
						if(isset($eachpostdata['likesdata'])&&isset($eachpostdata['likesdata'][$id]))
						{
							if($eachpostdata['likesdata'][$id]==-3)$pass=0;//my spam = -3 edupoints by me
						}
					}
					
					
					//global exception for admin id
					if(in_array($id,$admin_post_id))
					{
						if($filter=="All_Posts")$pass=1;
					}
					
					//finally add to response if passed
					if($pass==1)
					{
						$posts[$eachpost->id]=$eachpost;
					}
				}
			}
		}
		
		$data["posts"]=$posts;
		$data["id"]=$id;
		$data['lastdate']=$lastdate;
		$data['walltype']=$wall;
		
		return $data;
  }
}