PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/eform/src/Entity/EFormType.php

https://gitlab.com/geeta7/drupal
PHP | 429 lines | 138 code | 45 blank | 246 comment | 12 complexity | 3549c739f460844b341bf76372007f0a MD5 | raw file
  1. <?php
  2. namespace Drupal\eform\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
  4. use Drupal\Core\Url;
  5. /**
  6. * Defines the EForm type configuration entity.
  7. *
  8. * @ConfigEntityType(
  9. * id = "eform_type",
  10. * label = @Translation("EForm type"),
  11. * handlers = {
  12. * "access" = "Drupal\Core\Entity\EntityAccessControlHandler",
  13. * "form" = {
  14. * "add" = "Drupal\eform\Form\EFormTypeForm",
  15. * "edit" = "Drupal\eform\Form\EFormTypeForm",
  16. * "delete" = "Drupal\Core\Entity\EntityDeleteForm",
  17. * },
  18. * "list_builder" = "Drupal\eform\EFormTypeListBuilder",
  19. * },
  20. * admin_permission = "administer eform types",
  21. * config_prefix = "type",
  22. * bundle_of = "eform_submission",
  23. * entity_keys = {
  24. * "id" = "type",
  25. * "label" = "name",
  26. * },
  27. * links = {
  28. * "delete-form" = "/admin/structure/eform_types/manage/{eform_type}/delete",
  29. * "edit-form" = "/admin/structure/eform_types/manage/{eform_type}",
  30. * "collection" = "/admin/structure/eform_types",
  31. * }
  32. * )
  33. */
  34. class EFormType extends ConfigEntityBundleBase {
  35. /**
  36. * Closed status for a form.
  37. */
  38. const STATUS_CLOSED = 'EFORM_CLOSED';
  39. /**
  40. * Open status for a form.
  41. */
  42. const STATUS_OPEN = 'EFORM_OPEN';
  43. const RESUBMIT_ACTION_OLD = 'EFORM_RESUBMIT_OLD';
  44. const RESUBMIT_ACTION_NEW = 'EFORM_RESUBMIT_NEW';
  45. const RESUBMIT_ACTION_DISALLOW = 'EFORM_RESUBMIT_DISALLOW';
  46. // @todo Should action be changed from routing to confirm page to routing to user given url defaulting to confirm page?
  47. const RESUBMIT_ACTION_CONFIRM = 'EFORM_RESUBMIT_CONFIRM';
  48. /**
  49. * The machine name of this eform type.
  50. *
  51. * @var string
  52. *
  53. * @todo Rename to $id.
  54. */
  55. public $type;
  56. public $preview_page = 0;
  57. /**
  58. * The human-readable name of the eform type.
  59. *
  60. * @var string
  61. *
  62. * @todo Rename to $label.
  63. */
  64. public $name;
  65. /**
  66. * The title to use for the form.
  67. *
  68. * @var string
  69. */
  70. protected $form_title;
  71. /**
  72. * View to use admin submission view.
  73. *
  74. * @var string
  75. */
  76. protected $admin_submissions_view;
  77. /**
  78. * View to use user submission view.
  79. *
  80. * @var string
  81. */
  82. protected $user_submissions_view;
  83. /**
  84. * @return string
  85. */
  86. public function getUserView() {
  87. return $this->user_submissions_view;
  88. }
  89. /**
  90. * @param string $user_submissions_view
  91. */
  92. public function setUserView($user_submissions_view) {
  93. $this->user_submissions_view = $user_submissions_view;
  94. }
  95. /**
  96. * @return string
  97. */
  98. public function getAdminView() {
  99. return $this->admin_submissions_view;
  100. }
  101. /**
  102. * @param string $admin_submissions_view
  103. */
  104. public function setAdminView($admin_submissions_view) {
  105. $this->admin_submissions_view = $admin_submissions_view;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function getFormTitle() {
  111. return $this->form_title;
  112. }
  113. /**
  114. * @param string $form_title
  115. */
  116. public function setFormTitle($form_title) {
  117. $this->form_title = $form_title;
  118. }
  119. /**
  120. * A brief description of this eform type.
  121. *
  122. * @var string
  123. */
  124. protected $description;
  125. /**
  126. * @var boolean
  127. */
  128. protected $submission_show_submitted = FALSE;
  129. /**
  130. * @var array
  131. */
  132. protected $submission_text;
  133. /**
  134. * @var string
  135. */
  136. protected $submission_page_title;
  137. /**
  138. * @return string
  139. */
  140. public function getSubmissionPageTitle() {
  141. return $this->submission_page_title;
  142. }
  143. /**
  144. * @param string $submission_page_title
  145. */
  146. public function setSubmissionPageTitle($submission_page_title) {
  147. $this->submission_page_title = $submission_page_title;
  148. }
  149. /**
  150. * @return array
  151. */
  152. public function getSubmissionText() {
  153. return $this->submission_text;
  154. }
  155. /**
  156. * @return boolean
  157. */
  158. public function isSubmissionShowSubmitted() {
  159. return $this->submission_show_submitted;
  160. }
  161. /**
  162. * Load default values for any text or formatted text properties that are empty.
  163. *
  164. * @return $this
  165. */
  166. public function loadDefaults() {
  167. $config = $this->getConfigManager()->getConfigFactory()->get('eform.type_defaults');
  168. $default_values = $config->getRawData();
  169. foreach ($default_values as $key => $value) {
  170. $this->overrideWithDefaut($key, $value);
  171. }
  172. return $this;
  173. }
  174. /**
  175. * Override a property with a default value if empty.
  176. *
  177. * Only overrides string properties or empty formatted text properties.
  178. *
  179. * @param $key
  180. * @param $default_value
  181. */
  182. protected function overrideWithDefaut($key, $default_value) {
  183. $override = FALSE;
  184. if (!isset($this->{$key})) {
  185. $override = TRUE;
  186. }
  187. $value = $this->{$key};
  188. if (is_string($value)) {
  189. if (empty($value)) {
  190. $override = TRUE;
  191. }
  192. elseif ($value == '<none>') {
  193. $this->{$key} = '';
  194. return;
  195. }
  196. }
  197. elseif (is_array($value) && isset($value['value']) && isset($value['format'])) {
  198. if (empty($value['value'])) {
  199. $override = TRUE;
  200. }
  201. elseif ($value['value'] == '<none>') {
  202. $this->{$key} = '';
  203. return;
  204. }
  205. }
  206. if ($override) {
  207. $this->{$key} = $default_value;
  208. }
  209. }
  210. /**
  211. * @return boolean
  212. */
  213. public function isDraftable() {
  214. return $this->draftable;
  215. }
  216. /**
  217. * @var boolean;
  218. */
  219. protected $draftable;
  220. /**
  221. * @return string
  222. */
  223. public function getDescription() {
  224. return $this->description;
  225. }
  226. /**
  227. * Help information shown to the user when creating a EForm of this type.
  228. *
  229. * @var string
  230. */
  231. public $help;
  232. /**
  233. * Current status of the form. Currently only closed or open.
  234. *
  235. * @var string;
  236. */
  237. public $form_status;
  238. /**
  239. * Roles @todo text.
  240. *
  241. * @var array;
  242. */
  243. public $roles;
  244. /**
  245. * @var string;
  246. */
  247. protected $resubmit_action;
  248. /**
  249. * @var array;
  250. */
  251. protected $disallow_text;
  252. /**
  253. * @return array
  254. */
  255. public function getDisallowText() {
  256. // @todo Should there be a default disallow text?
  257. return $this->disallow_text;
  258. }
  259. /**
  260. * @param string $resubmit_action
  261. */
  262. public function setResubmitAction($resubmit_action) {
  263. $this->resubmit_action = $resubmit_action;
  264. }
  265. /**
  266. * @return string
  267. */
  268. public function getResubmitAction() {
  269. return $this->resubmit_action;
  270. }
  271. /**
  272. * Module-specific settings for this eform type, keyed by module name.
  273. *
  274. * @var array
  275. *
  276. * @todo Pluginify.
  277. */
  278. public $settings = array();
  279. /**
  280. * {@inheritdoc}
  281. */
  282. public function id() {
  283. return $this->type;
  284. }
  285. /**
  286. * {@inheritdoc}
  287. */
  288. public function getModuleSettings($module) {
  289. if (isset($this->settings[$module]) && is_array($this->settings[$module])) {
  290. return $this->settings[$module];
  291. }
  292. return array();
  293. }
  294. /**
  295. * {@inheritdoc}
  296. */
  297. public function isLocked() {
  298. $locked = \Drupal::state()->get('eform.type.locked');
  299. return isset($locked[$this->id()]) ? $locked[$this->id()] : FALSE;
  300. }
  301. /**
  302. * Get submit link.
  303. *
  304. * @return \Drupal\Core\GeneratedLink|string
  305. */
  306. public function getSubmitLink() {
  307. $url = Url::fromRoute('entity.eform_submission.submit_page', array('eform_type' => $this->type));
  308. // @todo should the submit label be distinct from regular label?
  309. return \Drupal::l($this->label(), $url);
  310. }
  311. /**
  312. * Get the permission string for an operation for this EForm Type.
  313. *
  314. * @param string $op
  315. *
  316. * @return string
  317. *
  318. * @throws \Exception
  319. */
  320. public function getPermission($op = 'submit') {
  321. switch ($op) {
  322. case 'submit':
  323. return 'submit' . $this->id() . ' eform';
  324. case 'edit own':
  325. case 'delete own':
  326. return $op . $this->id() . ' submissions';
  327. default:
  328. throw new \Exception('Unknown operation ' . $op . ' for Eform Type');
  329. }
  330. }
  331. /**
  332. * {@inheritdoc}
  333. *
  334. public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
  335. parent::postSave($storage_controller, $update);
  336. if (!$update) {
  337. // Clear the eform type cache, so the new type appears.
  338. \Drupal::cache()->deleteTags(array('eform_types' => TRUE));
  339. entity_invoke_bundle_hook('create', 'eform', $this->id());
  340. // Unless disabled, automatically create a Body field for new eform types.
  341. if ($this->get('create_body')) {
  342. $label = $this->get('create_body_label');
  343. eform_add_body_field($this, $label);
  344. }
  345. }
  346. elseif ($this->getOriginalID() != $this->id()) {
  347. // Clear the eform type cache to reflect the rename.
  348. \Drupal::cache()->deleteTags(array('eform_types' => TRUE));
  349. $update_count = eform_type_update_eforms($this->getOriginalID(), $this->id());
  350. if ($update_count) {
  351. drupal_set_message(format_plural($update_count,
  352. 'Changed the eform type of 1 post from %old-type to %type.',
  353. 'Changed the eform type of @count posts from %old-type to %type.',
  354. array(
  355. '%old-type' => $this->getOriginalID(),
  356. '%type' => $this->id(),
  357. )));
  358. }
  359. entity_invoke_bundle_hook('rename', 'eform', $this->getOriginalID(), $this->id());
  360. }
  361. else {
  362. // Invalidate the cache tag of the updated eform type only.
  363. cache()->invalidateTags(array('eform_type' => $this->id()));
  364. }
  365. }
  366. **
  367. * {@inheritdoc}
  368. *
  369. public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
  370. parent::postDelete($storage_controller, $entities);
  371. // Clear the eform type cache to reflect the removal.
  372. $storage_controller->resetCache(array_keys($entities));
  373. foreach ($entities as $entity) {
  374. entity_invoke_bundle_hook('delete', 'eform', $entity->id());
  375. }
  376. }
  377. */
  378. }