Odyssey
Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
CuAdminRepo Class Reference

Public Member Functions

 __construct ($dbh, $logger, String $creditUnion)
 
 Read ($creditUnion)
 
 Create ($data)
 
 Update ($identifiers, $data)
 
 Delete ($keys)
 

Public Attributes

const TABLE_NAME = 'cuadmin'
 

Protected Member Functions

 TableName ($creditUnion)
 

Private Member Functions

 BuildSetColumns ($data, $meta)
 
 BuildInsertColumns ($data, $meta)
 
 ColumnByType ($value, $type)
 

Private Attributes

 $dbh
 
 $creditUnion
 
 $logger
 

Detailed Description

Definition at line 2 of file CuAdminRepo.php.

Constructor & Destructor Documentation

◆ __construct()

CuAdminRepo::__construct (   $dbh,
  $logger,
String  $creditUnion 
)
Parameters
resource$dbhDatabase handle
object$loggerLogger to log
String$creditUnion

Definition at line 17 of file CuAdminRepo.php.

17  {
18  $this->dbh = $dbh;
19  $this->logger = $logger;
20  $this->creditUnion = $creditUnion;
21  }

Member Function Documentation

◆ BuildInsertColumns()

CuAdminRepo::BuildInsertColumns (   $data,
  $meta 
)
private

Simple helper to collect the column & value strings for inserts

Parameters
array$dataArray of columns and values
array$metaMeta information about the table structure
Returns
array

Definition at line 158 of file CuAdminRepo.php.

158  {
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  }
ColumnByType($value, $type)

◆ BuildSetColumns()

CuAdminRepo::BuildSetColumns (   $data,
  $meta 
)
private

Simple helper to collect the set values for update

Parameters
array$dataArray of columns and values
array$metaMeta information about the table structure
Returns
array

Definition at line 138 of file CuAdminRepo.php.

138  {
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  }
ColumnByType($value, $type)

◆ ColumnByType()

CuAdminRepo::ColumnByType (   $value,
  $type 
)
private

Simple helper to create a database value based on type

Parameters
string$valueColumn value
string$typeColumn type
Returns
string

Definition at line 188 of file CuAdminRepo.php.

188  {
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  }

◆ Create()

CuAdminRepo::Create (   $data)

Create

Parameters
array$dataArray of data to be inserted [ 'column_name' => 'my new value', ] An empty array or no valid columns will return a False
Returns
bool
Exceptions
Exception

Definition at line 72 of file CuAdminRepo.php.

72  {
73  }

◆ Delete()

CuAdminRepo::Delete (   $keys)

Delete feature gate

Parameters
array$keysIdentifiers
Returns
bool|int
Exceptions
Exception

Definition at line 128 of file CuAdminRepo.php.

128  {
129  }

◆ Read()

CuAdminRepo::Read (   $creditUnion)

Return cuadmin.

Returns
array|bool

Definition at line 32 of file CuAdminRepo.php.

32  {
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  }

◆ Update()

CuAdminRepo::Update (   $identifiers,
  $data 
)

Update

Parameters
array$identifiersUser identifiers
array$dataArray of data to be updated [ 'column_name' => 'my new value', ] An empty array or no valid columns will return a False
Returns
bool
Exceptions
Exception

Definition at line 86 of file CuAdminRepo.php.

86  {
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  }
BuildSetColumns($data, $meta)

Member Data Documentation

◆ TABLE_NAME

const CuAdminRepo::TABLE_NAME = 'cuadmin'

CuAdmin Repository

Definition at line 7 of file CuAdminRepo.php.


The documentation for this class was generated from the following file: