materio_showroom.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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_example_rgb'),
  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 and with a color.
  104. case 'materio_showroom_location_simple_text':
  105. foreach ($items as $delta => $item) {
  106. $term = taxonomy_term_load($item['showroom_tid']);
  107. $element[$delta] = array(
  108. '#type' => 'html_tag',
  109. '#tag' => 'p',
  110. '#value' => t('%showroom : @loc', array('%showroom' => $term->name, '@loc' => $item['location'])),
  111. );
  112. }
  113. break;
  114. }
  115. return $element;
  116. }
  117. // _ ___ __ __
  118. // | | / (_)___/ /___ ____ / /______
  119. // | | /| / / / __ / __ `/ _ \/ __/ ___/
  120. // | |/ |/ / / /_/ / /_/ / __/ /_(__ )
  121. // |__/|__/_/\__,_/\__, /\___/\__/____/
  122. // /____/
  123. /**
  124. * Implements hook_field_widget_info().
  125. *
  126. * Three widgets are provided.
  127. * - A simple text-only widget where the user enters the '#ffffff'.
  128. * - A 3-textfield widget that gathers the red, green, and blue values
  129. * separately.
  130. * - A farbtastic colorpicker widget that chooses the value graphically.
  131. *
  132. * These widget types will eventually show up in hook_field_widget_form,
  133. * where we will have to flesh them out.
  134. *
  135. * @see field_example_field_widget_form()
  136. */
  137. function materio_showroom_field_widget_info() {
  138. return array(
  139. 'materio_showroom_location_text' => array(
  140. 'label' => t('Location as text'),
  141. 'field types' => array('field_materio_showroom_location'),
  142. ),
  143. );
  144. }
  145. /**
  146. * Implements hook_field_widget_form().
  147. *
  148. * hook_widget_form() is where Drupal tells us to create form elements for
  149. * our field's widget.
  150. *
  151. * We provide one of three different forms, depending on the widget type of
  152. * the Form API item provided.
  153. *
  154. * The 'field_example_colorpicker' and 'field_example_text' are essentially
  155. * the same, but field_example_colorpicker adds a javascript colorpicker
  156. * helper.
  157. *
  158. * field_example_3text displays three text fields, one each for red, green,
  159. * and blue. However, the field type defines a single text column,
  160. * rgb, which needs an HTML color spec. Define an element validate
  161. * handler that converts our r, g, and b fields into a simulated single
  162. * 'rgb' form element.
  163. */
  164. function materio_showroom_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  165. $locval = isset($items[$delta]['location']) ? $items[$delta]['location'] : '';
  166. $showroom_tid = isset($items[$delta]['showroom_tid']) ? $items[$delta]['showroom_tid'] : 0;
  167. $widget = $element;
  168. $widget['#delta'] = $delta;
  169. switch ($instance['widget']['type']) {
  170. // TODO: loop through showrooms and don't allow more than one field by show room
  171. case 'materio_showroom_location_text':
  172. $widget['showroom_tid'] = array(
  173. '#type' => 'hidden',
  174. '#default_value' => $showroom_tid,
  175. // Allow a slightly larger size that the field length to allow for some
  176. // configurations where all characters won't fit in input field.
  177. '#size' => 10,
  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' => 10,
  187. '#maxlength' => 255,
  188. );
  189. break;
  190. }
  191. return $widget;
  192. }
  193. /**
  194. * Implements hook_field_widget_settings_form().
  195. */
  196. function materio_showroom_field_widget_settings_form($field, $instance) {
  197. $widget = $instance['widget'];
  198. $settings = $widget['settings'];
  199. $vocs = taxonomy_get_vocabularies();
  200. $options = array();
  201. foreach ($vocs as $vid => $voc) {
  202. $options[$vid] = $voc->name;
  203. }
  204. $form['vocabulary'] = array(
  205. '#type' => 'select',
  206. '#title' => t('Vocabulary'),
  207. '#default_value' => $settings['vocabulary'],
  208. // '#element_validate' => array('_tode_widget_settings_maxlength_validate'),
  209. '#required' => TRUE,
  210. '#description' => t('Choose which vocabulary will be associated as showroom.'),
  211. '#options' => $options,
  212. );
  213. return $form;
  214. }
  215. /**
  216. * Implements hook_form_alter().
  217. */
  218. function materio_showroom_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  219. if($form['#field']['type'] == "field_materio_showroom_location"){
  220. // dsm($form, 'form');
  221. $form['field']['cardinality']['#disabled'] = 'true';
  222. $form['field']['cardinality']['#default_value'] = -1;
  223. }
  224. //
  225. }
  226. /**
  227. * Implements hook_form_alter().
  228. */
  229. function materio_showroom_form_alter(&$form, &$form_state, $form_id) {
  230. // act only on node edit form
  231. if(isset($form['#node_edit_form'])){
  232. // dsm($form_id, 'form_id');
  233. // dsm($form, 'form');
  234. // dsm($form_state, 'form_state');
  235. _materio_showroom_alter_location_field_form($form, $form_state, $form_id);
  236. }
  237. }
  238. function _materio_showroom_alter_location_field_form(&$form, &$form_state, $form_id){
  239. // define some constants
  240. $field_type = 'field_materio_showroom_location';
  241. $node = $form['#node'];
  242. $nodetype = $form['type']['#value'];
  243. global $user;
  244. $user = user_load($user->uid); // Make sure the user object is fully loaded
  245. // dsm($user, 'user');
  246. $fieldsmap = field_info_field_map();
  247. $showroomfieldinstances = array();
  248. $user_termref_field_instances = array();
  249. foreach ($fieldsmap as $field_name => $field) {
  250. // dsm($field,$field_name);
  251. // get all (probably one :p) showroom field instances from the current node
  252. if ($field['type'] == $field_type
  253. && isset($field['bundles']['node'])
  254. && in_array($nodetype, $field['bundles']['node'])) {
  255. $showroomfieldinstances[] = $field_name;
  256. }
  257. // get all term reference fields instance from users
  258. if (isset($field['bundles']['user'])
  259. && $field['type'] == 'taxonomy_term_reference') {
  260. $field_info = field_info_field($field_name);
  261. // dsm($field_info, "field_info");
  262. $user_termref_field_instances[$field_info['settings']['allowed_values'][0]['vocabulary']] = $field_name;
  263. }
  264. }
  265. // dsm($showroomfieldinstances, 'showroomfieldinstances');
  266. // dsm($user_termref_field_instances, 'user_termref_field_instances');
  267. // if there is no showroom field instances do nothing
  268. if(!empty($showroomfieldinstances)){
  269. // act on each field instance
  270. foreach ($showroomfieldinstances as $field_name) {
  271. // retrive various field infos
  272. $field_info = field_info_field($field_name);
  273. $field_instance = field_info_instance('node', $field_name, $nodetype);
  274. // dsm($field_info, 'field_info');
  275. // get all terms from chosen vocabulary in field instance widget settings
  276. $vid = $field_instance['widget']['settings']['vocabulary'];
  277. $voc = taxonomy_vocabulary_load($vid);
  278. $tree = taxonomy_get_tree($vid);
  279. foreach ($tree as $key => $term) {
  280. $terms[$term->tid] = $term->name;
  281. }
  282. // dsm($terms, 'terms');
  283. // get user own term for current vocabulary (if any)
  284. if(isset($user_termref_field_instances[$voc->machine_name])){
  285. $user_field_name = $user_termref_field_instances[$voc->machine_name];
  286. foreach (field_get_items('user', $user, $user_field_name) as $key => $value) {
  287. $user_showrooms[] = $value['tid'];
  288. }
  289. // dsm($user_showrooms, "user_showrooms");
  290. }
  291. // get already recorded values
  292. $old_items = field_get_items('node', $node, $field_name);
  293. foreach ($old_items as $i => $value) {
  294. $values[$value['showroom_tid']] = $value['location'];
  295. }
  296. // dsm($values, 'values');
  297. // build new item list
  298. foreach ($terms as $tid => $name) {
  299. $items[] = array(
  300. 'showroom_tid' => $tid,
  301. 'location' => isset($values[$tid]) ? $values[$tid] : ''
  302. );
  303. }
  304. // dsm($items, 'items');
  305. // retrieve new field form with our custom items
  306. $new_field_form = field_default_form('node', $node, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state);
  307. // dsm($new_field_form, 'default_form');
  308. // change items location field title
  309. // and check access comparing term id with user taged with
  310. $i = 0;
  311. foreach ($terms as $tid => $name) {
  312. $item = $new_field_form[$field_name][LANGUAGE_NONE][$i];
  313. $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']];
  314. $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#disabled'] = !in_array($tid, $user_showrooms);
  315. $i++;
  316. }
  317. // remove the last one more item added by default
  318. unset($new_field_form[$field_name][LANGUAGE_NONE][$i]);
  319. // delete normal field form and replace it with our new custom field form
  320. unset($form[$field_name]);
  321. $form[$field_name] = $new_field_form[$field_name];
  322. // remove "add more" button
  323. unset($form[$field_name]['und']['add_more']);
  324. }
  325. }
  326. }
  327. // TODO: migrate old location field to new one
  328. /**
  329. * Implements hook_permission().
  330. */
  331. function materio_showroom_permission() {
  332. return array(
  333. 'materio showroom migrate fields' => array(
  334. 'title' => t('Migrate materio showroom location fields'),
  335. 'description' => t('Migrate materio showroom location fields'),
  336. ),
  337. );
  338. }
  339. // __ ____ __ _
  340. // / |/ (_)___ __________ _/ /_(_)___ ____
  341. // / /|_/ / / __ `/ ___/ __ `/ __/ / __ \/ __ \
  342. // / / / / / /_/ / / / /_/ / /_/ / /_/ / / / /
  343. // /_/ /_/_/\__, /_/ \__,_/\__/_/\____/_/ /_/
  344. // /____/
  345. /**
  346. * Implements hook_menu().
  347. */
  348. function materio_showroom_menu() {
  349. $items['admin/config/content/materio_showroom'] = array(
  350. 'title' => 'Showrooms location fields migration settings',
  351. 'page callback' => 'drupal_get_form',
  352. 'page arguments' => array('materio_showroom_migrate_location_fields_settings_form'),
  353. 'access arguments' => array('materio showroom migrate fields'),
  354. 'type' => MENU_LOCAL_ACTION,
  355. // 'file' => ,
  356. );
  357. // $items['admin/config/content/materio_showroom/migrate'] = array(
  358. // 'title' => 'Migrate showrooms location fields',
  359. // 'page callback' => 'materio_showroom_migrate_location_fields',
  360. // // 'page arguments' => array('materio_showroom_migrate_location_fields'),
  361. // 'access arguments' => array('materio showroom migrate fields'),
  362. // 'type' => MENU_LOCAL_TASK,
  363. // // 'file' => ,
  364. // );
  365. return $items;
  366. }
  367. function materio_showroom_migrate_location_fields_settings_form(){
  368. drupal_set_title('Showroom Migration settings', PASS_THROUGH);
  369. $field_type = 'field_materio_showroom_location';
  370. $types = node_type_get_types();
  371. // dsm($types, 'types');
  372. $nt_options = array();
  373. foreach ($types as $mn => $type) {
  374. $nt_options[$mn] = $type->name;
  375. }
  376. $node_type = variable_get('materio_showroom_migrate_node_type', null);
  377. // source field (must be a text field)
  378. $form['node_type'] = array(
  379. '#type'=>'select',
  380. '#options'=>$nt_options,
  381. '#default_value' => $node_type,
  382. '#title' => t('source field (must be a text field)'),
  383. '#multiple' => false,
  384. );
  385. if($node_type){
  386. $fieldsmap = field_info_field_map();
  387. $src_options = array();
  388. $target_options = array();
  389. foreach ($fieldsmap as $field_name => $field) {
  390. // dsm($field, $field_name);
  391. if ($field['type'] == 'text'
  392. && isset($field['bundles']['node'])
  393. && in_array($node_type, $field['bundles']['node'])) {
  394. $src_options[$field_name] = $field_name;
  395. }
  396. if ($field['type'] == $field_type
  397. && isset($field['bundles']['node'])
  398. && in_array($node_type, $field['bundles']['node'])) {
  399. $target_options[$field_name] = $field_name;
  400. }
  401. }
  402. // source field (must be a text field)
  403. $form['source_field'] = array(
  404. '#type'=>'select',
  405. '#options'=>$src_options,
  406. '#default_value' => variable_get('materio_showroom_migrate_source_field', null),
  407. '#title' => t('source field (must be a text field)'),
  408. '#multiple' => false,
  409. );
  410. // target field (must be a showroom location field)
  411. $form['target_field'] = array(
  412. '#type'=>'select',
  413. '#options'=>$target_options,
  414. '#default_value' => variable_get('materio_showroom_migrate_target_field', null),
  415. '#title' => t('target field (must be a showroom location field)'),
  416. '#multiple' => false,
  417. );
  418. $vocs = taxonomy_get_vocabularies();
  419. $voc_options = array();
  420. foreach ($vocs as $vid => $voc) {
  421. $voc_options[$vid] = $voc->name;
  422. }
  423. // vocabulary for showrooms
  424. $vid = variable_get('materio_showroom_migrate_vocabulary', null);
  425. $form['vocabulary'] = array(
  426. '#type'=>'select',
  427. '#options'=>$voc_options,
  428. '#default_value' => $vid,
  429. '#title' => t('vocabulary for showrooms'),
  430. '#multiple' => false,
  431. );
  432. // default taxonomy term
  433. if($vid){
  434. $tree = taxonomy_get_tree($vid);
  435. foreach ($tree as $key => $term) {
  436. $terms_options[$term->tid] = $term->name;
  437. }
  438. $term = variable_get('materio_showroom_migrate_default_term', null);
  439. $form['default_term'] = array(
  440. '#type'=>'select',
  441. '#options'=>$terms_options,
  442. '#default_value' => $term,
  443. '#title' => t('default taxonomy term'),
  444. '#multiple' => false,
  445. );
  446. if($term){
  447. $form['migrate'] = array(
  448. '#type' => 'submit',
  449. '#value' => 'Migrate',
  450. // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
  451. );
  452. }
  453. }
  454. }
  455. $form['submit'] = array(
  456. '#type' => 'submit',
  457. '#value' => 'Save',
  458. // '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
  459. );
  460. return $form;
  461. }
  462. function materio_showroom_migrate_location_fields_settings_form_submit($form, &$form_state){
  463. // dsm($form_state, 'form_state');
  464. $node_type = $form_state['values']['node_type'];
  465. $src_field = $form_state['values']['source_field'];
  466. $target_field = $form_state['values']['target_field'];
  467. $vid = $form_state['values']['vocabulary'];
  468. $default_term = $form_state['values']['default_term'];
  469. variable_set('materio_showroom_migrate_node_type', $node_type);
  470. variable_set('materio_showroom_migrate_source_field', $src_field);
  471. variable_set('materio_showroom_migrate_target_field', $target_field);
  472. variable_set('materio_showroom_migrate_vocabulary', $vid);
  473. variable_set('materio_showroom_migrate_default_term', $default_term);
  474. // dsm($node_type, 'node_type');
  475. // dsm($src_field,"src_field");
  476. // dsm($target_field,"target_field");
  477. // dsm($vid,"vid");
  478. // dsm($default_term,"default_term");
  479. if ($form_state['values']['op'] == 'Migrate'){
  480. _materio_showroom_batch_migration($node_type, $src_field, $target_field, $vid, $default_term);
  481. }
  482. // $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";
  483. //
  484. // $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";
  485. }
  486. function _materio_showroom_batch_migration($node_type, $src_field, $target_field, $vid, $tid){
  487. // Reset counter for debug information.
  488. $_SESSION['http_request_count'] = 0;
  489. $batch = array(
  490. 'title' => t('Migrating Showroom fields ...'),
  491. 'operations' => array(),
  492. 'init_message' => t('Commencing'),
  493. 'progress_message' => t('Processed @current out of @total.'),
  494. 'error_message' => t('An error occurred during processing'),
  495. 'finished' => '_materio_showroom_batch_migration_finished',
  496. );
  497. $query = new EntityFieldQuery();
  498. $query->entityCondition('entity_type', 'node')
  499. ->entityCondition('bundle', $node_type)
  500. ->fieldCondition($src_field, 'value', '', '!=');
  501. $result = $query->execute();
  502. // dsm($result, 'result');
  503. if (isset($result['node'])) {
  504. $voc = taxonomy_vocabulary_load($vid);
  505. $tree = taxonomy_get_tree($vid);
  506. foreach ($tree as $key => $term) {
  507. $terms[$term->tid] = $term->name;
  508. }
  509. if(!empty($terms)){
  510. foreach ($result['node'] as $nid => $value) {
  511. $batch['operations'][] = array(
  512. '_materio_showroom_batch_migration_op',
  513. array(
  514. $nid,
  515. $src_field, $target_field,
  516. $terms, $tid,
  517. )
  518. );
  519. }
  520. }
  521. }
  522. batch_set($batch);
  523. }
  524. function _materio_showroom_batch_migration_op($nid, $src_field, $target_field, $terms, $default_tid, &$context){
  525. $context['results']['field_migrated']++;
  526. $node = node_load($nid);
  527. $src_items = field_get_items('node', $node, $src_field);
  528. $src_value = $src_items ? $src_items[0]['value'] : '';
  529. // $src_value = str_replace('/', '-', $src_value);
  530. //
  531. // build new item list
  532. $items = array(LANGUAGE_NONE => array());
  533. foreach ($terms as $tid => $name) {
  534. $items[LANGUAGE_NONE][] = array(
  535. 'showroom_tid' => $tid,
  536. 'location' => $default_tid == $tid ? $src_value : '',
  537. );
  538. }
  539. $node->$target_field = $items;
  540. node_save($node);
  541. //Simply show the import row count.
  542. $context['message'] = t('Migrating node !c : %title (%nid)', array(
  543. '!c' => $context['results']['field_migrated'],
  544. '%title'=>$node->title,
  545. '%nid'=>$nid ));
  546. // In order to slow importing and debug better,
  547. // we can uncomment this line to make each import slightly slower.
  548. // usleep(2500);
  549. if ( false ) {
  550. $context['results']['failed_nodes'][] = $nid ;
  551. }
  552. _materio_showroom_update_http_requests();
  553. }
  554. function _materio_showroom_batch_migration_finished($success, $results, $operations){
  555. // dsm($success, 'success');
  556. // dsm($results, 'results');
  557. // dsm($operations, 'operations');
  558. if ($success) {
  559. // Here we could do something meaningful with the results.
  560. // We just display the number of nodes we processed...
  561. drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _materio_showroom_get_http_requests())));
  562. drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
  563. }
  564. else {
  565. // An error occurred.
  566. // $operations contains the operations that remained unprocessed.
  567. drupal_set_message(
  568. t('operations : @args',
  569. array(
  570. '@args' => print_r(current($operations), TRUE),
  571. )
  572. ),
  573. 'error'
  574. );
  575. $error_operation = reset($operations);
  576. drupal_set_message(
  577. t('An error occurred while processing @operation with arguments : @args',
  578. array(
  579. '@operation' => $error_operation[0],
  580. '@args' => print_r($error_operation[0], TRUE),
  581. )
  582. ),
  583. 'error'
  584. );
  585. }
  586. }
  587. /**
  588. * Utility function to increment HTTP requests in a session variable.
  589. */
  590. function _materio_showroom_update_http_requests() {
  591. $_SESSION['http_request_count']++;
  592. }
  593. /**
  594. * Utility function to count the HTTP requests in a session variable.
  595. *
  596. * @return int
  597. * Number of requests.
  598. */
  599. function _materio_showroom_get_http_requests() {
  600. return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
  601. }