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