PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/sites/all/modules/contrib/civicrm/CRM/Event/Form/ManageEvent.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 447 lines | 289 code | 51 blank | 107 comment | 48 complexity | f08cc68fe66d5468b4a827b5b61388e4 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  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. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2019
  30. */
  31. /**
  32. * This class generates form components for processing Event.
  33. */
  34. class CRM_Event_Form_ManageEvent extends CRM_Core_Form {
  35. /**
  36. * The id of the event we are processing.
  37. *
  38. * @var int
  39. */
  40. public $_id;
  41. /**
  42. * Is this the first page?
  43. *
  44. * @var bool
  45. */
  46. protected $_first = FALSE;
  47. /**
  48. * Are we in single form mode or wizard mode?
  49. *
  50. * @var bool
  51. */
  52. protected $_single;
  53. public $_action;
  54. /**
  55. * Are we actually managing an event template?
  56. * @var bool
  57. */
  58. protected $_isTemplate = FALSE;
  59. /**
  60. * Pre-populate fields based on this template event_id.
  61. *
  62. * @var int
  63. */
  64. protected $_templateId;
  65. protected $_cancelURL = NULL;
  66. /**
  67. * The campaign id of the existing event, we use this to know if we need to update
  68. * the participant records
  69. * @var int
  70. */
  71. protected $_campaignID = NULL;
  72. /**
  73. * Check if repeating event.
  74. * @var bool
  75. */
  76. public $_isRepeatingEvent;
  77. /**
  78. * Explicitly declare the entity api name.
  79. */
  80. public function getDefaultEntity() {
  81. return 'Event';
  82. }
  83. /**
  84. * Explicitly declare the form context.
  85. */
  86. public function getDefaultContext() {
  87. return 'create';
  88. }
  89. /**
  90. * Set the active tab
  91. *
  92. * @param string $default
  93. *
  94. * @throws \CRM_Core_Exception
  95. */
  96. public function setSelectedChild($default = NULL) {
  97. $selectedChild = CRM_Utils_Request::retrieve('selectedChild', 'Alphanumeric', $this, FALSE, $default);
  98. if (!empty($selectedChild)) {
  99. $this->set('selectedChild', $selectedChild);
  100. $this->assign('selectedChild', $selectedChild);
  101. }
  102. }
  103. /**
  104. * Set variables up before form is built.
  105. */
  106. public function preProcess() {
  107. $config = CRM_Core_Config::singleton();
  108. if (in_array('CiviEvent', $config->enableComponents)) {
  109. $this->assign('CiviEvent', TRUE);
  110. }
  111. CRM_Core_Form_RecurringEntity::preProcess('civicrm_event');
  112. $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
  113. $this->assign('action', $this->_action);
  114. $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'GET');
  115. if ($this->_id) {
  116. $this->_isRepeatingEvent = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
  117. $this->assign('eventId', $this->_id);
  118. if (!empty($this->_addBlockName) && empty($this->_addProfileBottom) && empty($this->_addProfileBottomAdd)) {
  119. $this->add('hidden', 'id', $this->_id);
  120. }
  121. $this->_single = TRUE;
  122. $params = ['id' => $this->_id];
  123. CRM_Event_BAO_Event::retrieve($params, $eventInfo);
  124. // its an update mode, do a permission check
  125. if (!CRM_Event_BAO_Event::checkPermission($this->_id, CRM_Core_Permission::EDIT)) {
  126. CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
  127. }
  128. $participantListingID = CRM_Utils_Array::value('participant_listing_id', $eventInfo);
  129. //CRM_Core_DAO::getFieldValue( 'CRM_Event_DAO_Event', $this->_id, 'participant_listing_id' );
  130. if ($participantListingID) {
  131. $participantListingURL = CRM_Utils_System::url('civicrm/event/participant',
  132. "reset=1&id={$this->_id}",
  133. TRUE, NULL, TRUE, TRUE
  134. );
  135. $this->assign('participantListingURL', $participantListingURL);
  136. }
  137. $this->assign('isOnlineRegistration', CRM_Utils_Array::value('is_online_registration', $eventInfo));
  138. $this->assign('id', $this->_id);
  139. }
  140. // figure out whether we’re handling an event or an event template
  141. if ($this->_id) {
  142. $this->_isTemplate = CRM_Utils_Array::value('is_template', $eventInfo);
  143. }
  144. elseif ($this->_action & CRM_Core_Action::ADD) {
  145. $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this);
  146. }
  147. $this->assign('isTemplate', $this->_isTemplate);
  148. if ($this->_id) {
  149. if ($this->_isTemplate) {
  150. $title = CRM_Utils_Array::value('template_title', $eventInfo);
  151. CRM_Utils_System::setTitle(ts('Edit Event Template') . " - $title");
  152. }
  153. else {
  154. $configureText = ts('Configure Event');
  155. $title = CRM_Utils_Array::value('title', $eventInfo);
  156. //If it is a repeating event change title
  157. if ($this->_isRepeatingEvent) {
  158. $configureText = 'Configure Repeating Event';
  159. }
  160. CRM_Utils_System::setTitle($configureText . " - $title");
  161. }
  162. $this->assign('title', $title);
  163. }
  164. elseif ($this->_action & CRM_Core_Action::ADD) {
  165. if ($this->_isTemplate) {
  166. $title = ts('New Event Template');
  167. CRM_Utils_System::setTitle($title);
  168. }
  169. else {
  170. $title = ts('New Event');
  171. CRM_Utils_System::setTitle($title);
  172. }
  173. $this->assign('title', $title);
  174. }
  175. if (CRM_Core_Permission::check('view event participants') &&
  176. CRM_Core_Permission::check('view all contacts')
  177. ) {
  178. $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
  179. $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
  180. $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
  181. $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
  182. $this->assign('findParticipants', $findParticipants);
  183. }
  184. $this->_templateId = (int) CRM_Utils_Request::retrieve('template_id', 'Integer', $this);
  185. //Is a repeating event
  186. if ($this->_isRepeatingEvent) {
  187. $isRepeatingEntity = TRUE;
  188. $this->assign('isRepeatingEntity', $isRepeatingEntity);
  189. }
  190. // CRM-16776 - show edit/copy/create buttons for Profiles if user has required permission.
  191. $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
  192. $ufCreate = CRM_ACL_API::group(CRM_Core_Permission::CREATE, NULL, 'civicrm_uf_group', $ufGroups);
  193. $ufEdit = CRM_ACL_API::group(CRM_Core_Permission::EDIT, NULL, 'civicrm_uf_group', $ufGroups);
  194. $checkPermission = [
  195. [
  196. 'administer CiviCRM',
  197. 'manage event profiles',
  198. ],
  199. ];
  200. if (CRM_Core_Permission::check($checkPermission) || !empty($ufCreate) || !empty($ufEdit)) {
  201. $this->assign('perm', TRUE);
  202. }
  203. // also set up tabs
  204. CRM_Event_Form_ManageEvent_TabHeader::build($this);
  205. // Set Done button URL and breadcrumb. Templates go back to Manage Templates,
  206. // otherwise go to Manage Event for new event or ManageEventEdit if event if exists.
  207. $breadCrumb = [];
  208. if (!$this->_isTemplate) {
  209. if ($this->_id) {
  210. $this->_doneUrl = CRM_Utils_System::url(CRM_Utils_System::currentPath(),
  211. "action=update&reset=1&id={$this->_id}"
  212. );
  213. }
  214. else {
  215. $this->_doneUrl = CRM_Utils_System::url('civicrm/event/manage',
  216. 'reset=1'
  217. );
  218. $breadCrumb = [
  219. [
  220. 'title' => ts('Manage Events'),
  221. 'url' => $this->_doneUrl,
  222. ],
  223. ];
  224. }
  225. }
  226. else {
  227. $this->_doneUrl = CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1');
  228. $breadCrumb = [
  229. [
  230. 'title' => ts('Manage Event Templates'),
  231. 'url' => $this->_doneUrl,
  232. ],
  233. ];
  234. }
  235. CRM_Utils_System::appendBreadCrumb($breadCrumb);
  236. }
  237. /**
  238. * Set default values for the form.
  239. *
  240. * For edit/view mode the default values are retrieved from the database.
  241. */
  242. public function setDefaultValues() {
  243. $defaults = [];
  244. if (isset($this->_id)) {
  245. $params = ['id' => $this->_id];
  246. CRM_Event_BAO_Event::retrieve($params, $defaults);
  247. $this->_campaignID = CRM_Utils_Array::value('campaign_id', $defaults);
  248. }
  249. elseif ($this->_templateId) {
  250. $params = ['id' => $this->_templateId];
  251. CRM_Event_BAO_Event::retrieve($params, $defaults);
  252. $defaults['is_template'] = $this->_isTemplate;
  253. $defaults['template_id'] = $defaults['id'];
  254. unset($defaults['id']);
  255. }
  256. else {
  257. $defaults['is_active'] = 1;
  258. $defaults['style'] = 'Inline';
  259. }
  260. return $defaults;
  261. }
  262. /**
  263. * Build the form object.
  264. */
  265. public function buildQuickForm() {
  266. $session = CRM_Core_Session::singleton();
  267. $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
  268. if (!$this->_cancelURL) {
  269. if ($this->_isTemplate) {
  270. $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/eventTemplate',
  271. 'reset=1'
  272. );
  273. }
  274. else {
  275. $this->_cancelURL = CRM_Utils_System::url('civicrm/event/manage',
  276. 'reset=1'
  277. );
  278. }
  279. }
  280. if ($this->_cancelURL) {
  281. $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
  282. }
  283. if ($this->_single) {
  284. $buttons = [
  285. [
  286. 'type' => 'upload',
  287. 'name' => ts('Save'),
  288. 'isDefault' => TRUE,
  289. ],
  290. [
  291. 'type' => 'upload',
  292. 'name' => ts('Save and Done'),
  293. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  294. 'subName' => 'done',
  295. ],
  296. [
  297. 'type' => 'cancel',
  298. 'name' => ts('Cancel'),
  299. ],
  300. ];
  301. $this->addButtons($buttons);
  302. }
  303. else {
  304. $buttons = [];
  305. if (!$this->_first) {
  306. $buttons[] = [
  307. 'type' => 'back',
  308. 'name' => ts('Previous'),
  309. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  310. ];
  311. }
  312. $buttons[] = [
  313. 'type' => 'upload',
  314. 'name' => ts('Continue'),
  315. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  316. 'isDefault' => TRUE,
  317. ];
  318. $buttons[] = [
  319. 'type' => 'cancel',
  320. 'name' => ts('Cancel'),
  321. ];
  322. $this->addButtons($buttons);
  323. }
  324. $session->replaceUserContext($this->_cancelURL);
  325. $this->add('hidden', 'is_template', $this->_isTemplate);
  326. }
  327. public function endPostProcess() {
  328. // make submit buttons keep the current working tab opened.
  329. if ($this->_action & CRM_Core_Action::UPDATE) {
  330. $className = CRM_Utils_String::getClassName($this->_name);
  331. // hack for special cases.
  332. switch ($className) {
  333. case 'Event':
  334. $attributes = $this->getVar('_attributes');
  335. $subPage = CRM_Utils_Request::retrieveComponent($attributes);
  336. break;
  337. case 'EventInfo':
  338. $subPage = 'settings';
  339. break;
  340. case 'ScheduleReminders':
  341. $subPage = 'reminder';
  342. break;
  343. default:
  344. $subPage = strtolower($className);
  345. break;
  346. }
  347. CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
  348. [1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className)]
  349. ), ts('Saved'), 'success');
  350. $config = CRM_Core_Config::singleton();
  351. if (in_array('CiviCampaign', $config->enableComponents)) {
  352. $values = $this->controller->exportValues($this->_name);
  353. $newCampaignID = CRM_Utils_Array::value('campaign_id', $values);
  354. $eventID = CRM_Utils_Array::value('id', $values);
  355. if ($eventID && $this->_campaignID != $newCampaignID) {
  356. CRM_Event_BAO_Event::updateParticipantCampaignID($eventID, $newCampaignID);
  357. }
  358. }
  359. $this->postProcessHook();
  360. if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
  361. if ($this->_isTemplate) {
  362. CRM_Core_Session::singleton()
  363. ->pushUserContext(CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1'));
  364. }
  365. else {
  366. CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/event/manage', 'reset=1'));
  367. }
  368. }
  369. else {
  370. CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url("civicrm/event/manage/{$subPage}",
  371. "action=update&reset=1&id={$this->_id}"
  372. ));
  373. }
  374. }
  375. }
  376. /**
  377. * @return string
  378. */
  379. public function getTemplateFileName() {
  380. if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || $this->_action & CRM_Core_Action::DELETE) {
  381. return parent::getTemplateFileName();
  382. }
  383. else {
  384. // hack lets suppress the form rendering for now
  385. self::$_template->assign('isForm', FALSE);
  386. return 'CRM/Event/Form/ManageEvent/Tab.tpl';
  387. }
  388. }
  389. /**
  390. * Pre-load libraries required by Online Registration Profile fields
  391. */
  392. public static function addProfileEditScripts() {
  393. CRM_UF_Page_ProfileEditor::registerProfileScripts();
  394. CRM_UF_Page_ProfileEditor::registerSchemas(['IndividualModel', 'ParticipantModel']);
  395. }
  396. }