Commit | Line | Data |
---|---|---|
1008246f J |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.4 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * | |
33 | */ | |
34 | ||
35 | class CRM_Wci_BAO_WidgetCache extends CRM_Wci_DAO_WidgetCache { | |
36 | ||
37 | /** | |
38 | * class constructor | |
39 | */ | |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Function to create widget cache | |
46 | * takes an associative array | |
47 | * | |
48 | * This function is invoked from within the web form layer and also from the api layer | |
49 | * | |
50 | * @param array $params (reference ) an assoc array of name/value pairs | |
51 | * | |
52 | * @return object CRM_Wci_BAO_WidgetCache object | |
53 | * @access public | |
54 | * @static | |
55 | */ | |
56 | static function create(array $params) { | |
57 | ||
58 | // check required params | |
59 | if (!self::dataExists($params)) { | |
60 | CRM_Core_Error::fatal('Not enough data to create a progress bar formula entry.'); | |
61 | } | |
62 | ||
63 | $widget_cache = new CRM_Wci_BAO_WidgetCache(); | |
64 | $widget_cache->copyValues($params); | |
65 | ||
66 | $widget_cache->save(); | |
67 | ||
68 | return $widget_cache; | |
69 | } | |
70 | ||
71 | /** | |
72 | * Get a list of Widgets matching the params, where params keys are column | |
73 | * names of civicrm_wci_widget. | |
74 | * | |
75 | * @param array $params | |
76 | * @return array of CRM_Wci_BAO_ProgressBarFormula objects | |
77 | */ | |
78 | static function retrieve(array $params) { | |
79 | $result = array(); | |
80 | ||
81 | $widget_cache = new CRM_Wci_BAO_WidgetCache(); | |
82 | $widget_cache->copyValues($params); | |
83 | $widget_cache->find(); | |
84 | ||
85 | while ($widget_cache->fetch()) { | |
86 | $result[(int) $widget_cache->id] = clone $widget_cache; | |
87 | } | |
88 | ||
89 | $widget_cache->free(); | |
90 | ||
91 | return $result; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Wrapper method for retrieve | |
96 | * | |
97 | * @param mixed $id Int or int-like string representing widget ID | |
98 | * @return CRM_Wci_BAO_ProgressBarFormula | |
99 | */ | |
100 | static function retrieveByID($id) { | |
101 | if (!is_int($id) && !ctype_digit($id)) { | |
102 | CRM_Core_Error::fatal(__CLASS__ . '::' . __FUNCTION__ . ' expects an integer.'); | |
103 | } | |
104 | $id = (int) $id; | |
105 | ||
106 | $widget_cache = self::retrieve(array('id' => $id)); | |
107 | ||
108 | if (!array_key_exists($id, $widget_cache)) { | |
109 | CRM_Core_Error::fatal("No formula entry with ID $id exists."); | |
110 | } | |
111 | ||
112 | return $widget_cache[$id]; | |
113 | } | |
114 | ||
115 | public static function setWidgetCache($widgetId, $code) { | |
116 | $query = "DELETE from civicrm_wci_widget_cache WHERE widget_id = %1"; | |
117 | CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), | |
118 | TRUE, 'CRM_Wci_DAO_WidgetCache'); | |
119 | ||
120 | $query = "INSERT into civicrm_wci_widget_cache (widget_id, widget_code) VALUES (%1, %2)"; | |
121 | $params = array(1=>array($widgetId, 'Integer'), | |
122 | 2=>array($code, 'String'),); | |
123 | CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_WidgetCache'); | |
124 | } | |
125 | public static function getCurrentTsDiff($widgetId) { | |
126 | $tsDiff = 0; | |
127 | $query = "SELECT TIMESTAMPDIFF(MINUTE, `ts`, NOW()) as tsDiff FROM | |
128 | civicrm_wci_widget_cache where widget_id = %1"; | |
129 | $dao = CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), | |
130 | TRUE, 'CRM_Wci_DAO_WidgetCache'); | |
131 | if ($dao->fetch()) { | |
132 | $tsDiff = $dao->tsDiff; | |
133 | } else { | |
134 | $tsDiff = PHP_INT_MAX; | |
135 | } | |
136 | return $tsDiff; | |
137 | } | |
138 | ||
139 | public static function getWidgetCache($widgetId) { | |
140 | $code = ""; | |
141 | $query = "SELECT widget_code FROM civicrm_wci_widget_cache where widget_id = %1"; | |
142 | $dao = CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), | |
143 | TRUE, 'CRM_Wci_DAO_WidgetCache'); | |
144 | if ($dao->fetch()) { | |
145 | $code = $dao->widget_code; | |
146 | } | |
147 | return $code; | |
148 | } | |
149 | } |