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

Public Member Functions

 __construct ()
 
 VendorByCuReport ()
 
 IsDownload ()
 
 isSuccessResult ($result)
 
 set ($prop, $value)
 

Protected Member Functions

 InitDownloadFlag ()
 
 DownloadCsv ($content)
 
 GetMasterData ()
 
 GetTvClients ()
 
 GetCuLocalData ()
 
 LocalDataSql ()
 
 GetExternalTrustedData ($server, $ids)
 
 setCurlCookies ($server)
 
 MergeClientsToCuData ($server, $external_data)
 
 isRealServer ($server, $cu)
 
 CuExistsInData ($cu)
 
 VendorExistsInData ($cu, $vendor)
 
 OutputReportHeader ()
 
 CsvHeader ()
 
 HtmlHeader ()
 
 OutputReportBody ()
 
 CsvBody ()
 
 OutputCsvDataRow ($cu)
 
 setCsvCellContent ($vendor_id, $cu)
 
 HtmlBody ()
 
 OutputHtmlDataRow ($cu, $toggle)
 
 GetCuEditLink ($cu)
 
 SetServerProtocol ()
 
 SetCellAttributes ($cu, $vendor_id)
 
 OutputReportFooter ()
 

Protected Attributes

 $dbh
 
 $SYS_TYPE_UPG_TEST
 
 $SYS_TYPE_CLOSED
 
 $download = false
 
 $dev_oauth = null
 
 $cu_index = 'hcuadm/cuindex.prg'
 
 $cu_trusted_url = 'homecu.net/hcuadm/CuVendorListApi.prg'
 
 $trusted_master = 'cutrustedmaster'
 
 $cu_admin = 'cuadmin'
 
 $cu_info = 'cuinfo'
 
 $cu_data = []
 
 $trusted_vendors = []
 
 $external_servers = ['my', 'www3', 'www5', 'www6']
 

Detailed Description

Class VendorByCu Copyright 07/2019 HomeCu LLC

Outputs a basic report showing what vendors are used by which credit unions. Aid in migration tool.

Usage:

$obj = new VendorByCu(); echo $obj ->set('dbh', $dbh) ->set('SYS_TYPE_UPG_TEST', $SYS_TYPE_UPG_TEST) ->set('SYS_TYPE_CLOSED', $SYS_TYPE_CLOSED) ->VendorByCuReport();

Odyssey uses Google Oauth. TO TEST LOCALLY get your my.homecu.net auth cookie and in your code that calls this class add

->set('dev_oauth', 'your homecu_dev_oauth2_proxy cookie value')

. . . or all your my.homecu.net CU's will show no trusted vendors.

Parameters
object$dbhdatabase resource
int$SYS_TYPE_UPG_TEST
int$SYS_TYPE_CLOSED
Returns
string HTML

Definition at line 31 of file VendorByCu.i.

Constructor & Destructor Documentation

◆ __construct()

VendorByCu::__construct ( )

All we're doing in the constructor is setting the download property if it exists in $_GET.

Returns
void
Exceptions
Exception

Definition at line 77 of file VendorByCu.i.

78  {
79  $this->InitDownloadFlag();
80  }
InitDownloadFlag()
Definition: VendorByCu.i:120

Member Function Documentation

◆ CsvBody()

VendorByCu::CsvBody ( )
protected

Output the body for CSV download.

Returns
string

Definition at line 454 of file VendorByCu.i.

455  {
456  $body = null;
457 
458  foreach ($this->cu_data as $cu => $trusted_array) {
459  $body .= $this->OutputCsvDataRow($cu);
460  }
461 
462  return $body;
463  }
OutputCsvDataRow($cu)
Definition: VendorByCu.i:470

◆ CsvHeader()

VendorByCu::CsvHeader ( )
protected

Helper for OutputReportHeader(), prepare the text header for download.

Returns
string

Definition at line 395 of file VendorByCu.i.

396  {
397  $header = '"Credit Union"' . ',';
398 
399  foreach ($this->trusted_vendors as $vendor_id => $name) {
400  $header .= '"' . $vendor_id . '",';
401  }
402 
403  return rtrim($header, "\t") . PHP_EOL;
404  }

◆ CuExistsInData()

VendorByCu::CuExistsInData (   $cu)
protected

Helper for MergeClientsToCuData(). Does this CU exist in $this->cu_data? @TODO Hmm, might we need to use this to create a new node?

Parameters
string$cu
Returns
bool

Definition at line 357 of file VendorByCu.i.

358  {
359  return array_key_exists($cu, $this->cu_data);
360  }

◆ DownloadCsv()

VendorByCu::DownloadCsv (   $content)
protected

Output the data for download.

Parameters
$content
Returns
string

Definition at line 134 of file VendorByCu.i.

135  {
136  $length = strlen($content) + 1;
137  header("Content-type: bad/type");
138  header("Content-length: " . $length);
139  header("Content-Disposition: attachment; filename=\"vendor-by-cu-". date('Y-m-d') . ".csv\"");
140  return $content;
141  }

◆ GetCuEditLink()

VendorByCu::GetCuEditLink (   $cu)
protected

Set a link from the HTML display to CU info.

Parameters
string$cu
Returns
string

Definition at line 554 of file VendorByCu.i.

555  {
556  if (! (
557  isset($this->cu_data[$cu]['vendor']) &&
558  isset($this->cu_data[$cu]['server'])
559  )) {
560  return '#';
561  }
562 
563  return $this->SetServerProtocol() . "?action=fetch&rowid=$cu"
564  . "&vc={$this->cu_data[$cu]['vendor']}"
565  . "&wc={$this->cu_data[$cu]['server']}";
566  }
SetServerProtocol()
Definition: VendorByCu.i:572

◆ GetCuLocalData()

VendorByCu::GetCuLocalData ( )
protected

Get the CU data from info; we need the CU Id from cuadmin and and the trusted vendor data from cu trusted vendors. Join admin and vendors on info to get all CU's.

Returns
$this
Exceptions
Exception

Definition at line 189 of file VendorByCu.i.

190  {
191  try {
192  $res = db_query($this->LocalDataSql(), $this->dbh);
193 
194  while ($row = db_fetch_assoc($res)) {
195 
196  // Why the white space in DB values?
197  $cu = trim($row['cu']);
198 
199  if (! isset($this->cu_data[$cu]['vendor'])) {
200  $this->cu_data[$cu]['vendor'] = trim($row['vendor']);
201  $this->cu_data[$cu]['server'] = trim($row['www_server']);
202  $this->cu_data[$cu]['in_setup'] = $row['in_setup'];
203  }
204  }
205  } catch (Exception $e) {
206  throw new Exception("Exception getting CU data: {$e->getMessage()} " . db_last_error());
207  }
208 
209  return $this;
210  }
LocalDataSql()
Definition: VendorByCu.i:216

◆ GetExternalTrustedData()

VendorByCu::GetExternalTrustedData (   $server,
  $ids 
)
protected

Using the array of trusted ID's, query a server for the associated CU's.

Parameters
string$server
array$ids
Returns
array

Definition at line 240 of file VendorByCu.i.

241  {
242  $params = http_build_query(['Cu' => '', 'trustedids' => json_encode($ids)]);
243  $protocol = ($server == 'localhost')? 'http' : 'https';
244  $cmd = "$protocol://$server.{$this->cu_trusted_url}";
245 
246  $ch = curl_init($cmd);
247  curl_setopt($ch,CURLOPT_COOKIE, $this->setCurlCookies($server));
248  curl_setopt($ch,CURLOPT_USERPWD,"nobody:no1home");
249  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
250  curl_setopt($ch,CURLOPT_POST, true);
251  curl_setopt($ch,CURLOPT_POSTFIELDS, $params);
252  //curl_setopt($ch, CURLOPT_VERBOSE, true);
253 
254  $rawresp = curl_exec($ch);
255 
256  return HCU_JsonDecode($rawresp, true);
257  }
setCurlCookies($server)
Definition: VendorByCu.i:264

◆ GetMasterData()

VendorByCu::GetMasterData ( )
protected

Get the master data record. any DB query, count excepted, should always be an array.

Returns
$this
Exceptions
Exception

Definition at line 149 of file VendorByCu.i.

150  {
151  $sql = "select * from {$this->trusted_master} order by trustedvendor";
152  try {
153  $res = db_query($sql, $this->dbh);
154  } catch (Exception $e) {
155  throw new Exception("Exception getting master data: {$e->getMessage()} " . pg_last_error($this->dbh));
156  }
157 
158  while ($row = db_fetch_assoc($res)) {
159  $this->trusted_vendors[$row['trustedid']] = $row['trustedvendor'];
160  }
161 
162  return $this;
163  }

◆ GetTvClients()

VendorByCu::GetTvClients ( )
protected

Query each of $this->external_servers at the CuVendorListApi.prg URL for which T.V.'s are associated with which CU's. Set into the existing trusted vendors array.

Returns
$this

Definition at line 170 of file VendorByCu.i.

171  {
172  $trusted_arr = array_keys($this->trusted_vendors);
173 
174  foreach ($this->external_servers as $server) {
175 
176  $ext = $this->GetExternalTrustedData($server, $trusted_arr);
177  $this->MergeClientsToCuData($server, $ext);
178  }
179 
180  return $this;
181  }
MergeClientsToCuData($server, $external_data)
Definition: VendorByCu.i:290
GetExternalTrustedData($server, $ids)
Definition: VendorByCu.i:240

◆ HtmlBody()

VendorByCu::HtmlBody ( )
protected

Helper for OutputReportBody(), return the body for HTML output.

Returns
string

Definition at line 508 of file VendorByCu.i.

509  {
510  $body = null;
511  $toggle = 'odd';
512 
513  foreach ($this->cu_data as $cu => $vendors_array) {
514  $toggle = ($toggle == 'even')? 'odd' : 'even';
515 
516  $body .= $this->OutputHtmlDataRow($cu, $toggle);
517  }
518 
519  return $body;
520  }
OutputHtmlDataRow($cu, $toggle)
Definition: VendorByCu.i:528

◆ HtmlHeader()

VendorByCu::HtmlHeader ( )
protected

Helper for OutputReportHeader(), prepare the HTML output header.

Returns
string

Definition at line 410 of file VendorByCu.i.

411  {
412  $id = 1;
413 
414  // looks bad if we print "Credit Union" in first col, no reason to overstate the obvious.
415  $header = "
416  <p style=\"text-align:center\"><strong><em>If the CU does not have trusted vendors listed,
417  there are no trusted vendors set up in their Trusted Details.</em></strong></p>
418  <table id=\"report-by-vendor-table\">
419  <tr>
420  <th id=\"tv-hd-$id\" class=\"cu-name\">&nbsp;</th>
421  ";
422 
423  foreach ($this->trusted_vendors as $vendor_id => $name) {
424 
425  $id++;
426  $title = (! empty($name))? $name : str_replace('_', ' ', $vendor_id);
427 
428  $header .= "
429  <th id=\"tv-hd-$id\" class=\"rotate\" title=\"$title\"><div><span>"
430  . str_replace('_', ' ', $vendor_id) . "</span></div></th>";
431  }
432 
433  $header .= '
434  </tr>
435  ';
436 
437  return $header;
438  }

◆ InitDownloadFlag()

VendorByCu::InitDownloadFlag ( )
protected

If they've clicked download, set the flag.

Returns
$this

Definition at line 120 of file VendorByCu.i.

121  {
122  if (isset($_GET['download'])) {
123  $this->download = true;
124  }
125 
126  return $this;
127  }

◆ IsDownload()

VendorByCu::IsDownload ( )

Because of this tightly coupled system that mixes logic and output, we need a pubic method to avoid HTML being printed if we are trying to download.

Returns
bool

Definition at line 111 of file VendorByCu.i.

112  {
113  return $this->download === true;
114  }

◆ isRealServer()

VendorByCu::isRealServer (   $server,
  $cu 
)
protected

When querying external servers, there may be old implementations of the current CU. The local record will always tell us what the real server is. Use to ignore others.

Parameters
string$server
string$cu
Returns
bool

Definition at line 323 of file VendorByCu.i.

324  {
325  if ($server == 'my') {
326  $server .= '.homecu.net';
327  }
328 
329  if (isset($this->cu_data[$cu]['server'])) {
330  return $this->cu_data[$cu]['server'] == $server;
331  }
332 
333  return false;
334  }

◆ isSuccessResult()

VendorByCu::isSuccessResult (   $result)

Helper for MergeClientsToCuData(), make sure the array members we need are all present.

Parameters
array$result
Returns
bool

Definition at line 341 of file VendorByCu.i.

342  {
343  return
344  array_key_exists('status', $result) &&
345  array_key_exists('Response', $result['status']) &&
346  ($result['status']['Response'] == true) &&
347  array_key_exists('data', $result) &&
348  is_array($result['data']);
349  }

◆ LocalDataSql()

VendorByCu::LocalDataSql ( )
protected

Helper for GetCuLocalData().

Returns
string

Definition at line 216 of file VendorByCu.i.

217  {
218  return "select"
219  ." coalesce ("
220  . " {$this->cu_admin}.cu,"
221  . " upper({$this->cu_info}.user_name),"
222  . " 'none'"
223  . " ) as cu,"
224  . "((coalesce({$this->cu_info}.system_options, 0) & {$this->SYS_TYPE_UPG_TEST}) <> 0)::int as in_setup,"
225  . " {$this->cu_info}.vendor as vendor,"
226  . " {$this->cu_info}.www_server as www_server"
227  . " from {$this->cu_info}"
228  . " left join {$this->cu_admin}"
229  . " on upper({$this->cu_admin}.user_name) = upper({$this->cu_info}.user_name)"
230  . " where {$this->cu_info}.system_options & {$this->SYS_TYPE_CLOSED} = 0"
231  . " order by {$this->cu_info}.user_name asc";
232  }

