| 1: | <?php |
| 2: | declare(strict_types=1); |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | class Quote_Module extends Module_Skeleton |
| 21: | { |
| 22: | |
| 23: | private static $CRM_SERVER_HOST = CRM_TICKET_HOST; |
| 24: | private static $CRM_SERVER_USER = CRM_TICKET_USER; |
| 25: | |
| 26: | |
| 27: | private static $CRM_SERVER_PASSWORD = CRM_TICKET_PASSWORD; |
| 28: | |
| 29: | private static $CRM_SERVER_DATABASE = CRM_TICKET_DB; |
| 30: | |
| 31: | public $exportedFunctions = array('*' => PRIVILEGE_SITE); |
| 32: | |
| 33: | private $_db; |
| 34: | private $_metaCache = array(); |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public function __construct() |
| 42: | { |
| 43: | parent::__construct(); |
| 44: | } |
| 45: | |
| 46: | public function __destruct() |
| 47: | { |
| 48: | $this->_db = null; |
| 49: | } |
| 50: | |
| 51: | public function update($quote, $rating = 5, $name = null, $site = null) |
| 52: | { |
| 53: | if ($rating < 1 || $rating > 5) { |
| 54: | return error("invalid rating `$rating'"); |
| 55: | } |
| 56: | if (!$this->has_quote()) { |
| 57: | return $this->add($quote, $rating, $name, $site); |
| 58: | } |
| 59: | $invoice = $this->billing_get_invoice(); |
| 60: | if (!$invoice) { |
| 61: | return false; |
| 62: | } |
| 63: | $quote = trim($quote); |
| 64: | if (!$quote) { |
| 65: | return error('missing quote data'); |
| 66: | } |
| 67: | $def = array( |
| 68: | 'site' => $site, |
| 69: | 'name' => $name, |
| 70: | 'quote' => Util_HTML_BBCode::clean2HTML($quote), |
| 71: | 'rating' => $rating, |
| 72: | 'invoice' => $invoice, |
| 73: | ); |
| 74: | |
| 75: | $db = $this->_connect(); |
| 76: | $stmt = $db->prepare('UPDATE quotes |
| 77: | SET quote = CONCAT(quote,"\r\n\r\nUpdate ", DATE_FORMAT(NOW(), "%M %d, %Y"), ":\r\n", :quote), |
| 78: | name = :name, site = :site, rating = :rating |
| 79: | WHERE invoice = :invoice'); |
| 80: | |
| 81: | return $stmt->execute($def); |
| 82: | |
| 83: | } |
| 84: | |
| 85: | public function has_quote() |
| 86: | { |
| 87: | return (bool)$this->get(); |
| 88: | } |
| 89: | |
| 90: | public function get() |
| 91: | { |
| 92: | $invoice = $this->billing_get_invoice(); |
| 93: | if (!$invoice) { |
| 94: | return false; |
| 95: | } |
| 96: | $db = $this->_connect(); |
| 97: | $q = "SELECT quote, name, id, rating, site, UNIX_TIMESTAMP(since) |
| 98: | FROM quotes WHERE invoice = '" . $invoice . "'"; |
| 99: | $rs = $db->query($q); |
| 100: | if ($rs->rowCount() < 1) { |
| 101: | return array(); |
| 102: | } |
| 103: | |
| 104: | return $rs->fetch(PDO::FETCH_ASSOC); |
| 105: | } |
| 106: | |
| 107: | private function _connect() |
| 108: | { |
| 109: | if ($this->_db instanceof PDO) { |
| 110: | return $this->_db; |
| 111: | } |
| 112: | Error_Reporter::suppress_php_error('PDO::.*'); |
| 113: | $db = self::$CRM_SERVER_DATABASE; |
| 114: | $host = self::$CRM_SERVER_HOST; |
| 115: | $user = self::$CRM_SERVER_USER; |
| 116: | $password = self::$CRM_SERVER_PASSWORD; |
| 117: | $dsn = 'mysql:dbname=' . $db . ';host=' . $host; |
| 118: | try { |
| 119: | $this->_db = new PDO($dsn, $user, $password); |
| 120: | } catch (PDOException $e) { |
| 121: | Error_Reporter::report('unable to connect to quote db - falling back' . $e->getMessage()); |
| 122: | $this->_db = null; |
| 123: | |
| 124: | return error('unable to connect to ticket database - use help@apisnetworks.com'); |
| 125: | } |
| 126: | |
| 127: | return $this->_db; |
| 128: | |
| 129: | } |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | public function add($quote, $rating = 5, $name = null, $site = null) |
| 138: | { |
| 139: | if ($this->auth_is_demo()) { |
| 140: | return error('cannot add testimonial for demo account'); |
| 141: | } |
| 142: | if ($rating < 1 || $rating > 5) { |
| 143: | return error("invalid rating `$rating'"); |
| 144: | } |
| 145: | $invoice = $this->billing_get_invoice(); |
| 146: | if (!$invoice) { |
| 147: | return false; |
| 148: | } |
| 149: | $quote = trim($quote); |
| 150: | if (!$quote) { |
| 151: | return error('missing quote data'); |
| 152: | } |
| 153: | $def = array( |
| 154: | 'site' => $site, |
| 155: | 'name' => $name, |
| 156: | 'since' => null, |
| 157: | 'quote' => $quote, |
| 158: | 'invoice' => $invoice, |
| 159: | 'rating' => $rating |
| 160: | ); |
| 161: | $def['since'] = $this->billing_get_customer_since(); |
| 162: | $db = $this->_connect(); |
| 163: | $def['quote'] = Util_HTML_BBCode::clean2HTML($def['quote']); |
| 164: | $stmt = $db->prepare('INSERT INTO quotes |
| 165: | (id, quote, since, name, rating, site, invoice) |
| 166: | VALUES |
| 167: | (null, |
| 168: | :quote, |
| 169: | FROM_UNIXTIME(:since), |
| 170: | :name, |
| 171: | :rating, |
| 172: | :site, |
| 173: | :invoice)'); |
| 174: | Mail::send('matt+feedback@apisnetworks.com', 'Client Testimonial - ' . $site, var_export($def, true)); |
| 175: | |
| 176: | return $stmt->execute($def); |
| 177: | |
| 178: | } |
| 179: | |
| 180: | public function get_random() |
| 181: | { |
| 182: | $db = $this->_connect(); |
| 183: | $q = 'SELECT id, name, site, |
| 184: | UNIX_TIMESTAMP(since) AS since, quote ' . |
| 185: | 'FROM quotes ORDER BY RAND() LIMIT 1'; |
| 186: | $rs = $db->query($q); |
| 187: | if ($rs->rowCount() < 1) { |
| 188: | return array(); |
| 189: | } |
| 190: | |
| 191: | return $rs->fetch(PDO::FETCH_ASSOC); |
| 192: | } |
| 193: | |
| 194: | public function get_all() |
| 195: | { |
| 196: | $db = $this->_connect(); |
| 197: | if (!$db) { |
| 198: | return false; |
| 199: | } |
| 200: | $quotes = array(); |
| 201: | $q = 'SELECT quote, name, id, rating, site, UNIX_TIMESTAMP(since) |
| 202: | FROM quotes'; |
| 203: | $rs = $db->query($q); |
| 204: | if ($rs->rowCount() < 1) { |
| 205: | return array(); |
| 206: | } |
| 207: | while (false !== ($r = $rs->fetchObject())) { |
| 208: | $quotes[] = array( |
| 209: | 'quote' => $r->quote, |
| 210: | 'name' => $r->name, |
| 211: | 'id' => $r->id, |
| 212: | 'rating' => $r->rating, |
| 213: | 'site' => $r->site |
| 214: | ); |
| 215: | } |
| 216: | |
| 217: | return $quotes; |
| 218: | } |
| 219: | |
| 220: | |
| 221: | } |