Merge remote-tracking branch 'upstream/4.4' into 4.4-4.5-2014-09-08-20-42-29
[civicrm-core.git] / tools / CRM / Auction / BAO / Auction.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36
37 require_once 'CRM/Auction/DAO/Auction.php';
38
39 /**
40 * Class CRM_Auction_BAO_Auction
41 */
42 class CRM_Auction_BAO_Auction extends CRM_Auction_DAO_Auction {
43
44 /**
45 * class constructor
46 */
47 function __construct() {
48 parent::__construct();
49 }
50
51 /**
52 * Takes a bunch of params that are needed to match certain criteria and
53 * retrieves the relevant objects. Typically the valid params are only
54 * contact_id. We'll tweak this function to be more full featured over a period
55 * of time. This is the inverse function of create. It also stores all the retrieved
56 * values in the default array
57 *
58 * @param array $params (reference ) an assoc array of name/value pairs
59 * @param array $defaults (reference ) an assoc array to hold the flattened values
60 *
61 * @return object CRM_Auction_BAO_Auction object
62 * @access public
63 * @static
64 */
65 static
66 function retrieve(&$params, &$defaults) {
67 $auction = new CRM_Auction_DAO_Auction();
68 $auction->copyValues($params);
69 if ($auction->find(TRUE)) {
70 CRM_Core_DAO::storeValues($auction, $defaults);
71 return $auction;
72 }
73 return NULL;
74 }
75
76 /**
77 * update the is_active flag in the db
78 *
79 * @param int $id id of the database record
80 * @param boolean $is_active value we want to set the is_active field
81 *
82 * @return Object DAO object on sucess, null otherwise
83 * @static
84 */
85 static
86 function setIsActive($id, $is_active) {
87 return CRM_Core_DAO::setFieldValue('CRM_Auction_DAO_Auction', $id, 'is_active', $is_active);
88 }
89
90 /**
91 * function to add the auction
92 *
93 * @param array $params reference array contains the values submitted by the form
94 *
95 * @access public
96 * @static
97 *
98 * @return object
99 */
100 static
101 function add(&$params) {
102 require_once 'CRM/Utils/Hook.php';
103
104 if (!empty($params['id'])) {
105 CRM_Utils_Hook::pre('edit', 'Auction', $params['id'], $params);
106 }
107 else {
108 CRM_Utils_Hook::pre('create', 'Auction', NULL, $params);
109 }
110
111 $auction = new CRM_Auction_DAO_Auction();
112
113 $auction->copyValues($params);
114 $result = $auction->save();
115
116 if (!empty($params['id'])) {
117 CRM_Utils_Hook::post('edit', 'Auction', $auction->id, $auction);
118 }
119 else {
120 CRM_Utils_Hook::post('create', 'Auction', $auction->id, $auction);
121 }
122
123 return $result;
124 }
125
126 /**
127 * function to create the auction
128 *
129 * @param array $params reference array contains the values submitted by the form
130 *
131 * @return object
132 * @access public
133 * @static
134 *
135 */
136 public static function create(&$params) {
137 require_once 'CRM/Core/Transaction.php';
138 $transaction = new CRM_Core_Transaction();
139
140 $auction = self::add($params);
141
142 if (is_a($auction, 'CRM_Core_Error')) {
143 CRM_Core_DAO::transaction('ROLLBACK');
144 return $auction;
145 }
146
147 $transaction->commit();
148
149 return $auction;
150 }
151
152 /**
153 * Function to delete the auction
154 *
155 * @param int $id auction id
156 *
157 * @access public
158 * @static
159 *
160 */
161 static
162 function del($id) {
163 require_once 'CRM/Auction/DAO/Auction.php';
164 $auction = new CRM_Auction_DAO_Auction();
165 $auction->id = $id;
166 $result = $auction->delete();
167 return $result;
168 }
169
170 /**
171 * Function to get current/future Auctions
172 *
173 * @param $all boolean true if auctions all are required else returns current and future auctions
174 *
175 * @param bool $id
176 *
177 * @return array
178 * @static
179 */
180 static
181 function getAuctions($all = FALSE, $id = FALSE) {
182 $query = "SELECT `id`, `title`, `start_date` FROM `civicrm_auction`";
183
184 if (!$all) {
185 $endDate = date('YmdHis');
186 $query .= " WHERE `end_date` >= {$endDate} OR end_date IS NULL";
187 }
188 if ($id) {
189 $query .= " WHERE `id` = {$id}";
190 }
191
192 $query .= " ORDER BY title asc";
193 $auctions = array();
194
195 $dao = &CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
196 while ($dao->fetch()) {
197 $auctions[$dao->id] = $dao->title . ' - ' . CRM_Utils_Date::customFormat($dao->start_date);
198 }
199
200 return $auctions;
201 }
202 }
203