cer.admin.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Form builder to create or edit presets.
  4. */
  5. function cer_form(array $form, array &$form_state, CerPreset $preset, $do = 'edit', $entity_type = NULL) {
  6. field_attach_form('cer', $preset, $form, $form_state);
  7. $form['cer_right']['#id'] = 'edit-cer-right';
  8. $form['cer_left'][LANGUAGE_NONE]['#ajax'] = array(
  9. 'callback' => 'cer_update_right',
  10. 'wrapper' => $form['cer_right']['#id'],
  11. );
  12. $form['actions'] = array(
  13. 'submit' => array(
  14. '#type' => 'submit',
  15. '#value' => t('Save'),
  16. ),
  17. '#weight' => 10,
  18. );
  19. return $form;
  20. }
  21. function cer_form_submit(array &$form, array &$form_state) {
  22. $preset = entity_ui_form_submit_build_entity($form, $form_state)->save();
  23. drupal_set_message(t('The preset has been saved.'));
  24. $form_state['redirect'] = 'admin/config/content/cer';
  25. }
  26. function cer_update_right(array &$form, array &$form_state) {
  27. $preset = $form_state['build_info']['args'][0];
  28. field_attach_submit('cer', $preset, $form, $form_state, array('field_name' => 'cer_left'));
  29. $left = $preset->wrapper->cer_left->chain->value();
  30. $left_identifier = $left->__toString();
  31. $left_re = $left->regex();
  32. $hierarchy = new FieldHierarchy();
  33. foreach (CerFieldChain::collectAll() as $chain) {
  34. if (preg_match($left_re, $chain->__toString()) && preg_match($chain->regex(), $left_identifier)) {
  35. $hierarchy->addChain($chain);
  36. }
  37. }
  38. $options = $hierarchy->options();
  39. if ($options) {
  40. $form['cer_right'][LANGUAGE_NONE]['#options'] = $options;
  41. }
  42. else {
  43. drupal_set_message(t('There are no fields which can correspond with your selection.'), 'warning');
  44. }
  45. return $form['cer_right'];
  46. }
  47. /**
  48. * Enables or disables a preset, depending on its current status. This callback
  49. * is defined by CerUIController::hook_menu().
  50. */
  51. function cer_toggle_preset(CerPreset $preset) {
  52. $preset->wrapper->cer_enabled->set(! $preset->wrapper->cer_enabled->value());
  53. $preset->save();
  54. drupal_goto(isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/content/cer');
  55. }
  56. /**
  57. * Permanently inverts a preset (i.e., deletes the original). This callback
  58. * is defined by CerUIController::hook_menu().
  59. */
  60. function cer_invert_preset(CerPreset $original) {
  61. $original->invert()->save();
  62. $original->delete();
  63. drupal_goto(isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/content/cer');
  64. }
  65. /**
  66. * Allows batch updating of existing entities.
  67. */
  68. function cer_bulk_update_form(array $form, array &$form_state) {
  69. $form['type'] = array(
  70. '#type' => 'select',
  71. '#title' => t('Entity type'),
  72. '#required' => TRUE,
  73. '#options' => array(),
  74. '#description' => t('Select the entity type that you want to update.'),
  75. );
  76. foreach (entity_get_info() as $type => $class) {
  77. $form['type']['#options'][$type] = $class['label'];
  78. }
  79. $form['submit'] = array(
  80. '#type' => 'submit',
  81. '#value' => t('Submit'),
  82. );
  83. return $form;
  84. }
  85. /**
  86. * The update form. Allows bulk updating of current entities.
  87. */
  88. function cer_bulk_update_form_submit(array $form, array &$form_state) {
  89. $batch = array(
  90. 'finished' => 'cer_batch_update_existing_finished',
  91. 'title' => t('Processing'),
  92. 'init_message' => t('Preparing to update corresponding entity references for existing entities...'),
  93. 'progress_message' => t('Processing entities...'),
  94. 'error_message' => t('Corresponding entity references - existing entity update has encountered an error.'),
  95. 'operations' => array(),
  96. );
  97. $entity_type = $form_state['values']['type'];
  98. $query = new EntityFieldQuery();
  99. $query->entityCondition('entity_type', $entity_type);
  100. if ($entity_type == 'user') {
  101. $query->entityCondition('entity_id', 0, '>');
  102. }
  103. $result = $query->execute();
  104. if (isset($result[$entity_type])) {
  105. foreach (array_keys($result[$entity_type]) as $entity_id) {
  106. $batch['operations'][] = array('cer_processing_entity', array('update', $entity_id, $entity_type));
  107. }
  108. }
  109. batch_set($batch);
  110. }