◆ MergeClientsToCuData()

VendorByCu::MergeClientsToCuData (   $server,
  $external_data 
)
protected

What it says, merge external server results into $this->cu_data.

Parameters
string$server
array$external_data
Returns
$this

Definition at line 290 of file VendorByCu.i.

291  {
292  if (! $this->isSuccessResult($external_data)) {
293  return $this;
294  }
295 
296  foreach ($external_data['data'] as $key => $vendor_arr) {
297 
298  // Funky key value, but it allows us to not have to walk the array. See comments.
299  list ($cu, $vendor) = explode('|', $key);
300 
301  if (
302  ! $this->isRealServer($server, $cu) ||
303  ! $this->CuExistsInData($cu) ||
304  $this->VendorExistsInData($cu, strtoupper($vendor))
305  ) {
306  continue;
307  }
308 
309  // Store as all upper and then do strtoupper on output so we always have a match.
310  $this->cu_data[$cu]['trustedid'][] = strtoupper($vendor);
311  }
312 
313  return $this;
314  }
isSuccessResult($result)
Definition: VendorByCu.i:341
isRealServer($server, $cu)
Definition: VendorByCu.i:323
VendorExistsInData($cu, $vendor)
Definition: VendorByCu.i:370
CuExistsInData($cu)
Definition: VendorByCu.i:357

◆ OutputCsvDataRow()

VendorByCu::OutputCsvDataRow (   $cu)
protected

Output a row of data for download.

Parameters
string$cu
Returns
string|null

Definition at line 470 of file VendorByCu.i.

471  {
472  $row = '"' . $cu . '",';
473 
474  foreach ($this->trusted_vendors as $vendor_id => $name) {
475 
476  $used = $this->setCsvCellContent($vendor_id, $cu);
477  $row .= '"' . $used . '",';
478  }
479 
480  $row = rtrim($row, ',') . PHP_EOL;
481 
482  return $row;
483  }
setCsvCellContent($vendor_id, $cu)
Definition: VendorByCu.i:491

◆ OutputHtmlDataRow()

VendorByCu::OutputHtmlDataRow (   $cu,
  $toggle 
)
protected

Output a row of data from tr to /tr.

Parameters
string$cu
string$toggleeven|odd
Returns
string|null

Definition at line 528 of file VendorByCu.i.

529  {
530  $link = $this->GetCuEditLink($cu);
531 
532  $row = "
533  <tr class=\"$toggle\">
534  <td><a href=\"$link\">$cu</a></td>";
535 
536  foreach ($this->trusted_vendors as $vendor_id => $name) {
537 
538  list ($class, $txt, $usage) = $this->SetCellAttributes($cu, $vendor_id);
539  $row .= "<td class=\"$class\" title=\"$vendor_id $usage by $cu\">$txt</td>";
540  }
541 
542  $row .= '
543  </tr>
544  ';
545 
546  return $row;
547  }
SetCellAttributes($cu, $vendor_id)
Definition: VendorByCu.i:590
GetCuEditLink($cu)
Definition: VendorByCu.i:554

◆ OutputReportBody()

VendorByCu::OutputReportBody ( )
protected

Output the body HTML.

Exceptions
Exception
Returns
string

Definition at line 445 of file VendorByCu.i.

446  {
447  return ($this->IsDownload())? $this->CsvBody() : $this->HtmlBody();
448  }

◆ OutputReportFooter()

VendorByCu::OutputReportFooter ( )
protected

Simple single responsibility or, do we want footer data and row here?

Returns
string|null;

Definition at line 608 of file VendorByCu.i.

609  {
610 
611  if ($this->IsDownload()) {
612  return null;
613  }
614 
615  $count = count($this->trusted_vendors) + 1;
616 
617  return '
618  <tr>
619  <td id="vendor-by-cu-footer" colspan="' . $count . '">
620  <a href="?download" target="_blank">Download</a>
621  </td>
622  </tr>
623  </table>
624  ';
625  }

◆ OutputReportHeader()

VendorByCu::OutputReportHeader ( )
protected

Walk the trusted vendors and output the report head.

Returns
string

Definition at line 386 of file VendorByCu.i.

387  {
388  return ($this->IsDownload())? $this->CsvHeader() : $this->HtmlHeader();
389  }

