├── LICENSE ├── README.md ├── application ├── config │ └── notifications.php └── libraries │ └── Notifications.php └── database └── Notification-tables.sql /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 pathusutariya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CI-Notification-library 2 | Codeigniter Library for Notification System between users. 3 | -------------------------------------------------------------------------------- /application/config/notifications.php: -------------------------------------------------------------------------------- 1 | ci = &get_instance(); 95 | $this->ci->load->database(); 96 | $this->config(); 97 | } 98 | 99 | public function config($new_options = []) { 100 | 101 | $this->ci->config->load('notifications', TRUE); 102 | $options = $this->ci->config->item('notifications'); 103 | $options = array_merge($options, $new_options); 104 | 105 | 106 | $this->notificationTable = $options['notification_table']; 107 | $this->notificationUserTable = $options['notification_read_tracking_table']; 108 | $this->perpage_limit = $options['per_page']; 109 | $this->target = $options['default_target']; 110 | $this->userId = $options['default_sender']; 111 | $this->comment = $options['default_comment']; 112 | $this->type = $options['default_type']; 113 | $this->token = $options['default_token']; 114 | } 115 | 116 | /** 117 | * Set UserId 118 | * @param int $userid 119 | * @return $this 120 | */ 121 | public function user($userid) { 122 | $this->userId = $userid; 123 | return $this; 124 | } 125 | 126 | /** 127 | * Set which type of Notification 128 | * @param string $type 129 | * @return $this 130 | */ 131 | public function type($type, $id = 0) { 132 | $this->type = $type; 133 | if ($id) 134 | $this->type_id = $id; 135 | return $this; 136 | } 137 | 138 | public function flush() { 139 | $this->type = ''; 140 | $this->type_id = 0; 141 | $this->token = ''; 142 | $this->comment = ''; 143 | $this->id = 0; 144 | return $this; 145 | } 146 | 147 | /** 148 | * Set comment 149 | * @param string $comment 150 | * @return $this 151 | */ 152 | public function comment($comment) { 153 | $this->comment = $comment; 154 | return $this; 155 | } 156 | 157 | /** 158 | * Set token 159 | * @param string $token 160 | * @return $this 161 | */ 162 | public function token($token) { 163 | $this->token = $token; 164 | return $this; 165 | } 166 | 167 | /** 168 | * Set Notification Id 169 | * @param int $id 170 | * @return $this 171 | */ 172 | public function id($id) { 173 | $this->id = $id; 174 | return $this; 175 | } 176 | 177 | /** 178 | * Set Notified user id or ids 179 | * @param int|array $notified_ids 180 | * @return $this 181 | */ 182 | public function notify($notified_ids = 0) { 183 | if ($notified_ids) 184 | $this->target = $notified_ids; 185 | if ($this->id) { 186 | $data = array(); 187 | if (is_array(($this->target))) { 188 | foreach ($this->target as $value) { 189 | $data[] = array( 190 | "notification_id" => $this->id, 191 | "user_id" => $value, 192 | ); 193 | } 194 | $this->ci->db->insert_batch($this->notificationUserTable, $data); 195 | } 196 | elseif (is_int($this->target)) { 197 | $data = array( 198 | "notification_id" => $this->id, 199 | "user_id" => $this->target, 200 | ); 201 | $this->ci->db->insert($this->notificationUserTable, $data); 202 | } 203 | } 204 | } 205 | 206 | /** 207 | * Set User is already viewed 208 | * @param int|array $notified_ids 209 | * @return $this 210 | */ 211 | public function unread() { 212 | $this->only_unread = 1; 213 | return $this; 214 | } 215 | 216 | /** 217 | * Get Notification data 218 | */ 219 | public function get() { 220 | $this->ci->db->from($this->notificationUserTable); 221 | $this->ci->db->join($this->notificationTable, "{$this->notificationUserTable}.notification_id = {$this->notificationTable}.id"); 222 | $this->_querybuilder(); 223 | $query = $this->ci->db->get(); 224 | return $this->_dbcleanresult($query); 225 | } 226 | 227 | /** 228 | * Add Notification to Database 229 | */ 230 | public function add() { 231 | if ($this->userId) 232 | $data['user_id'] = $this->userId; 233 | if ($this->type) 234 | $data['type'] = $this->type; 235 | if ($this->type_id) 236 | $data['type_id'] = $this->type_id; 237 | if ($this->token) 238 | $data['token'] = $this->token; 239 | if ($this->comment) 240 | $data['comment'] = $this->comment; 241 | 242 | $this->ci->db->insert($this->notificationTable, $data); 243 | $this->id = $this->ci->db->insert_id(); 244 | return $this; 245 | } 246 | 247 | /** 248 | * Update mark as read 249 | */ 250 | public function mark_as_read() { 251 | if ($this->userId) 252 | $this->ci->db->where('user_id', $this->userId); 253 | if ($this->id) 254 | $this->ci->db->where('notification_id', $this->id); 255 | 256 | $this->ci->db->update($this->notificationUserTable, array("read_at" => date("Y:m:d H:i:s", time()))); 257 | } 258 | 259 | /** 260 | * Update mark as un-read 261 | */ 262 | public function mark_as_unread() { 263 | if ($this->userId) 264 | $this->ci->db->where('user_id', $this->userId); 265 | if ($this->id) 266 | $this->ci->db->where('notification_id', $this->id); 267 | 268 | $this->ci->db->update($this->notificationUserTable, array("read_at" => NULL)); 269 | } 270 | 271 | /** 272 | * Set Pagination 273 | * @param int $offset 274 | * @param int $limit 275 | * @return $this 276 | */ 277 | public function page($offset, $limit = 0) { 278 | if ($limit) 279 | $this->perpage_limit = $limit; 280 | $this->page_offset = $offset-1; 281 | return $this; 282 | } 283 | 284 | public function error() { 285 | return $this->error; 286 | } 287 | 288 | private function _dbcleanresult($result) { 289 | if ($result->num_rows() > 1) 290 | return $result->result(); 291 | if ($result->num_rows() == 1) 292 | return $result->row(); 293 | else 294 | return false; 295 | } 296 | 297 | private function _querybuilder() { 298 | if ($this->type) 299 | $this->ci->db->where("{$this->notificationTable}.type", $this->type); 300 | if ($this->type_id) 301 | $this->ci->db->where("{$this->notificationTable}.type_id", $this->type_id); 302 | if ($this->token) 303 | $this->ci->db->where("{$this->notificationTable}.token", $this->token); 304 | if ($this->comment) 305 | $this->ci->db->where("{$this->notificationTable}.comment", $this->comment); 306 | if ($this->userId) { 307 | $this->ci->db->where("{$this->notificationUserTable}.user_id", $this->userId); 308 | } 309 | if ($this->only_unread) 310 | $this->ci->db->where("{$this->notificationUserTable}.read_at", NULL); 311 | if ($this->id) { 312 | $this->ci->db->where("{$this->notificationUserTable}.notification_id", $this->id); 313 | $this->ci->db->where("{$this->notificationUserTable}.notification_id", $this->id); 314 | } 315 | 316 | if ($this->perpage_limit) 317 | $this->ci->db->limit( $this->perpage_limit, ($this->perpage_limit * $this->page_offset)); 318 | } 319 | 320 | } 321 | 322 | ?> -------------------------------------------------------------------------------- /database/Notification-tables.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 4.7.9 3 | -- https://www.phpmyadmin.net/ 4 | -- 5 | -- Host: 127.0.0.1 6 | -- Generation Time: May 11, 2018 at 01:44 PM 7 | -- Server version: 10.1.31-MariaDB 8 | -- PHP Version: 7.1.15 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | SET AUTOCOMMIT = 0; 12 | START TRANSACTION; 13 | SET time_zone = "+00:00"; 14 | 15 | 16 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 17 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 18 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 19 | /*!40101 SET NAMES utf8mb4 */; 20 | 21 | -- 22 | -- Database: `test` 23 | -- 24 | 25 | -- -------------------------------------------------------- 26 | 27 | -- 28 | -- Table structure for table `notifications` 29 | -- 30 | 31 | CREATE TABLE `notifications` ( 32 | `id` int(11) NOT NULL, 33 | `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 34 | `user_id` int(255) NOT NULL, 35 | `type` varchar(255) NOT NULL, 36 | `type_id` int(255) NOT NULL, 37 | `token` varchar(255) NOT NULL, 38 | `comment` text 39 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 40 | 41 | -- -------------------------------------------------------- 42 | 43 | -- 44 | -- Table structure for table `user_notification` 45 | -- 46 | 47 | CREATE TABLE `user_notification` ( 48 | `notification_id` int(11) NOT NULL, 49 | `user_id` int(11) NOT NULL, 50 | `read_at` timestamp NULL DEFAULT NULL 51 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 52 | 53 | -- 54 | -- Indexes for dumped tables 55 | -- 56 | 57 | -- 58 | -- Indexes for table `notifications` 59 | -- 60 | ALTER TABLE `notifications` 61 | ADD PRIMARY KEY (`id`); 62 | 63 | -- 64 | -- AUTO_INCREMENT for dumped tables 65 | -- 66 | 67 | -- 68 | -- AUTO_INCREMENT for table `notifications` 69 | -- 70 | ALTER TABLE `notifications` 71 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; 72 | COMMIT; 73 | 74 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 75 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 76 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 77 | --------------------------------------------------------------------------------