File: Stop_manager_model.php

package info (click to toggle)
kalkun 0.8.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,340 kB
  • sloc: php: 30,659; javascript: 30,443; sql: 961; sh: 766; xml: 105; makefile: 41
file content (129 lines) | stat: -rw-r--r-- 3,483 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
 * Kalkun
 * An open source web based SMS Management
 *
 * @package Kalkun
 * @author  Kalkun Dev Team
 * @license https://spdx.org/licenses/GPL-2.0-or-later.html
 * @link    https://kalkun.sourceforge.io/
 */

// ------------------------------------------------------------------------

/**
 * Stop_manager_model Class
 *
 * Handle all plugin database activity
 *
 * @package     Kalkun
 * @subpackage  Plugin
 * @category    Models
 */
include_once(APPPATH.'plugins/Plugin_helper.php');
Plugin_helper::autoloader();

class Stop_manager_model extends CI_Model {

	function get($option = NULL, $limit = NULL, $offset = NULL)
	{
		switch ($option)
		{
			case 'all':
				$this->db->select('*');
				$this->db->order_by('reg_date', 'DESC');
				return $this->db->get('plugin_stop_manager');
			break;

			case 'paginate':
				$this->db->select('*');
				$this->db->order_by('reg_date', 'DESC');
				return $this->db->get('plugin_stop_manager', $limit, $offset);
			break;

			case 'search':
				$search_word = strtolower($this->input->post('search_name'));
				$this->db->select('*');
				$this->db->from('plugin_stop_manager');
				$this->db->like('LOWER('.$this->db->protect_identifiers('destination_number').')', $search_word);
				$this->db->or_like('LOWER('.$this->db->protect_identifiers('stop_type').')', $search_word);
				$this->db->or_like('LOWER('.$this->db->protect_identifiers('stop_message').')', $search_word);
				$this->db->order_by('reg_date', 'DESC');
				return $this->db->get();
			break;

			case 'count':
				$this->db->select('count(*) as count');
				return $this->db->get('plugin_stop_manager')->row('count');
			break;
		}
	}

	function get_num_for_type($type)
	{
		// Query is:
		// SELECT DISTINCT(destination_number)
		// FROM plugin_stop_manager
		// WHERE LOWER("stop_type") = LOWER('$type')"

		$this->db->distinct();
		$this->db->select('destination_number');
		if ($type !== NULL)
		{
			$this->db->where('LOWER('.$this->db->protect_identifiers('stop_type').')', strtolower(str_replace("'", "''", $type)));
		}
		return $this->db->get('plugin_stop_manager');
	}

	function add($number, $type, $msg)
	{
		$this->load->helper('kalkun');
		$number = phone_format_e164($number);

		$this->db->where('destination_number', $number);
		$this->db->where('stop_type', trim($type));
		$q = $this->db->get('plugin_stop_manager');
		$this->db->reset_query();

		if ($q->num_rows() === 1)
		{
			// do UPDATE (there is already a row)
			$data = array (
				'stop_message' => trim($msg),
				'reg_date' => date ('Y-m-d H:i:s'),
			);
			$this->db->where('destination_number', $number);
			$this->db->where('stop_type', trim($type));
			$this->db->update('plugin_stop_manager', $data);
		}
		else
		{
			if ($q->num_rows() > 1)
			{
				// do DELETE (there is more than 1 row, so remove them)
				$this->delete($number, $type);
			}

			// do INSERT (there is no record for this (number;type))
			$data = array (
				'destination_number' => $number,
				'stop_type' => trim($type),
				'stop_message' => trim($msg),
				'reg_date' => date ('Y-m-d H:i:s'),
			);
			$this->db->insert('plugin_stop_manager', $data);
		}
	}

	function delete($number, $type)
	{
		if ($type === \Kalkun\Plugins\StopManager\MsgIncoming::TYPE_NOT_SET)
		{
			$this->db->delete('plugin_stop_manager', array('destination_number' => $number));
		}
		else
		{
			$this->db->delete('plugin_stop_manager', array('destination_number' => $number, 'stop_type' => trim($type)));
		}
	}
}