◆ set()

VendorByCu::set (   $prop,
  $value 
)

Typical setter

Parameters
string$prop
mixed$value
Returns
$this

Definition at line 633 of file VendorByCu.i.

634  {
635  $this->{$prop} = $value;
636  return $this;
637  }

◆ SetCellAttributes()

VendorByCu::SetCellAttributes (   $cu,
  $vendor_id 
)
protected

Helper for outputDataRow.

Parameters
string$cu
string$vendor_id
Returns
array

Definition at line 590 of file VendorByCu.i.

591  {
592  if (
593  isset($this->cu_data[$cu]['trustedid']) &&
594  is_array($this->cu_data[$cu]['trustedid']) &&
595  in_array(strtoupper($vendor_id), $this->cu_data[$cu]['trustedid'])
596  ) {
597  return ['used', '&#10004;', 'used'];
598  }
599 
600  return ['not-used', 'x', 'not used'];
601 
602  }

◆ setCsvCellContent()

VendorByCu::setCsvCellContent (   $vendor_id,
  $cu 
)
protected

Helper for OutputCsvDataRow(), make the cell content.

Parameters
string$vendor_id
string$cu
Returns
string YES|-

Definition at line 491 of file VendorByCu.i.

492  {
493  if (
494  isset($this->cu_data[$cu]['trustedid']) &&
495  is_array($this->cu_data[$cu]['trustedid']) &&
496  in_array(strtoupper($vendor_id), $this->cu_data[$cu]['trustedid'])
497  ) {
498  return 'YES';
499  }
500 
501  return '-';
502  }

◆ setCurlCookies()

VendorByCu::setCurlCookies (   $server)
protected

Allows a dev cookie to test locally via the setter, see comments at head.

Parameters
string$server
Returns
string

Definition at line 264 of file VendorByCu.i.

265  {
266  $cookies = "HCUTicket={$_COOKIE['HCUTicket']};";
267 
268  if ($server == 'my') {
269 
270  if ($this->dev_oauth) {
271  $cookies .= ";homecu_dev_oauth2_proxy={$this->dev_oauth}";
272  return $cookies;
273  }
274 
275  if (isset($_COOKIE['homecu_dev_oauth2_proxy'])) {
276  $cookies .= ";homecu_dev_oauth2_proxy={$_COOKIE['homecu_dev_oauth2_proxy']}";
277  return $cookies;
278  }
279  }
280 
281  return $cookies;
282  }

◆ SetServerProtocol()

VendorByCu::SetServerProtocol ( )
protected

Going to try this without HB_ENV. If it causes problems, we'll inject it.

Returns
string

Definition at line 572 of file VendorByCu.i.

573  {
574  $protocol = (
575  ($_SERVER['REQUEST_SCHEME'] != 'https') &&
576  (getenv('HTTP_X_FORWARDED_PROTO') != 'https')
577  ) ?
578  'http' : 'https';
579 
580  return $protocol . '://' . strtolower($_SERVER['HTTP_HOST']) . '/' . $this->cu_index;
581 
582  }

◆ VendorByCuReport()

VendorByCu::VendorByCuReport ( )

Entry point.

Exceptions
Exception
Returns
string (HTML) or csv (download)

Definition at line 87 of file VendorByCu.i.

88  {
89  $this
90  ->GetMasterData()
91  ->GetCuLocalData()
92  ->GetTvClients();
93 
94  $content =
95  $this->OutputReportHeader() .
96  $this->OutputReportBody() .
97  $this->OutputReportFooter();
98 
99  if ($this->IsDownload()) {
100  return $this->DownloadCsv($content);
101  }
102 
103  return $content;
104  }
OutputReportFooter()
Definition: VendorByCu.i:608
OutputReportBody()
Definition: VendorByCu.i:445
DownloadCsv($content)
Definition: VendorByCu.i:134
OutputReportHeader()
Definition: VendorByCu.i:386

◆ VendorExistsInData()

VendorByCu::VendorExistsInData (   $cu,
  $vendor 
)
protected

Helper for MergeClientsToCuData(). Vendor already in the cu_data array? The external query may return duplicates, and we've already ensured the cu_data trustedid member is an array.

Parameters
string$cu
string$vendor
Returns
bool

Definition at line 370 of file VendorByCu.i.

371  {
372  if (
373  isset($this->cu_data[$cu]['trustedid']) &&
374  is_array($this->cu_data[$cu]['trustedid'])
375  ) {
376  return in_array($vendor, $this->cu_data[$cu]['trustedid']);
377  }
378 
379  return false;
380  }

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