44
55use Illuminate \Http \Request ;
66use Exception ;
7+ use Illuminate \Support \Facades \DB ;
8+ use Illuminate \Support \Facades \Route ;
79use Lcw \Activitylog \Models \ActivityLog as ModelsActivityLog ;
810use Lcw \Activitylog \Traits \ActivityLogActions ;
911
1416 *
1517 * Default Route is Your-Domain/log
1618 *
19+ * @param ignore_route [If set system ignore to create the activity on that route]
1720 * @param log [string]
1821 * @param server_ip [The server ip address]
1922 * @param user_ip [The client/user ip address]
@@ -44,23 +47,22 @@ public function get(Request $request)
4447
4548 // Delete all old Records
4649 self ::logDelete ();
47-
48- $ condition = '1 ' ;
50+ $ activityLog = new ModelsActivityLog ();
4951 if (!empty ($ request ->from_created_at )) {
50- $ condition .= ' AND DATE(created_at) >= " ' . $ request ->from_created_at . ' " ' ;
52+ $ activityLog = $ activityLog -> where ( DB :: raw ( ' DATE(created_at) ' ), ' >= ' , $ request ->from_created_at ) ;
5153 }
5254 if (!empty ($ request ->to_created_at )) {
53- $ condition .= ' AND DATE(created_at) <= " ' . $ request ->to_created_at . ' " ' ;
55+ $ activityLog = $ activityLog -> where ( DB :: raw ( ' DATE(created_at) ' ), ' <= ' , $ request ->to_created_at ) ;
5456 }
5557 if (!empty ($ request ->log )) {
56- $ condition .= ' AND log LIKE " % ' . $ request ->log . '%" ' ;
58+ $ activityLog = $ activityLog -> where ( ' log ' , ' LIKE ' , ' % ' . $ request ->log . '% ' ) ;
5759 }
5860 if (!empty ($ request ->user_id )) {
59- $ condition .= ' AND user_id = " ' . $ request ->user_id . ' " ' ;
61+ $ activityLog = $ activityLog -> where ( ' user_id ' , $ request ->user_id ) ;
6062 }
6163
62- $ activityLog = new ModelsActivityLog ();
63- return $ activityLog ->whereRaw ( $ condition )-> orderBy ('id ' , 'DESC ' )->paginate ($ limit );
64+
65+ return $ activityLog ->orderBy ('id ' , 'DESC ' )->paginate ($ limit );
6466 } catch (Exception $ e ) {
6567 return '[Get Method] Fetch data not working: ' . $ e ->getMessage ();
6668 }
@@ -79,21 +81,22 @@ public function getUsers(array $parameters = [])
7981 $ aData = [];
8082 foreach ($ allUsers as $ key => $ value ) {
8183 $ user = json_decode ($ value ['user ' ], true );
82- if (array_key_exists ('username ' , $ user )) {
83- $ aData [$ user ['id ' ]] = $ user ['username ' ];
84- } elseif (array_key_exists ('name ' , $ user )) {
85- $ aData [$ user ['id ' ]] = $ user ['name ' ];
86- } elseif (array_key_exists ('firstname ' , $ user )) {
87- $ aData [$ user ['id ' ]] = $ user ['firstname ' ];
88- } elseif (array_key_exists ('lastname ' , $ user )) {
89- $ aData [$ user ['id ' ]] = $ user ['lastname ' ];
90- } elseif (array_key_exists ('first_name ' , $ user )) {
91- $ aData [$ user ['id ' ]] = $ user ['first_name ' ];
92- } elseif (array_key_exists ('last_name ' , $ user )) {
93- $ aData [$ user ['id ' ]] = $ user ['last_name ' ];
94- } else {
95- $ aData [$ user ['id ' ]] = $ user ['id ' ];
96- }
84+ if (!empty ($ user ))
85+ if (array_key_exists ('username ' , $ user )) {
86+ $ aData [$ user ['id ' ]] = $ user ['username ' ];
87+ } elseif (array_key_exists ('name ' , $ user )) {
88+ $ aData [$ user ['id ' ]] = $ user ['name ' ];
89+ } elseif (array_key_exists ('firstname ' , $ user )) {
90+ $ aData [$ user ['id ' ]] = $ user ['firstname ' ];
91+ } elseif (array_key_exists ('lastname ' , $ user )) {
92+ $ aData [$ user ['id ' ]] = $ user ['lastname ' ];
93+ } elseif (array_key_exists ('first_name ' , $ user )) {
94+ $ aData [$ user ['id ' ]] = $ user ['first_name ' ];
95+ } elseif (array_key_exists ('last_name ' , $ user )) {
96+ $ aData [$ user ['id ' ]] = $ user ['last_name ' ];
97+ } else {
98+ $ aData [$ user ['id ' ]] = $ user ['id ' ];
99+ }
97100 }
98101 asort ($ aData );
99102 return array_unique ($ aData );
@@ -150,11 +153,25 @@ public function defaultData()
150153
151154 /**
152155 * Save the log record in table
156+ * If ignore route set system will not create any activity on that route
153157 * @param parameters array
154158 * @return resources with created log
155159 */
156160 public function create (array $ paramerts = [])
157161 {
162+
163+ $ configSettings = $ this ->getConfigSettings ();
164+ if (isset ($ configSettings ['ignore_routes ' ]) && !empty ($ configSettings ['ignore_routes ' ])) {
165+ $ routeName = Route::currentRouteName ();
166+ $ routePath = Route::getFacadeRoot ()->current ()->uri ();
167+ if (in_array ($ routeName , $ configSettings ['ignore_routes ' ])) {
168+ return true ;
169+ }
170+ if (in_array ($ routePath , $ configSettings ['ignore_routes ' ])) {
171+ return true ;
172+ }
173+ }
174+
158175 $ activityLog = new ModelsActivityLog ();
159176 $ paramerts = array_filter ($ paramerts );
160177 $ defaultParams = self ::defaultData ();
@@ -172,22 +189,23 @@ public function create(array $paramerts = [])
172189
173190 /**
174191 * Delete older data
175- * Set in .env [ACTIVITY_LOG_DEL = 3] to change the default delete limit
192+ * Set [delete_limit] in config file.
176193 * @param pass any number in months
177194 * @return bool
178195 */
179196 public function logDelete (int $ month = 3 )
180197 {
181198 try {
182- if (getenv ("ACTIVITY_LOG_DEL " )) {
183- $ month = env ("ACTIVITY_LOG_DEL " );
199+ if (!isset ($ month )) {
200+ $ configSettings = $ this ->getConfigSettings ();
201+ $ month = $ configSettings ['delete_limit ' ];
184202 }
185203 $ oldDate = date ('Y-m-d ' , strtotime ('- ' . $ month . ' months ' ));
186204
187205 $ activityLog = new ModelsActivityLog ();
188- return $ activityLog ->where (' created_at ' , '< ' , $ oldDate )->delete ();
206+ return $ activityLog ->where (DB :: raw ( ' DATE( created_at) ' ) , '< ' , $ oldDate )->delete ();
189207 } catch (Exception $ e ) {
190- return '[Get Method] Fetch data not working: ' . $ e ->getMessage ();
208+ return '[Delete Method] log delete not working: ' . $ e ->getMessage ();
191209 }
192210 }
193- }
211+ }
0 commit comments