materio_showroom.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. // TODO: alter entity translation field permission with field_permission
  3. // __ __ _ _______ __ __
  4. // / / ____ _________ _/ /_(_)___ ____ / ____(_)__ / /___/ /
  5. // / / / __ \/ ___/ __ `/ __/ / __ \/ __ \ / /_ / / _ \/ / __ /
  6. // / /___/ /_/ / /__/ /_/ / /_/ / /_/ / / / / / __/ / / __/ / /_/ /
  7. // /_____/\____/\___/\__,_/\__/_/\____/_/ /_/ /_/ /_/\___/_/\__,_/
  8. /**
  9. * Implements hook_field_info().
  10. *
  11. * Provides the description of the field.
  12. */
  13. function materio_showroom_field_info() {
  14. return array(
  15. // We name our field as the associative name of the array.
  16. 'field_materio_showroom_location' => array(
  17. 'label' => t('Showroom Location'),
  18. 'description' => t('Define material location by showroom'),
  19. 'default_widget' => 'materio_showroom_location_text',
  20. 'default_formatter' => 'materio_showroom_location_simple_text',
  21. ),
  22. );
  23. // TODO: define in settings wich vocabulary is for showrooms
  24. }
  25. /**
  26. * Implements hook_field_validate().
  27. *
  28. * This hook gives us a chance to validate content that's in our
  29. * field. We're really only interested in the $items parameter, since
  30. * it holds arrays representing content in the field we've defined.
  31. * We want to verify that the items only contain RGB hex values like
  32. * this: #RRGGBB. If the item validates, we do nothing. If it doesn't
  33. * validate, we add our own error notification to the $errors parameter.
  34. *
  35. * @see field_example_field_widget_error()
  36. */
  37. // function materio_showroom_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  38. // foreach ($items as $delta => $item) {
  39. // // if (!empty($item['rgb'])) {
  40. // // if (!preg_match('@^#[0-9a-f]{6}$@', $item['rgb'])) {
  41. // // $errors[$field['field_name']][$langcode][$delta][] = array(
  42. // // 'error' => 'field_example_invalid',
  43. // // 'message' => t('Color must be in the HTML format #abcdef.'),
  44. // // );
  45. // // }
  46. // // }
  47. // }
  48. // }
  49. /**
  50. * Implements hook_field_is_empty().
  51. *
  52. * hook_field_is_empty() is where Drupal asks us if this field is empty.
  53. * Return TRUE if it does not contain data, FALSE if it does. This lets
  54. * the form API flag an error when required fields are empty.
  55. */
  56. function materio_showroom_field_is_empty($item, $field) {
  57. // dsm($item,'item');
  58. // dsm($field,'field');
  59. return empty($item['location']); // && empty($item['showroom_tid'])
  60. }
  61. // ______ __
  62. // / ____/___ _________ ___ ____ _/ /____ __________
  63. // / /_ / __ \/ ___/ __ `__ \/ __ `/ __/ _ \/ ___/ ___/
  64. // / __/ / /_/ / / / / / / / / /_/ / /_/ __/ / (__ )
  65. // /_/ \____/_/ /_/ /_/ /_/\__,_/\__/\___/_/ /____/
  66. /**
  67. * Implements hook_field_formatter_info().
  68. *
  69. * We need to tell Drupal that we have two different types of formatters
  70. * for this field. One will change the text color, and the other will
  71. * change the background color.
  72. *
  73. * @see field_example_field_formatter_view()
  74. */
  75. function materio_showroom_field_formatter_info() {
  76. return array(
  77. // This formatter just displays the hex value in the color indicated.
  78. 'materio_showroom_location_simple_text' => array(
  79. 'label' => t('Simple text-based formatter'),
  80. 'field types' => array('field_materio_showroom_location'),
  81. ),
  82. // This formatter changes the background color of the content region.
  83. // 'field_example_color_background' => array(
  84. // 'label' => t('Change the background of the output text'),
  85. // 'field types' => array('field_example_rgb'),
  86. // ),
  87. );
  88. }
  89. /**
  90. * Implements hook_field_formatter_view().
  91. *
  92. * Two formatters are implemented.
  93. * - field_example_simple_text just outputs markup indicating the color that
  94. * was entered and uses an inline style to set the text color to that value.
  95. * - field_example_color_background does the same but also changes the
  96. * background color of div.region-content.
  97. *
  98. * @see field_example_field_formatter_info()
  99. */
  100. function materio_showroom_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  101. $element = array();
  102. switch ($display['type']) {
  103. // This formatter simply outputs the field as text.
  104. case 'materio_showroom_location_simple_text':
  105. foreach ($items as $delta => $item) {
  106. if(!empty($item['location'])){
  107. $term = taxonomy_term_load($item['showroom_tid']);
  108. $element[$delta] = array(
  109. '#type' => 'html_tag',
  110. '#tag' => 'p',
  111. '#value' => t('%showroom : @loc', array('%showroom' => $term->name, '@loc' => $item['location'])),
  112. );
  113. }
  114. }
  115. break;
  116. }
  117. return $element;
  118. }
  119. // _ ___ __ __
  120. // | | / (_)___/ /___ ____ / /______
  121. // | | /| / / / __ / __ `/ _ \/ __/ ___/
  122. // | |/ |/ / / /_/ / /_/ / __/ /_(__ )
  123. // |__/|__/_/\__,_/\__, /\___/\__/____/
  124. // /____/
  125. /**
  126. * Implements hook_field_widget_info().
  127. *
  128. * Three widgets are provided.
  129. * - A simple text-only widget where the user enters the '#ffffff'.
  130. * - A 3-textfield widget that gathers the red, green, and blue values
  131. * separately.
  132. * - A farbtastic colorpicker widget that chooses the value graphically.
  133. *
  134. * These widget types will eventually show up in hook_field_widget_form,
  135. * where we will have to flesh them out.
  136. *
  137. * @see field_example_field_widget_form()
  138. */
  139. function materio_showroom_field_widget_info() {
  140. return array(
  141. 'materio_showroom_location_text' => array(
  142. 'label' => t('Location as text'),
  143. 'field types' => array('field_materio_showroom_location'),
  144. ),
  145. );
  146. }
  147. /**
  148. * Implements hook_field_widget_form().
  149. *
  150. * hook_widget_form() is where Drupal tells us to create form elements for
  151. * our field's widget.
  152. *
  153. * We provide one of three different forms, depending on the widget type of
  154. * the Form API item provided.
  155. *
  156. * The 'field_example_colorpicker' and 'field_example_text' are essentially
  157. * the same, but field_example_colorpicker adds a javascript colorpicker
  158. * helper.
  159. *
  160. * field_example_3text displays three text fields, one each for red, green,
  161. * and blue. However, the field type defines a single text column,
  162. * rgb, which needs an HTML color spec. Define an element validate
  163. * handler that converts our r, g, and b fields into a simulated single
  164. * 'rgb' form element.
  165. */
  166. function materio_showroom_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  167. $widget = $element;
  168. $widget['#delta'] = $delta;
  169. switch ($instance['widget']['type']) {
  170. case 'materio_showroom_location_text':
  171. $locval = isset($items[$delta]['location']) ? $items[$delta]['location'] : '';
  172. $showroom_tid = isset($items[$delta]['showroom_tid']) ? $items[$delta]['showroom_tid'] : 0;
  173. $widget['showroom_tid'] = array(
  174. '#type' => 'hidden',
  175. '#default_value' => $showroom_tid,
  176. // Allow a slightly larger size that the field length to allow for some
  177. // configurations where all characters won't fit in input field.
  178. '#maxlength' => 255,
  179. );
  180. $widget['location'] = array(
  181. '#type' => 'textfield',
  182. '#title' => "Showroom", //$showroom_term->name,
  183. '#default_value' => $locval,
  184. // Allow a slightly larger size that the field length to allow for some
  185. // configurations where all characters won't fit in input field.
  186. '#size' => 20,
  187. '#maxlength' => 255,
  188. );
  189. $widget['#attached'] = array(
  190. // Add javascript to remove the draggable behaviour.
  191. 'js' => array(drupal_get_path('module', 'materio_showroom') . '/js/materio_showroom.js'),
  192. );
  193. break;
  194. }
  195. return $widget;
  196. }
  197. /**
  198. * Implements hook_field_widget_settings_form().
  199. */
  200. function materio_showroom_field_widget_settings_form($field, $instance) {
  201. $widget = $instance['widget'];
  202. $settings = $widget['settings'];
  203. $vocs = taxonomy_get_vocabularies();
  204. $options = array();
  205. foreach ($vocs as $vid => $voc) {
  206. $options[$vid] = $voc->name;
  207. }
  208. $form['vocabulary'] = array(
  209. '#type' => 'select',
  210. '#title' => t('Vocabulary'),
  211. '#default_value' => $settings['vocabulary'],
  212. // '#element_validate' => array('_tode_widget_settings_maxlength_validate'),
  213. '#required' => TRUE,
  214. '#description' => t('Choose which vocabulary will be associated as showroom.'),
  215. '#options' => $options,
  216. );
  217. return $form;
  218. }
  219. /**
  220. * Implements hook_form_alter().
  221. */
  222. function materio_showroom_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  223. if($form['#field']['type'] == "field_materio_showroom_location"){
  224. // dsm($form, 'form');
  225. $form['field']['cardinality']['#disabled'] = 'true';
  226. $form['field']['cardinality']['#default_value'] = -1;
  227. }
  228. //
  229. }
  230. /**
  231. * Implements hook_form_alter().
  232. */
  233. function materio_showroom_form_alter(&$form, &$form_state, $form_id) {
  234. // dsm($form_id);
  235. // act only on node edit form
  236. if(isset($form['#node_edit_form'])){
  237. // dsm($form_id, 'form_id');
  238. // dsm($form, 'form');
  239. // dsm($form_state, 'form_state');
  240. _materio_showroom_alter_location_field_form($form, $form_state, $form_id);
  241. }
  242. }
  243. function _materio_showroom_alter_location_field_form(&$form, &$form_state, $form_id){
  244. // define some constants
  245. $field_type = 'field_materio_showroom_location';
  246. $node = $form['#node'];
  247. $nodetype = $form['type']['#value'];
  248. // dsm($form_state, 'form_state');
  249. global $user;
  250. $user = user_load($user->uid); // Make sure the user object is fully loaded
  251. // dsm($user, 'user');
  252. $fieldsmap = field_info_field_map();
  253. $showroomfieldinstances = array();
  254. $user_termref_field_instances = array();
  255. foreach ($fieldsmap as $field_name => $field) {
  256. // dsm($field,$field_name);
  257. // get all (probably one :p) showroom field instances from the current node
  258. if ($field['type'] == $field_type
  259. && isset($field['bundles']['node'])
  260. && in_array($nodetype, $field['bundles']['node'])) {
  261. $showroomfieldinstances[] = $field_name;
  262. }
  263. // get all term reference fields instance from users
  264. if (isset($field['bundles']['user'])
  265. && $field['type'] == 'taxonomy_term_reference') {
  266. $field_info = field_info_field($field_name);
  267. // dsm($field_info, "field_info");
  268. $user_termref_field_instances[$field_info['settings']['allowed_values'][0]['vocabulary']] = $field_name;
  269. }
  270. }
  271. // dsm($showroomfieldinstances, 'showroomfieldinstances');
  272. // dsm($user_termref_field_instances, 'user_termref_field_instances');
  273. // if there is no showroom field instances do nothing
  274. if(!empty($showroomfieldinstances)){
  275. // act on each field instance
  276. foreach ($showroomfieldinstances as $field_name) {
  277. // retrive various field infos
  278. $field_info = field_info_field($field_name);
  279. // dsm($field_info, 'field_info');
  280. $field_instance = field_info_instance('node', $field_name, $nodetype);
  281. // get all terms from chosen vocabulary in field instance widget settings
  282. $vid = $field_instance['widget']['settings']['vocabulary'];
  283. $voc = taxonomy_vocabulary_load($vid);
  284. $tree = taxonomy_get_tree($vid);
  285. // dsm($tree, 'tree');
  286. foreach ($tree as $key => $term) {
  287. $terms[$term->tid] = $term->name;
  288. }
  289. // dsm($terms, 'terms');
  290. // get user own term for current vocabulary (if any)
  291. if(isset($user_termref_field_instances[$voc->machine_name])){
  292. $user_field_name = $user_termref_field_instances[$voc->machine_name];
  293. foreach (field_get_items('user', $user, $user_field_name) as $key => $value) {
  294. $user_showrooms[] = $value['tid'];
  295. }
  296. // dsm($user_showrooms, "user_showrooms");
  297. }
  298. // get already recorded values
  299. $old_items = field_get_items('node', $node, $field_name);
  300. // dsm($old_items, 'old_items');
  301. foreach ($old_items as $i => $value) {
  302. $values[$value['showroom_tid']] = $value['location'];
  303. }
  304. // dsm($values, 'values');
  305. // build new item list
  306. // for ($k=0; $k < 3; $k++) {
  307. foreach ($terms as $tid => $name) {
  308. $items[] = array(
  309. 'showroom_tid' => "$tid",
  310. 'location' => isset($values[$tid]) ? $values[$tid] : ''
  311. );
  312. }
  313. // }
  314. // dsm($items, 'items');
  315. // change form_state items count
  316. // without this, form wont populated with all wanted items
  317. $form_state['field'][$field_name][LANGUAGE_NONE]['items_count'] = count($items);
  318. // retrieve new field form with our custom items
  319. $new_field_form = field_default_form('node', null, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state)[$field_name];
  320. // dsm($new_field_form, 'new_field_form');
  321. // $elements = field_multiple_value_form($field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state);
  322. // dsm($elements, 'elements');
  323. // change items location field title
  324. // and check access comparing term id with user taged with
  325. $i = 0;
  326. foreach ($terms as $tid => $name) {
  327. $item = $new_field_form[LANGUAGE_NONE][$i];
  328. $new_field_form[LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']];
  329. $disabled = !user_access('materio showroom edit all samples fields', $user) && !in_array($tid, $user_showrooms);
  330. $new_field_form[LANGUAGE_NONE][$i]['location']['#disabled'] = $disabled;
  331. $i++;
  332. }
  333. // remove the last one more item added by default
  334. unset($new_field_form[LANGUAGE_NONE][$i]);
  335. // remove "add more" button
  336. unset($new_field_form[LANGUAGE_NONE]['add_more']);
  337. // dsm($new_field_form, 'new_field_form');
  338. // delete normal field form and replace it with our new custom field form
  339. unset($form[$field_name]);
  340. $form[$field_name] = $new_field_form;
  341. }
  342. }
  343. }
  344. /**
  345. * Implements hook_permission().
  346. */
  347. function materio_showroom_permission() {
  348. return array(
  349. 'materio showroom edit all samples fields' => array(
  350. 'title' => t('Edit all materiO showroom samples fields'),
  351. 'description' => t('Edit all materiO showroom samples fields'),
  352. ),
  353. 'materio showroom migrate fields' => array(
  354. 'title' => t('Migrate materio showroom location fields'),
  355. 'description' => t('Migrate materio showroom location fields'),
  356. ),
  357. );
  358. }
  359. /**
  360. * Implements hook_menu_local_tasks_alter().
  361. */
  362. function materio_showroom_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  363. switch($root_path){
  364. case 'admin/content/showrooms' :
  365. $item = menu_get_item('node/add/showroom');
  366. if ($item['access']) {
  367. $data['actions']['output'][] = array(
  368. '#theme' => 'menu_local_action',
  369. '#link' => $item,
  370. );
  371. }
  372. // TODO: prepopulate user role and showroom taxo (https://www.drupal.org/project/prepopulate)
  373. // $item = menu_get_item('admin/people/create');
  374. // if ($item['access']) {
  375. // $data['actions']['output'][] = array(
  376. // '#theme' => 'menu_local_action',
  377. // '#link' => $item,
  378. // );
  379. // }
  380. break;
  381. }
  382. }
  383. // __ ____ __ _
  384. // / |/ (_)___ __________ _/ /_(_)___ ____
  385. // / /|_/ / / __ `/ ___/ __ `/ __/ / __ \/ __ \
  386. // / / / / / /_/ / / / /_/ / /_/ / /_/ / / / /
  387. // /_/ /_/_/\__, /_/ \__,_/\__/_/\____/_/ /_/
  388. // /____/
  389. /**
  390. * Implements hook_menu().
  391. */
  392. function materio_showroom_menu() {
  393. $items['admin/config/content/materio/migrate_showroom'] = array(
  394. 'title' => 'Showrooms location fields migration settings',
  395. 'page callback' => 'drupal_get_form',
  396. 'page arguments' => array('materio_showroom_migrate_location_fields_settings_form'),
  397. 'access arguments' => array('materio showroom migrate fields'),
  398. 'type' => MENU_LOCAL_TASK,
  399. // 'file' => ,
  400. );
  401. return $items;
  402. }
  403. function materio_showroom_migrate_location_fields_settings_form(){
  404. drupal_set_title('Showroom Migration settings', PASS_THROUGH);
  405. $field_type = 'field_materio_showroom_location';
  406. $types = node_type_get_types();
  407. // dsm($types, 'types');
  408. $nt_options = array();
  409. foreach ($types as $mn => $type) {
  410. $nt_options[$mn] = $type->name;
  411. }
  412. $node_type = variable_get('materio_showroom_migrate_node_type', null);
  413. // source field (must be a text field)
  414. $form['node_type'] = array(
  415. '#type'=>'select',
  416. '#options'=>$nt_options,
  417. '#default_value' => $node_type,
  418. '#title' => t('Content type'),
  419. '#multiple' => false,
  420. );
  421. if($node_type){
  422. $fieldsmap = field_info_field_map();
  423. $src_options = array();
  424. $target_options = array();
  425. foreach ($fieldsmap as $field_name => $field) {
  426. // dsm($field, $field_name);
  427. if ($field['type'] == 'text'
  428. && isset($field['bundles']['node'])
  429. && in_array($node_type, $field['bundles']['node'])) {
  430. $src_options[$field_name] = $field_name;
  431. }
  432. if ($field['type'] == $field_type
  433. && isset($field['bundles']['node'])
  434. && in_array($node_type, $field['bundles']['node'])) {
  435. $target_options[$field_name] = $field_name;
  436. }
  437. }
  438. // source field (must be a text field)
  439. $form['source_field'] = array(
  440. '#type'=>'select',
  441. '#options'=>$src_options,
  442. '#default_value' => variable_get('materio_showroom_migrate_source_field', null),
  443. '#title' => t('source field (must be a text field)'),
  444. '#multiple' => false,
  445. );
  446. // target field (must be a showroom location field)
  447. $form['target_field'] = array(
  448. '#type'=>'select',
  449. '#options'=>$target_options,
  450. '#default_value' => variable_get('materio_showroom_migrate_target_field', null),
  451. '#title' => t('target field (must be a showroom location field)'),
  452. '#multiple' => false,
  453. );
  454. $vocs = taxonomy_get_vocabularies();
  455. $voc_options = array();
  456. foreach ($vocs as $vid => $voc) {
  457. $voc_options[$vid] = $voc->name;
  458. }
  459. // vocabulary for showrooms
  460. $vid = variable_get('materio_showroom_migrate_vocabulary', null);
  461. $form['vocabulary'] = array(
  462. '#type'=>'select',
  463. '#options'=>$voc_options,
  464. '#default_value' => $vid,
  465. '#title' => t('vocabulary for showrooms'),
  466. '#multiple' => false,
  467. );
  468. // default taxonomy term
  469. if($vid){
  470. $tree = taxonomy_get_tree($vid);
  471. foreach ($tree as $key => $term) {
  472. $terms_options[$term->tid] = $term->name;
  473. }
  474. $term = variable_get('materio_showroom_migrate_default_term', null);
  475. $form['default_term'] = array(
  476. '#type'=>'select',
  477. '#options'=>$terms_options,
  478. '#default_value' => $term,
  479. '#title' => t('default taxonomy term'),
  480. '#multiple' => false,
  481. );
  482. if($term){
  483. $form['migrate'] = array(
  484. '#type' => 'submit',
  485. '#value' => 'Migrate',
  486. // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
  487. );
  488. }
  489. }
  490. }
  491. $form['submit'] = array(
  492. '#type' => 'submit',
  493. '#value' => 'Save',
  494. // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
  495. );
  496. return $form;
  497. }
  498. function materio_showroom_migrate_location_fields_settings_form_submit($form, &$form_state){
  499. // dsm($form_state, 'form_state');
  500. $node_type = $form_state['values']['node_type'];
  501. $src_field = $form_state['values']['source_field'];
  502. $target_field = $form_state['values']['target_field'];
  503. $vid = $form_state['values']['vocabulary'];
  504. $default_term = $form_state['values']['default_term'];
  505. variable_set('materio_showroom_migrate_node_type', $node_type);
  506. variable_set('materio_showroom_migrate_source_field', $src_field);
  507. variable_set('materio_showroom_migrate_target_field', $target_field);
  508. variable_set('materio_showroom_migrate_vocabulary', $vid);
  509. variable_set('materio_showroom_migrate_default_term', $default_term);
  510. // dsm($node_type, 'node_type');
  511. // dsm($src_field,"src_field");
  512. // dsm($target_field,"target_field");
  513. // dsm($vid,"vid");
  514. // dsm($default_term,"default_term");
  515. if ($form_state['values']['op'] == 'Migrate'){
  516. _materio_showroom_batch_migration($node_type, $src_field, $target_field, $vid, $default_term);
  517. }
  518. // $query = "INSERT INTO field_data_field_screenshot SELECT a.* FROM field_data_field_image a LEFT JOIN field_data_field_screenshot b ON a.entity_id = b.entity_id AND a.entity_type = b.entity_type WHERE b.entity_id IS NULL";
  519. //
  520. // $query2 = "INSERT INTO field_revision_field_screenshot SELECT a.* FROM field_revision_field_image a LEFT JOIN field_revision_field_screenshot b ON a.entity_id = b.entity_id AND a.entity_type = b.entity_type WHERE b.entity_id IS NULL";
  521. }
  522. function _materio_showroom_batch_migration($node_type, $src_field, $target_field, $vid, $tid){
  523. // Reset counter for debug information.
  524. $_SESSION['http_request_count'] = 0;
  525. $batch = array(
  526. 'title' => t('Migrating Showroom fields ...'),
  527. 'operations' => array(),
  528. 'init_message' => t('Commencing'),
  529. 'progress_message' => t('Processed @current out of @total.'),
  530. 'error_message' => t('An error occurred during processing'),
  531. 'finished' => '_materio_showroom_batch_migration_finished',
  532. );
  533. $query = new EntityFieldQuery();
  534. $query->entityCondition('entity_type', 'node')
  535. ->entityCondition('bundle', $node_type)
  536. ->fieldCondition($src_field, 'value', '', '!=');
  537. $result = $query->execute();
  538. // dsm($result, 'result');
  539. if (isset($result['node'])) {
  540. $voc = taxonomy_vocabulary_load($vid);
  541. $tree = taxonomy_get_tree($vid);
  542. foreach ($tree as $key => $term) {
  543. $terms[$term->tid] = $term->name;
  544. }
  545. if(!empty($terms)){
  546. foreach ($result['node'] as $nid => $value) {
  547. $batch['operations'][] = array(
  548. '_materio_showroom_batch_migration_op',
  549. array(
  550. $nid,
  551. $src_field, $target_field,
  552. $terms, $tid,
  553. )
  554. );
  555. }
  556. }
  557. }
  558. batch_set($batch);
  559. }
  560. function _materio_showroom_batch_migration_op($nid, $src_field, $target_field, $terms, $default_tid, &$context){
  561. $context['results']['field_migrated']++;
  562. $node = node_load($nid);
  563. $src_items = field_get_items('node', $node, $src_field);
  564. $src_value = $src_items ? $src_items[0]['value'] : '';
  565. // $src_value = str_replace('/', '-', $src_value);
  566. //
  567. // build new item list
  568. $items = array(LANGUAGE_NONE => array());
  569. foreach ($terms as $tid => $name) {
  570. $items[LANGUAGE_NONE][] = array(
  571. 'showroom_tid' => $tid,
  572. 'location' => $default_tid == $tid ? $src_value : '',
  573. );
  574. }
  575. $node->$target_field = $items;
  576. node_save($node);
  577. //Simply show the import row count.
  578. $context['message'] = t('Migrating node !c : %title (%nid)', array(
  579. '!c' => $context['results']['field_migrated'],
  580. '%title'=>$node->title,
  581. '%nid'=>$nid ));
  582. // In order to slow importing and debug better,
  583. // we can uncomment this line to make each import slightly slower.
  584. // usleep(2500);
  585. if ( false ) {
  586. $context['results']['failed_nodes'][] = $nid ;
  587. }
  588. _materio_showroom_update_http_requests();
  589. }
  590. function _materio_showroom_batch_migration_finished($success, $results, $operations){
  591. // dsm($success, 'success');
  592. // dsm($results, 'results');
  593. // dsm($operations, 'operations');
  594. if ($success) {
  595. // Here we could do something meaningful with the results.
  596. // We just display the number of nodes we processed...
  597. drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _materio_showroom_get_http_requests())));
  598. drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
  599. }
  600. else {
  601. // An error occurred.
  602. // $operations contains the operations that remained unprocessed.
  603. drupal_set_message(
  604. t('operations : @args',
  605. array(
  606. '@args' => print_r(current($operations), TRUE),
  607. )
  608. ),
  609. 'error'
  610. );
  611. $error_operation = reset($operations);
  612. drupal_set_message(
  613. t('An error occurred while processing @operation with arguments : @args',
  614. array(
  615. '@operation' => $error_operation[0],
  616. '@args' => print_r($error_operation[0], TRUE),
  617. )
  618. ),
  619. 'error'
  620. );
  621. }
  622. }
  623. /**
  624. * Utility function to increment HTTP requests in a session variable.
  625. */
  626. function _materio_showroom_update_http_requests() {
  627. $_SESSION['http_request_count']++;
  628. }
  629. /**
  630. * Utility function to count the HTTP requests in a session variable.
  631. *
  632. * @return int
  633. * Number of requests.
  634. */
  635. function _materio_showroom_get_http_requests() {
  636. return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
  637. }