PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Contribute/Form/ContributionPage.php

https://github.com/michaelmcandrew/vaw
PHP | 354 lines | 205 code | 45 blank | 104 comment | 57 complexity | 574aed0a5c2663dc744bee9d7a4a3cca MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. require_once 'CRM/Core/Form.php';
  35. require_once 'CRM/Core/PseudoConstant.php';
  36. require_once 'CRM/Contribute/PseudoConstant.php';
  37. /**
  38. * form to process actions on the group aspect of Custom Data
  39. */
  40. class CRM_Contribute_Form_ContributionPage extends CRM_Core_Form
  41. {
  42. /**
  43. * the page id saved to the session for an update
  44. *
  45. * @var int
  46. * @access protected
  47. */
  48. protected $_id;
  49. /**
  50. * the pledgeBlock id saved to the session for an update
  51. *
  52. * @var int
  53. * @access protected
  54. */
  55. protected $_pledgeBlockID;
  56. /**
  57. * are we in single form mode or wizard mode?
  58. *
  59. * @var boolean
  60. * @access protected
  61. */
  62. protected $_single;
  63. /**
  64. * is this the first page?
  65. *
  66. * @var boolean
  67. * @access protected
  68. */
  69. protected $_first = false;
  70. /**
  71. * store price set id.
  72. *
  73. * @var int
  74. * @access protected
  75. */
  76. protected $_priceSetID = null;
  77. protected $_values;
  78. /**
  79. * Function to set variables up before form is built
  80. *
  81. * @return void
  82. * @access public
  83. */
  84. public function preProcess()
  85. {
  86. // current contribution page id
  87. $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
  88. $this, false, 0);
  89. $this->assign( 'contributionPageID', $this->_id );
  90. // get the requested action
  91. $this->_action = CRM_Utils_Request::retrieve('action', 'String',
  92. $this, false, 'browse'); // default to 'browse'
  93. // setting title and 3rd level breadcrumb for html page if contrib page exists
  94. if ( $this->_id ) {
  95. $title = CRM_Core_DAO::getFieldValue( 'CRM_Contribute_DAO_ContributionPage', $this->_id, 'title' );
  96. if ($this->_action == CRM_Core_Action::UPDATE) {
  97. $this->_single = true;
  98. }
  99. }
  100. // set up tabs
  101. require_once 'CRM/Contribute/Form/ContributionPage/TabHeader.php';
  102. CRM_Contribute_Form_ContributionPage_TabHeader::build( $this );
  103. if ($this->_action == CRM_Core_Action::UPDATE) {
  104. CRM_Utils_System::setTitle(ts('Configure Page - %1', array(1 => $title)));
  105. } else if ($this->_action == CRM_Core_Action::VIEW) {
  106. CRM_Utils_System::setTitle(ts('Preview Page - %1', array(1 => $title)));
  107. } else if ($this->_action == CRM_Core_Action::DELETE) {
  108. CRM_Utils_System::setTitle(ts('Delete Page - %1', array(1 => $title)));
  109. }
  110. //cache values.
  111. $this->_values = $this->get( 'values' );
  112. if ( !is_array( $this->_values ) ) {
  113. $this->_values = array( );
  114. if ( isset( $this->_id ) && $this->_id ) {
  115. $params = array('id' => $this->_id );
  116. CRM_Core_DAO::commonRetrieve( 'CRM_Contribute_DAO_ContributionPage', $params, $this->_values );
  117. }
  118. $this->set( 'values', $this->_values );
  119. }
  120. }
  121. /**
  122. * Function to actually build the form
  123. *
  124. * @return void
  125. * @access public
  126. */
  127. public function buildQuickForm()
  128. {
  129. $this->applyFilter('__ALL__', 'trim');
  130. $session =& CRM_Core_Session::singleton( );
  131. $this->_cancelURL = CRM_Utils_Array::value( 'cancelURL', $_POST );
  132. if ( !$this->_cancelURL ) {
  133. $this->_cancelURL = CRM_Utils_System::url( 'civicrm/admin/contribute', 'reset=1' );
  134. }
  135. if ( $this->_cancelURL ) {
  136. $this->addElement( 'hidden', 'cancelURL', $this->_cancelURL );
  137. }
  138. if ( $this->_single ) {
  139. $this->addButtons(array(
  140. array ( 'type' => 'next',
  141. 'name' => ts('Save'),
  142. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  143. 'isDefault' => true ),
  144. array ( 'type' => 'upload',
  145. 'name' => ts('Save and Done'),
  146. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  147. 'subName' => 'done' ),
  148. array ( 'type' => 'cancel',
  149. 'name' => ts('Cancel') ),
  150. )
  151. );
  152. } else {
  153. $buttons = array( );
  154. if ( ! $this->_first ) {
  155. $buttons[] = array ( 'type' => 'back',
  156. 'name' => ts('<< Previous'),
  157. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' );
  158. }
  159. $buttons[] = array ( 'type' => 'next',
  160. 'name' => ts('Continue >>'),
  161. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  162. 'isDefault' => true );
  163. $buttons[] = array ( 'type' => 'cancel',
  164. 'name' => ts('Cancel') );
  165. $this->addButtons( $buttons );
  166. }
  167. $session->replaceUserContext( $this->_cancelURL );
  168. // views are implemented as frozen form
  169. if ($this->_action & CRM_Core_Action::VIEW) {
  170. $this->freeze();
  171. $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'"));
  172. }
  173. }
  174. /**
  175. * This function sets the default values for the form. Note that in edit/view mode
  176. * the default values are retrieved from the database
  177. *
  178. * @access public
  179. * @return void
  180. */
  181. function setDefaultValues()
  182. {
  183. //some child classes calling setdefaults directly w/o preprocess.
  184. $this->_values = $this->get( 'values' );
  185. if ( !is_array( $this->_values ) ) {
  186. $this->_values = array( );
  187. if ( isset( $this->_id ) && $this->_id ) {
  188. $params = array('id' => $this->_id );
  189. CRM_Core_DAO::commonRetrieve( 'CRM_Contribute_DAO_ContributionPage', $params, $this->_values );
  190. }
  191. $this->set( 'values', $this->_values );
  192. }
  193. $defaults = $this->_values;
  194. $config = CRM_Core_Config::singleton();
  195. if (isset($this->_id)) {
  196. //set defaults for pledgeBlock values.
  197. require_once 'CRM/Pledge/BAO/PledgeBlock.php';
  198. $pledgeBlockParams = array( 'entity_id' => $this->_id,
  199. 'entity_table' => ts('civicrm_contribution_page') );
  200. $pledgeBlockDefaults = array( );
  201. CRM_Pledge_BAO_pledgeBlock::retrieve( $pledgeBlockParams, $pledgeBlockDefaults );
  202. if ( $this->_pledgeBlockID = CRM_Utils_Array::value('id', $pledgeBlockDefaults ) ) {
  203. $defaults['is_pledge_active'] = true;
  204. }
  205. $pledgeBlock = array( 'is_pledge_interval', 'max_reminders',
  206. 'initial_reminder_day', 'additional_reminder_day' );
  207. foreach ( $pledgeBlock as $key ) {
  208. $defaults[$key] = CRM_Utils_Array::value( $key, $pledgeBlockDefaults );
  209. }
  210. require_once 'CRM/Core/BAO/CustomOption.php';
  211. if ( CRM_Utils_Array::value( 'pledge_frequency_unit', $pledgeBlockDefaults ) ) {
  212. $defaults['pledge_frequency_unit'] =
  213. array_fill_keys( explode( CRM_Core_DAO::VALUE_SEPARATOR,
  214. $pledgeBlockDefaults['pledge_frequency_unit'] ), '1' );
  215. }
  216. // fix the display of the monetary value, CRM-4038
  217. require_once 'CRM/Utils/Money.php';
  218. if (isset($defaults['goal_amount'])) {
  219. $defaults['goal_amount'] = CRM_Utils_Money::format($defaults['goal_amount'], null, '%a');
  220. }
  221. // get price set of type contributions
  222. require_once 'CRM/Price/BAO/Set.php';
  223. //this is the value for stored in db if price set extends contribution
  224. $usedFor = 2;
  225. $this->_priceSetID = CRM_Price_BAO_Set::getFor( 'civicrm_contribution_page', $this->_id, $usedFor );
  226. if ( $this->_priceSetID ) $defaults['price_set_id'] = $this->_priceSetID;
  227. if ( CRM_Utils_Array::value( 'end_date', $defaults ) ) {
  228. list( $defaults['end_date'], $defaults['end_date_time'] ) = CRM_Utils_Date::setDateDefaults( $defaults['end_date'] );
  229. }
  230. if ( CRM_Utils_Array::value( 'start_date', $defaults ) ) {
  231. list( $defaults['start_date'], $defaults['start_date_time'] ) = CRM_Utils_Date::setDateDefaults( $defaults['start_date'] );
  232. }
  233. } else {
  234. $defaults['is_active'] = 1;
  235. // set current date as start date
  236. list( $defaults['start_date'], $defaults['start_date_time'] ) = CRM_Utils_Date::setDateDefaults( );
  237. }
  238. if (! isset($defaults['for_organization'])) {
  239. $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
  240. }
  241. if ( CRM_Utils_Array::value( 'recur_frequency_unit',$defaults ) ) {
  242. require_once 'CRM/Core/BAO/CustomOption.php';
  243. $defaults['recur_frequency_unit'] =
  244. array_fill_keys( explode( CRM_Core_DAO::VALUE_SEPARATOR,
  245. $defaults['recur_frequency_unit'] ), '1' );
  246. } else {
  247. require_once 'CRM/Core/OptionGroup.php';
  248. $defaults['recur_frequency_unit'] =
  249. array_fill_keys( CRM_Core_OptionGroup::values( 'recur_frequency_units' ), '1' );
  250. }
  251. if ( CRM_Utils_Array::value( 'is_for_organization', $defaults ) ) {
  252. $defaults['is_organization'] = 1;
  253. } else {
  254. $defaults['is_for_organization'] = 1;
  255. }
  256. return $defaults;
  257. }
  258. /**
  259. * Process the form
  260. *
  261. * @return void
  262. * @access public
  263. */
  264. public function postProcess()
  265. {
  266. $pageId = $this->get( 'id' );
  267. //page is newly created.
  268. if ( $pageId && !$this->_id ) {
  269. $session = CRM_Core_Session::singleton( );
  270. $session->pushUserContext( CRM_Utils_System::url( 'civicrm/admin/contribute', 'reset=1' ) );
  271. }
  272. }
  273. function endPostProcess( )
  274. {
  275. // make submit buttons keep the current working tab opened.
  276. if ( $this->_action & CRM_Core_Action::UPDATE ) {
  277. $className = CRM_Utils_String::getClassName( $this->_name );
  278. if ( $className == 'ThankYou' ) {
  279. $subPage = 'thankYou';
  280. } else if ( $className == 'Contribute' ) {
  281. $subPage = 'friend';
  282. } else if ( $className == 'MembershipBlock' ) {
  283. $subPage = 'membership';
  284. } else {
  285. $subPage = strtolower( $className );
  286. }
  287. CRM_Core_Session::setStatus( ts("'%1' information has been saved.",
  288. array( 1 => ( $subPage == 'friend' ) ? 'Friend' : $className ) ) );
  289. $this->postProcessHook( );
  290. if ( $this->controller->getButtonName('submit') == "_qf_{$className}_next" ) {
  291. CRM_Utils_System::redirect( CRM_Utils_System::url( "civicrm/admin/contribute/{$subPage}",
  292. "action=update&reset=1&id={$this->_id}" ) );
  293. } else {
  294. CRM_Utils_System::redirect( CRM_Utils_System::url( "civicrm/admin/contribute", 'reset=1' ) );
  295. }
  296. }
  297. }
  298. function getTemplateFileName( )
  299. {
  300. if ( $this->controller->getPrint( ) == CRM_Core_Smarty::PRINT_NOFORM ||
  301. $this->getVar( '_id' ) <= 0 ||
  302. ( $this->_action & CRM_Core_Action::DELETE ) ||
  303. ( CRM_Utils_String::getClassName( $this->_name ) == 'AddProduct' ) ) {
  304. return parent::getTemplateFileName( );
  305. } else {
  306. return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';
  307. }
  308. }
  309. }