fix in recurrence widget (#426)
[openemr.git] / interface / product_registration / product_registration_service.php
blob9d630ec6dbeef4b384230571dcce71fb12af3f5d
1 <?php
2 /**
3 * ProductRegistrationService
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16 * @package OpenEMR
17 * @author Matthew Vita <matthewvita48@gmail.com>
18 * @link http://www.open-emr.org
21 require_once($GLOBALS['fileroot'] . "/interface/main/exceptions/invalid_email_exception.php");
22 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/generic_product_registration_exception.php");
23 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/duplicate_registration_exception.php");
25 class ProductRegistrationService {
26 public function __construct() {}
28 public function getProductStatus() {
29 $row = sqlQuery('SELECT * FROM product_registration LIMIT 1');
31 $payload = new stdClass();
33 if (empty($row)) {
34 $payload->status = 'UNREGISTERED';
35 } else if (!(empty($row['registration_id']))) {
36 $payload->status = 'REGISTERED';
37 $payload->email = $row['email'];
38 $payload->registration_id = $row['registration_id'];
39 } else if (!(empty($row['opt_out'])) && $row['opt_out'] == true) {
40 $payload->status = 'OPT_OUT';
43 return $payload;
46 public function registerProduct($email) {
47 if (!$email || $email == 'false') {
48 $this->optOutStrategy();
49 return null;
50 } else {
51 return $this->optInStrategy($email);
55 private function optInStrategy($email) {
56 $curl = curl_init('https://reg.open-emr.org/api/registration');
57 curl_setopt($curl, CURLOPT_POST, true);
58 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('email' => $email)));
59 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
60 $responseBodyRaw = curl_exec($curl);
61 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
62 curl_close($curl);
64 switch ($responseCode) {
65 case 201:
66 $responseBodyParsed = json_decode($responseBodyRaw);
67 sqlStatement('INSERT INTO product_registration (registration_id, email, opt_out) VALUES (?, ?, ?)', array(
68 $responseBodyParsed->productId, $email, false)
71 return $responseBodyParsed->productId;
72 break;
73 case 400:
74 throw new InvalidEmailException($email . ' ' . xl("is not a valid email address"));
75 break;
76 case 409:
77 throw new DuplicateRegistrationException(xl("Already registered"));
78 break;
79 default:
80 throw new GenericProductRegistrationException(xl("Server error: try again later"));
84 // void... don't bother checking for success/failure.
85 private function optOutStrategy() {
86 sqlStatement('INSERT INTO product_registration (registration_id, email, opt_out) VALUES (?, ?, ?)',
87 array('', null, true));