Merge pull request #3317 from eileenmcnaughton/CRM-14197-postprocesfn
[civicrm-core.git] / tools / CRM / Auction / BAO / Item.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_Item
41 */
42 class CRM_Auction_BAO_Item 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_Item object
62 * @access public
63 * @static
64 */
65 static
66 function retrieve(&$params, &$defaults) {
67 $auction = new CRM_Auction_DAO_Item();
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_Item', $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_Item', $params['id'], $params);
106 }
107 else {
108 CRM_Utils_Hook::pre('create', 'Auction_Item', NULL, $params);
109 }
110
111 $auction = new CRM_Auction_DAO_Item();
112
113 $auction->copyValues($params);
114 $auction->save();
115
116 // add attachments as needed
117 CRM_Core_BAO_File::formatAttachment($params,
118 $params,
119 'civicrm_auction_item',
120 $auction->id
121 );
122 // add attachments as needed
123 CRM_Core_BAO_File::processAttachment($params,
124 'civicrm_auction_item',
125 $auction->id
126 );
127
128 if (!empty($params['id'])) {
129 CRM_Utils_Hook::post('edit', 'Auction_Item', $auction->id, $auction);
130 }
131 else {
132 CRM_Utils_Hook::post('create', 'Auction_Item', $auction->id, $auction);
133 }
134
135 return $result;
136 }
137
138 /**
139 * function to create the auction
140 *
141 * @param array $params reference array contains the values submitted by the form
142 *
143 * @return object
144 * @access public
145 * @static
146 *
147 */
148 public static function create(&$params) {
149 require_once 'CRM/Core/Transaction.php';
150 $transaction = new CRM_Core_Transaction();
151
152 $auction = self::add($params);
153
154 if (is_a($auction, 'CRM_Core_Error')) {
155 CRM_Core_DAO::transaction('ROLLBACK');
156 return $auction;
157 }
158
159 $transaction->commit();
160
161 return $auction;
162 }
163
164 /**
165 * Function to delete the auction
166 *
167 * @param int $id auction id
168 *
169 * @access public
170 * @static
171 *
172 */
173 static
174 function del($id) {
175 require_once 'CRM/Auction/DAO/Item.php';
176 $auction = new CRM_Auction_DAO_Item();
177 $auction->id = $id;
178 $result = $auction->delete();
179 return $result;
180 }
181
182 /**
183 * Function to check if email is enabled for a given profile
184 *
185 * @param $profileId
186 *
187 * @internal param int $id profile id
188 *
189 * @return boolean
190 * @access public
191 * @static
192 */
193 static
194 function isEmailInProfile($profileId) {
195 $query = "
196 SELECT field_name
197 FROM civicrm_uf_field
198 WHERE field_name like 'email%' And is_active = 1 And uf_group_id = %1";
199
200 $params = array(1 => array($profileId, 'Integer'));
201 $dao = CRM_Core_DAO::executeQuery($query, $params);
202 if (!$dao->fetch()) {
203 return TRUE;
204 }
205 return FALSE;
206 }
207 }
208