Odyssey
CuAdminRepo.php
1 <?php
2 class CuAdminRepo {
3  /**
4  * CuAdmin Repository
5  */
6 
7  const TABLE_NAME = 'cuadmin';
8  private $dbh;
9  private $creditUnion;
10  private $logger;
11 
12  /**
13  * @param resource $dbh Database handle
14  * @param object $logger Logger to log
15  * @param String $creditUnion
16  */
17  public function __construct($dbh, $logger, String $creditUnion) {
18  $this->dbh = $dbh;
19  $this->logger = $logger;
20  $this->creditUnion = $creditUnion;
21  }
22 
23  protected function TableName($creditUnion) {
24  return sprintf(static::TABLE_NAME, $creditUnion);
25  }
26 
27  /**
28  * Return cuadmin.
29  *
30  * @return array|bool
31  */
32  public function Read($creditUnion) {
33 
34  $query = "SELECT COALESCE(flagset, 0) as flagset,
35  COALESCE(flagset2, 0) as flagset2,
36  COALESCE(flagset3, 0) as flagset3,
37  livewait,
38  TRIM(lastupdate) as lastupdate,
39  min_chlng_qst,
40  pname,
41  COALESCE(histdays, 0) as fhdays,
42  COALESCE(gracelimit, 0) as grace,
43  trmemomaxlen,
44  TRIM(cu) as cu
45  FROM %s
46  WHERE LOWER(cu) = '". strtolower(prep_save($creditUnion)) ."'";
47 
48  $query = sprintf($query, static::TABLE_NAME);
49  $sth = db_query($query, $this->dbh);
50  $rows = db_fetch_all($sth);
51 
52  if (!$rows) {
53  throw new Exception('CuAdmin: Problem getting credit union settings!');;
54  }
55  // $admininfo = $rows[0];
56  // $this->admininfo = $admininfo;
57  return $rows[0];
58  }
59 
60  /**
61  * Create
62  *
63  * @param array $data Array of data to be inserted
64  * [
65  * 'column_name' => 'my new value',
66  * ]
67  * An empty array or no valid columns will return a False
68  * @return bool
69  *
70  * @throws Exception
71  */
72  public function Create($data) {
73  }
74  /**
75  * Update
76  *
77  * @param array $identifiers User identifiers
78  * @param array $data Array of data to be updated
79  * [
80  * 'column_name' => 'my new value',
81  * ]
82  * An empty array or no valid columns will return a False
83  * @return bool
84  * @throws Exception
85  */
86  public function Update($identifiers, $data) {
87  if (empty($identifiers)) {
88  throw new Exception(static::class . ': Did not pass an identifier to update');
89  }
90  if (empty($data)) {
91  throw new Exception(static::class . ': Did not pass any valid columns to update in table ' . $this->TableName($this->creditUnion));
92  }
93  $meta = db_meta_data($this->dbh, $this->TableName($this->creditUnion));
94  list($set, $values) = $this->BuildSetColumns($data, $meta);
95  if (empty($set)) {
96  throw new Exception(static::class . ': Did not pass any valid columns to update in table ' . $this->TableName($this->creditUnion) );
97  }
98  $key = '';
99  $index = count($values) + 1;
100  foreach ($identifiers as $value) {
101  $key .= "\$$index,";
102  $index++;
103  }
104  $key = rtrim($key, ',');
105  $sql = <<< SQL
106  UPDATE %s
107  $set
108  WHERE cu IN ($key);
109 SQL;
110  $sql = sprintf($sql, $this->TableName($this->creditUnion) );
111  $query = db_query_params($sql, array_merge($values, $identifiers), $this->dbh);
112  if (!$query) {
113  $error = db_last_error();
114  $this->logger->error('Failed updating identifier [' . implode(',', $identifiers) . ": $error");
115  return false;
116  }
117  return true;
118  }
119 
120  /**
121  * Delete feature gate
122  *
123  * @param array $keys Identifiers
124  * @return bool|int
125  *
126  * @throws Exception
127  */
128  public function Delete($keys) {
129  }
130 
131  /**
132  * Simple helper to collect the set values for update
133  *
134  * @param array $data Array of columns and values
135  * @param array $meta Meta information about the table structure
136  * @return array
137  */
138  private function BuildSetColumns($data, $meta) {
139  $result = 'SET ';
140  $values = [];
141  $index = 1;
142  foreach ($data as $column => $value) {
143  $type = $meta[$column]['type'];
144  $result .= "$column = \$$index,\n";
145  $index++;
146  $values[] = $this->ColumnByType($value, $type);
147  }
148  return [rtrim($result, ",\n"), $values];
149  }
150 
151  /**
152  * Simple helper to collect the column & value strings for inserts
153  *
154  * @param array $data Array of columns and values
155  * @param array $meta Meta information about the table structure
156  * @return array
157  */
158  private function BuildInsertColumns($data, $meta) {
159  $columns = [];
160  $values = [];
161  $place = [];
162  $count = 1;
163  foreach ($data as $column => $value) {
164  if (!array_key_exists($column, $meta)) {
165  $this->logger->debug("Tried to insert invalid column: $column, into table " . static::TABLE_NAME);
166  continue;
167  }
168  $type = $meta[$column]['type'];
169  $columns[] = $column;
170  $place[] = "\$$count";
171  $values[] = $this->ColumnByType($value, $type);
172  $count++;
173  }
174  return [
175  implode(',', $columns),
176  implode(',', $place),
177  $values,
178  ];
179  }
180 
181  /**
182  * Simple helper to create a database value based on type
183  *
184  * @param string $value Column value
185  * @param string $type Column type
186  * @return string
187  */
188  private function ColumnByType($value, $type) {
189  switch (true) {
190  case strpos($type, 'int') !== false:
191  return intval($value);
192  break;
193  case strpos($type, 'double') !== false:
194  case 'numeric' === $type:
195  return floatval($value);
196  break;
197  case strpos($type, 'char') !== false:
198  case 'text' === $type:
199  return $value;
200  break;
201  case strpos($type, 'timestamp') !== false:
202  case 'date' === $type:
203  return $value;
204  break;
205  case strpos($type, 'bool') !== false:
206  return $value ? 'true' : 'false';
207  break;
208  default:
209  return $value;
210  }
211  }
212 }
BuildInsertColumns($data, $meta)
BuildSetColumns($data, $meta)
Create($data)
Definition: CuAdminRepo.php:72
Update($identifiers, $data)
Definition: CuAdminRepo.php:86
__construct($dbh, $logger, String $creditUnion)
Definition: CuAdminRepo.php:17
ColumnByType($value, $type)
Read($creditUnion)
Definition: CuAdminRepo.php:32
Delete($keys)
const TABLE_NAME
Definition: CuAdminRepo.php:7