date_tools.wizard.inc 12 KB

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