date_tools.wizard.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * @file
  4. * The Date Wizard code.
  5. */
  6. /**
  7. * Implements hook_form().
  8. *
  9. * @todo.
  10. */
  11. function date_tools_wizard_form() {
  12. $form = array();
  13. $form['type'] = array(
  14. '#type' => 'fieldset',
  15. '#title' => t('Content type'),
  16. );
  17. $form['type']['bundle'] = array(
  18. '#type' => 'textfield',
  19. '#default_value' => 'date',
  20. '#title' => t('Content type name'),
  21. '#description' => t('Machine-readable name. Allowed values: (a-z, 0-9, _). If this is not an existing content type, the content type will be created.'),
  22. );
  23. $form['type']['name'] = array(
  24. '#type' => 'textfield',
  25. '#default_value' => t('Date'),
  26. '#title' => t('Content type label'),
  27. '#description' => t('The human-readable name for this content type. Only needed when creating a new content type.'),
  28. );
  29. $form['type']['type_description'] = array(
  30. '#type' => 'textarea',
  31. '#default_value' => t('A date content type that is linked to a Views calendar.'),
  32. '#title' => t('Content type description'),
  33. '#description' => t('A description for the content type. Only needed when creating a new content type.'),
  34. );
  35. $form['field'] = array(
  36. '#type' => 'fieldset',
  37. '#title' => t('Date field'),
  38. );
  39. $form['field']['field_name'] = array(
  40. '#type' => 'textfield',
  41. '#default_value' => 'date',
  42. '#field_prefix' => 'field_',
  43. '#title' => t('Date field name'),
  44. '#description' => t('Machine-readable name. Allowed values: (a-z, 0-9, _) Must not be an existing field name.'),
  45. );
  46. $form['field']['label'] = array(
  47. '#tree' => TRUE,
  48. '#type' => 'textfield',
  49. '#default_value' => t('Date'),
  50. '#title' => t('Date field label'),
  51. '#description' => t('The human-readable label for this field.'),
  52. );
  53. $form['field']['widget_type'] = array(
  54. '#type' => 'select',
  55. '#options' => date_tools_wizard_widget_types(),
  56. '#default_value' => 'date_select',
  57. '#title' => t('Date widget type'),
  58. );
  59. $form['field']['repeat'] = array(
  60. '#type' => 'select',
  61. '#default_value' => 0,
  62. '#options' => array(
  63. 0 => t('No'),
  64. 1 => t('Yes'),
  65. ),
  66. '#title' => t('Show repeating date options'),
  67. '#access' => module_exists('date_repeat_field'),
  68. );
  69. $form['field']['advanced'] = array(
  70. '#type' => 'fieldset',
  71. '#collapsible' => TRUE,
  72. '#collapsed' => TRUE,
  73. '#title' => t('Advanced options'),
  74. );
  75. $form['field']['advanced']['todate'] = array(
  76. '#type' => 'select',
  77. '#default_value' => 'optional',
  78. '#options' => array(
  79. '' => t('Never'),
  80. 'optional' => t('Optional'),
  81. 'required' => t('Required'),
  82. ),
  83. '#title' => t('End Date'),
  84. '#description' => t("Display a matching second date field as a 'End date'."),
  85. );
  86. $form['field']['advanced']['field_type'] = array(
  87. '#type' => 'select',
  88. '#options' => date_tools_wizard_field_types(),
  89. '#default_value' => 'datetime',
  90. '#title' => t('Date field type'),
  91. '#description' => t("The recommend type is Datetime, except for historical dates or dates with only year or month granularity. Older or incomplete dates should use the Date type (an ISO date)."),
  92. );
  93. $form['field']['advanced']['granularity'] = array(
  94. '#type' => 'select',
  95. '#options' => date_granularity_names(),
  96. '#default_value' => array('month', 'day', 'year', 'hour', 'minute'),
  97. '#title' => t('Granularity'),
  98. '#multiple' => TRUE,
  99. );
  100. $form['field']['advanced']['year_range'] = array(
  101. '#type' => 'textfield',
  102. '#default_value' => '-1:+1',
  103. '#title' => t('Year range'),
  104. '#description' => t("Range of allowed years, oldest to newest. '-1:+1 means oldest date is one year back, newest is one year forward from current year."),
  105. );
  106. $form['field']['advanced']['tz_handling'] = array(
  107. '#type' => 'select',
  108. '#options' => date_tools_wizard_tz_handling(),
  109. '#default_value' => 'site',
  110. '#title' => t('Date timezone handling'),
  111. '#description' => t("Timezone handling should be set to 'none' for granularity without time elements."),
  112. );
  113. $form['calendar'] = array(
  114. '#type' => 'select',
  115. '#default_value' => module_exists('calendar'),
  116. '#options' => array(
  117. 0 => t('No'),
  118. 1 => t('Yes'),
  119. ),
  120. '#title' => t('Create a calendar for this date field'),
  121. '#access' => module_exists('calendar'),
  122. );
  123. $form['submit'] = array(
  124. '#type' => 'submit',
  125. '#value' => t('Save'),
  126. );
  127. return $form;
  128. }
  129. /**
  130. * Form validate.
  131. *
  132. * @todo.
  133. */
  134. function date_tools_wizard_form_validate(&$form, &$form_state) {
  135. $bundle = $form_state['values']['bundle'];
  136. $field_name = 'field_' . $form_state['values']['field_name'];
  137. $args = array(
  138. ':field_name' => $field_name,
  139. ':bundle' => $bundle,
  140. ':entity_type' => 'node',
  141. );
  142. $query = "SELECT type FROM {node_type} WHERE type=:bundle";
  143. $existing_type = db_query($query, array(':bundle' => $args[':bundle']))->fetchField();
  144. $query = "SELECT field_name FROM {field_config_instance} WHERE field_name=:field_name AND bundle=:bundle AND entity_type=:entity_type";
  145. $existing_instance = db_query($query, $args)->fetchField();
  146. if ($existing_type) {
  147. drupal_set_message(t('This content type name already exists, adding new field to existing content type.'));
  148. }
  149. if (!preg_match('!^[a-z0-9_]+$!', $bundle)) {
  150. form_set_error('bundle', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  151. }
  152. if (!empty($form_state['values']['calendar']) && !empty($form_state['values']['blocks']) && strlen($bundle) > 12) {
  153. form_set_error('bundle', t('The content type name must be no more than 12 characters long when using it to create a calendar and blocks.'));
  154. }
  155. if ($existing_instance) {
  156. form_set_error('field_name', t('This field name already exists.'));
  157. }
  158. if (strlen($field_name) > 32) {
  159. form_set_error('field_name', t('The field name must be no more than 26 characters long.'));
  160. }
  161. if (!date_has_time($form_state['values']['granularity']) && $form_state['values']['tz_handling'] != 'none') {
  162. form_set_error('tz_handling', t('Timezone handling must be none for granularity without time.'));
  163. }
  164. }
  165. /**
  166. * Form submit.
  167. *
  168. * @todo.
  169. */
  170. function date_tools_wizard_form_submit(&$form, &$form_state) {
  171. $view_name = date_tools_wizard_build($form_state['values']);
  172. menu_rebuild();
  173. if (!empty($form_state['values']['calendar']) && !empty($view_name)) {
  174. $form_state['redirect'] = 'admin/structure/views/template/' . $view_name . '/add';
  175. }
  176. else {
  177. $form_state['redirect'] = 'admin/structure/types/manage/' . str_replace('_', '-', $form_state['values']['bundle']) . '/fields';
  178. }
  179. }
  180. /**
  181. * Wizard build.
  182. *
  183. * @todo.
  184. */
  185. function date_tools_wizard_build($form_values) {
  186. extract($form_values);
  187. $field_name = 'field_' . $field_name;
  188. $base_table = 'node';
  189. // Create a node type if it doesn't already exist.
  190. // This makes it possible to add additional date fields to
  191. // an existing type.
  192. $types = node_type_get_names();
  193. $type_settings = array();
  194. if (!array_key_exists($bundle, $types)) {
  195. date_tools_wizard_create_content_type($name, $bundle, $type_description, $type_settings);
  196. drupal_set_message(t('Your content type @name has been created.', array('@name' => $name)));
  197. }
  198. else {
  199. $types = node_type_get_types();
  200. $type = $types[$bundle];
  201. if (!empty($type_settings)) {
  202. foreach ($type_settings as $key => $setting) {
  203. $type->$key = $setting;
  204. }
  205. node_type_save($type);
  206. }
  207. $name = $type->name;
  208. }
  209. $field = array(
  210. 'field_name' => $field_name,
  211. 'type' => $field_type,
  212. 'cardinality' => $repeat ? FIELD_CARDINALITY_UNLIMITED : 1,
  213. 'settings' => array(
  214. 'granularity' => $granularity,
  215. 'tz_handling' => $tz_handling,
  216. 'timezone_db' => date_get_timezone_db($tz_handling),
  217. 'repeat' => $repeat,
  218. 'todate' => !empty($todate) ? $todate : 'optional',
  219. ),
  220. );
  221. $instance = array(
  222. 'entity_type' => 'node',
  223. 'field_name' => $field_name,
  224. 'label' => $label,
  225. 'bundle' => $bundle,
  226. // Move the date right below the title.
  227. 'weight' => -4,
  228. 'widget' => array(
  229. 'type' => $widget_type,
  230. // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
  231. 'settings' => array(
  232. 'increment' => 15,
  233. // The number of years to go back and forward in drop-down year
  234. // selectors.
  235. 'year_range' => !empty($year_range) ? $year_range : '-0:+1',
  236. 'input_format' => date_default_format($widget_type),
  237. 'text_parts' => array(),
  238. 'label_position' => 'above',
  239. 'repeat_collapsed' => 0,
  240. ),
  241. 'weight' => -4,
  242. ),
  243. 'settings' => array(
  244. 'default_value' => 'now',
  245. 'default_value2' => 'blank',
  246. ),
  247. );
  248. $instance['display'] = array(
  249. 'default' => array(
  250. 'label' => 'above',
  251. 'type' => 'date_default',
  252. 'settings' => array(
  253. 'format_type' => 'long',
  254. 'show_repeat_rule' => 'show',
  255. 'multiple_number' => '',
  256. 'multiple_from' => '',
  257. 'multiple_to' => '',
  258. 'fromto' => 'both',
  259. ),
  260. 'module' => 'date',
  261. 'weight' => 0 ,
  262. ),
  263. 'teaser' => array(
  264. 'label' => 'above',
  265. 'type' => 'date_default',
  266. 'weight' => 0,
  267. 'settings' => array(
  268. 'format_type' => 'long',
  269. 'show_repeat_rule' => 'show',
  270. 'multiple_number' => '',
  271. 'multiple_from' => '',
  272. 'multiple_to' => '',
  273. 'fromto' => 'both',
  274. ),
  275. 'module' => 'date',
  276. ),
  277. );
  278. $field = field_create_field($field);
  279. $instance = field_create_instance($instance);
  280. $view_name = 'calendar_node_' . $field_name;
  281. field_info_cache_clear(TRUE);
  282. field_cache_clear(TRUE);
  283. drupal_set_message(t('Your date field @name has been created.', array('@name' => $label)));
  284. return $view_name;
  285. }
  286. /**
  287. * Includes handler.
  288. *
  289. * @todo.
  290. */
  291. function date_tools_wizard_include() {
  292. module_load_include('inc', 'node', 'content_types');
  293. module_load_include('inc', 'node', 'node.pages');
  294. module_load_include('inc', 'field', 'field.crud');
  295. module_load_include('inc', 'date', 'date_admin');
  296. }
  297. /**
  298. * Implements hook_field_types().
  299. *
  300. * @todo.
  301. */
  302. function date_tools_wizard_field_types() {
  303. $field_types = array();
  304. foreach (date_field_info() as $name => $info) {
  305. $field_types[$name] = $info['label'];
  306. }
  307. return $field_types;
  308. }
  309. /**
  310. * Implements hook_widget_types().
  311. * @todo.
  312. */
  313. function date_tools_wizard_widget_types() {
  314. $widget_types = array();
  315. foreach (date_field_widget_info() as $name => $info) {
  316. if (!strstr($name, '_repeat')) {
  317. $widget_types[$name] = $info['label'];
  318. }
  319. }
  320. return $widget_types;
  321. }
  322. /**
  323. * Tz handler.
  324. *
  325. * @todo.
  326. */
  327. function date_tools_wizard_tz_handling() {
  328. include_once drupal_get_path('module', 'date') . '/date_admin.inc';
  329. return date_timezone_handling_options();
  330. }
  331. /**
  332. * Create date tools wizard content type.
  333. *
  334. * @todo.
  335. */
  336. function date_tools_wizard_create_content_type($name, $bundle, $description, $type_settings = array()) {
  337. date_tools_wizard_include();
  338. // Create the content type.
  339. $values = array(
  340. 'name' => $name,
  341. 'type' => $bundle,
  342. 'description' => $description,
  343. 'title_label' => 'Title',
  344. 'body_label' => 'Body',
  345. 'min_word_count' => '0',
  346. 'help' => '',
  347. 'node_options' => array(
  348. 'status' => 1,
  349. 'promote' => 1,
  350. 'sticky' => 0,
  351. 'revision' => 0,
  352. ),
  353. 'language_content_type' => '0',
  354. 'old_type' => $bundle,
  355. 'orig_type' => '',
  356. 'base' => 'node_content',
  357. 'custom' => '1',
  358. 'modified' => '1',
  359. 'locked' => '0',
  360. 'url_str' => str_replace('_', '-', $bundle),
  361. );
  362. // Allow overrides of these values.
  363. foreach ($type_settings as $key => $value) {
  364. $values[$key] = $value;
  365. }
  366. node_type_save((object) $values);
  367. // Add the body field.
  368. $trim_length = variable_get('teaser_length', 600);
  369. $field = field_info_field('body');
  370. $instance = array(
  371. 'field_name' => 'body',
  372. 'entity_type' => 'node',
  373. 'bundle' => $bundle,
  374. 'label' => t('Description'),
  375. 'widget' => array(
  376. 'type' => 'text_textarea_with_summary',
  377. 'settings' => array(
  378. 'rows' => 20,
  379. 'summary_rows' => 5,
  380. ),
  381. 'weight' => -4,
  382. 'module' => 'text',
  383. ),
  384. 'settings' => array(
  385. 'display_summary' => TRUE,
  386. ),
  387. 'display' => array(
  388. 'default' => array(
  389. 'label' => 'hidden',
  390. 'type' => 'text_default',
  391. ),
  392. 'teaser' => array(
  393. 'label' => 'hidden',
  394. 'type' => 'text_summary_or_trimmed',
  395. 'trim_length' => $trim_length,
  396. ),
  397. ),
  398. );
  399. field_create_instance($instance);
  400. }