migrate_materio.adherent.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. /**
  3. * MaterioIndustrialUserMigration
  4. *
  5. */
  6. class MaterioAdherentUserMigration extends MaterioBasicMigration {
  7. public function __construct() {
  8. parent::__construct();
  9. $this->description = t('Migrate Materio Members Account Users');
  10. // provide better description for source fields
  11. // and add new field source not from sql
  12. $source_fields = array(
  13. 'id_login' => t('Member id from source'),
  14. 'memo'=>t('memo'),
  15. 'roles'=>t('roles'),
  16. 'user_name' => 'User name',
  17. );
  18. $query = db_select(MIG_MAT_SRC_DB .'.login_id', 'lid');
  19. $query->join(MIG_MAT_SRC_DB .'.member_id', 'mid', 'mid.id_member = lid.id_member');
  20. // $query->join(MIG_MAT_SRC_DB .'.locale_id', 'locid', 'lid.def_locale = locid.id_locale');
  21. $query->join(MIG_MAT_SRC_DB .'.member_info', 'minf', 'mid.id_member = minf.id_member');
  22. $query
  23. ->fields('lid', array('id_login', 'status', 'date_creation', 'date_modif', 'date_end', 'type', 'email', 'news_letter', 'def_locale'))
  24. ->fields('mid', array('memo'))
  25. // ->fields('locid', array('name', 'short_name', 'suffix'))
  26. ->fields('minf', array('activity'))
  27. // ->condition('email_general', '', '<>')
  28. ->orderBy('lid.id_login', 'DESC');
  29. // $query->groupBy('lid.id_member');
  30. $this->source = new MigrateSourceSQL($query, $source_fields);
  31. $this->destination = new MigrateDestinationUser();
  32. $this->map = new MigrateSQLMap($this->machineName,
  33. array(
  34. 'id_login' => array(
  35. 'type' => 'int',
  36. 'unsigned' => TRUE,
  37. 'not null' => TRUE,
  38. 'description' => 'OLD Unique member ID',
  39. 'alias' => 'lid',
  40. )
  41. ),
  42. MigrateDestinationUser::getKeySchema()
  43. );
  44. // Make the mappings
  45. $this->addFieldMapping('name', 'user_name');
  46. $this->addFieldMapping('pass')->defaultValue(NULL);
  47. $this->addFieldMapping('mail', 'email');
  48. # NOTE : maybe if we provide only init mail, we wiil be able to ask email confirmation for users
  49. $this->addFieldMapping('init', 'email');
  50. $this->addFieldMapping('language', 'def_locale');
  51. $this->addFieldMapping('theme')->defaultValue('');
  52. $this->addFieldMapping('signature')->defaultValue('');
  53. $this->addFieldMapping('signature_format')->defaultValue('filtered_html');
  54. $this->addFieldMapping('created', 'date_creation');
  55. $this->addFieldMapping('access')->defaultValue(0);//, 'date_modif');
  56. $this->addFieldMapping('login')->defaultValue(0);
  57. $this->addFieldMapping('status')->defaultValue(0);
  58. $this->addFieldMapping('picture')->defaultValue(0);
  59. $this->addFieldMapping('timezone')->defaultValue(NULL);
  60. // $this->addFieldMapping('path')->issueGroup(t('DNM'));
  61. $this->addFieldMapping('pathauto')->defaultValue(1);
  62. $this->addFieldMapping('roles', 'roles');// 7 = utilisateur, 6 = adherent, 8 = premium; ->defaultValue(array(6)); // adhérent
  63. $this->addFieldMapping('is_new')->defaultValue(TRUE);
  64. $this->addFieldMapping('field_newsletter', 'news_letter');
  65. $this->addFieldMapping('field_memo', 'memo');
  66. $this->addFieldMapping(NULL, 'activity');
  67. $this->addUnmigratedDestinations(array(
  68. 'role_names', 'data', 'field_memo:language',
  69. 'field_company', 'field_company:source_type', 'field_company:create_term', 'field_company:ignore_case',
  70. 'path',
  71. ));
  72. $this->addUnmigratedSources(array(
  73. 'status', 'date_end', 'type',
  74. ));
  75. }
  76. public function prepareRow($cr) {
  77. # change roles by end date
  78. if($cr->date_end == ''){
  79. $roles[] = 7;
  80. }else{
  81. $today = time();
  82. $time_end = strtotime($cr->date_end);
  83. // dsm($time_end, 'time_end');
  84. $roles = array();
  85. if($time_end > $today){
  86. $roles[] = 6;
  87. }else{
  88. $roles[] = 7;
  89. }
  90. }
  91. // dsm($roles, 'roles');
  92. $cr->roles = $roles;
  93. #user name
  94. $cr->user_name = $this->getUserName($cr->email);
  95. // TODO: set role expiration
  96. #duplicate email
  97. if($this->checkDupEmail($cr->email)){
  98. $memo[] = "@dup-email : ".$cr->email;
  99. }
  100. #language
  101. $cr->def_locale = $cr->def_locale == 1 ? 'en' : 'fr';
  102. #memo
  103. $memo[] = "@imported";
  104. #statuts
  105. $memo[] = "statut : ".$cr->status;
  106. #statuts
  107. $memo[] = "type : ".$cr->type;
  108. #statuts
  109. $memo[] = "activity : ".$cr->activity;
  110. #memo
  111. $cr->memo = implode("\n", $memo) ."\n\n". $cr->memo;
  112. return true;
  113. // return FALSE if you wish to skip a particular row
  114. }
  115. public function prepare($user, stdClass $row) {
  116. // dsm('-- prepare --');
  117. // dsm($user, '$user');
  118. // dsm($row, '$row');
  119. // $node->field_private_email = array('und'=>array($row->email_contact));
  120. }
  121. public function complete($user, $row) {
  122. // dsm('-- complete --');
  123. // Do what you need to do to
  124. // dsm($row, '$row');
  125. // dsm($user, '$user');
  126. if(in_array(6, $user->roles)){
  127. $time_end = strtotime($row->date_end);
  128. $min = strtotime('2013 01 30');
  129. if($time_end < $min)
  130. $time_end = $min;
  131. uc_roles_grant($user, 6, $time_end, FALSE, TRUE);
  132. }
  133. }
  134. /**
  135. * helpers
  136. */
  137. private function getUserName($email){
  138. // Default implementation of name generation.
  139. $new_name = preg_replace('/@.*$/', '', $email);
  140. // Remove unwanted characters.
  141. $new_name = preg_replace('/[^a-zA-Z0-9.-]/', '', $new_name);
  142. // if username generated from email record already exists, append underscore and number eg:(chris_123)
  143. if ((bool) db_query("SELECT 1 FROM {users} WHERE LOWER(name) = LOWER(:new_name)", array(':new_name' => $new_name))->fetchField()) {
  144. $name_idx = db_query_range("SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP :search ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC", 0, 1, array(':search' => '^' . $new_name . '_[0-9]+$'))->fetchField();
  145. $new_name .= '_' . ($name_idx + 1);
  146. }
  147. return $new_name;
  148. }
  149. private function checkDupEmail($email){
  150. if ((bool) db_query("SELECT 1 FROM {users} WHERE mail = :email", array(':email' => $email))->fetchField()) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. }
  156. /**
  157. * MaterioIndustrialProfile2Migration
  158. */
  159. class MaterioAdherentProfile2Migration extends MaterioBasicMigration {
  160. public function __construct() {
  161. parent::__construct();
  162. global $user;
  163. $this->description = t('Migrate Materio Member Profiles2');
  164. // provide better description for source fields
  165. // and add new field source not from sql
  166. $source_fields = array(
  167. 'id_login' => t('Member id from source'),
  168. 'member_name' => t('Main member name'),
  169. 'member_firstname' => t('Main member first name'),
  170. 'premise'=>'',
  171. 'sub_premise'=>'',
  172. );
  173. $query = db_select(MIG_MAT_SRC_DB .'.login_id', 'lid');
  174. $query->join(MIG_MAT_SRC_DB .'.member_id', 'mid', 'mid.id_member = lid.id_member');
  175. $query->join(MIG_MAT_SRC_DB .'.member_info', 'minf', 'mid.id_member = minf.id_member');
  176. $query
  177. ->fields('lid', array('id_login', 'status', 'name',))
  178. // ->fields('mid', array('memo', 'cgv'))
  179. ->fields('minf', array(
  180. 'con_title', 'con_name1', 'con_name2', 'con_quality', 'con_service', 'con_ccode1', 'con_tel1',
  181. 'organization', 'employee', 'naf', 'siret', 'web', 'address', 'city', 'zip', 'country', )
  182. )
  183. // ->condition('status', 'A')
  184. ->orderBy('lid.id_login', 'DESC');
  185. // $query->groupBy('lid.id_member');
  186. $this->source = new MigrateSourceSQL($query, $source_fields);
  187. // $this->dependencies = array('MaterioIndustrialUser');
  188. $this->destination = new MigrateDestinationProfile2('adherent');
  189. $this->map = new MigrateSQLMap($this->machineName,
  190. array(
  191. 'id_login' => array(
  192. 'type' => 'int',
  193. 'unsigned' => TRUE,
  194. 'not null' => TRUE,
  195. 'description' => 'OLD Unique Member ID',
  196. 'alias' => 'lid',
  197. )
  198. ),
  199. MigrateDestinationProfile2::getKeySchema()
  200. );
  201. // Connecting the profile2 to the user:
  202. $this->addFieldMapping('uid', 'id_login')
  203. ->sourceMigration('MaterioAdherentUser') // If your user migration class was named 'MyUserMigration', the string is 'MyUser'
  204. ->description(t('The assignment of profile2-items to the respective Adherent user'));
  205. $this->addFieldMapping('revision_uid', 'id_login')
  206. ->sourceMigration('MaterioAdherentUser');
  207. // Make the mappings
  208. $this->addFieldMapping('language')->defaultValue('');
  209. $this->addFieldMapping('field_name', 'member_name');
  210. $this->addFieldMapping('field_first_name', 'member_firstname');
  211. $this->addFieldMapping(NULL, 'con_name1');
  212. $this->addFieldMapping(NULL, 'con_name2');
  213. $this->addFieldMapping(NULL, 'name');
  214. $this->addFieldMapping('field_private_quality', 'con_quality'); // http://drupal.org/node/1232028
  215. $this->addFieldMapping('field_private_name_title', 'con_title');
  216. $arguments = array(
  217. 'number' => array('source_field' => 'con_tel1'),
  218. );
  219. $this->addFieldMapping('field_private_phone', 'con_ccode1')
  220. ->arguments($arguments);
  221. // field_private_phone:number
  222. // field_private_phone:extension
  223. $this->addFieldMapping(NULL, 'con_tel1');
  224. $this->addFieldMapping(NULL, 'status');
  225. $arguments = array(
  226. 'thoroughfare' => array('source_field' => 'address'),
  227. 'premise' => array('source_field' => 'premise'),
  228. 'sub_premise' => array('source_field' => 'sub_premise'),
  229. 'locality' => array('source_field' => 'city'),
  230. 'postal_code' => array('source_field' => 'zip'),
  231. );
  232. $this->addFieldMapping('field_adresse', 'country')
  233. ->arguments($arguments);
  234. $this->addFieldMapping(NULL, 'address');
  235. $this->addFieldMapping(NULL, 'premise');
  236. $this->addFieldMapping(NULL, 'sub_premise');
  237. $this->addFieldMapping(NULL, 'city');
  238. $this->addFieldMapping(NULL, 'zip');
  239. $this->addFieldMapping(NULL, 'country');
  240. $this->addFieldMapping('field_organization', 'organization');
  241. $this->addFieldMapping('field_service', 'con_service');
  242. $this->addFieldMapping('field_employee', 'employee');
  243. $this->addFieldMapping('field_naf', 'naf');
  244. $this->addFieldMapping('field_siret', 'siret');
  245. $this->addFieldMapping('field_user_website', 'web');
  246. $this->addUnmigratedDestinations(array(
  247. 'field_private_quality:language', 'field_first_name:language', 'field_name:language',
  248. 'field_organization:language', 'field_activity:language', 'field_service:language', 'field_naf:language', 'field_siret:language',
  249. 'field_private_phone:number', 'field_private_phone:extension',
  250. ));
  251. }
  252. public function prepareRow($cr) {
  253. if($cr->status == 'A'){
  254. # nom prenom
  255. $cr->member_name = $cr->con_name1;
  256. $cr->member_firstname = $cr->con_name2;
  257. #telephone
  258. if($cr->con_ccode1 != ''){
  259. $ccs = cck_phone_countrycodes();
  260. $cc_founded = false;
  261. foreach ($ccs as $cc => $cc_values) {
  262. if('+'.preg_replace('/^\+/', '', trim($cr->con_ccode1)) == $cc_values['code']){
  263. $cr->con_ccode1 = $cc;
  264. $cc_founded = true;
  265. break;
  266. }
  267. }
  268. if(!$cc_founded){
  269. // dsm($cr->con_ccode1, '$cr->con_ccode1');
  270. $cr->con_ccode1 = null;
  271. }
  272. }
  273. }else{
  274. $name = explode(' ', $cr->name);
  275. $cr->member_name = array_shift($name);
  276. $cr->member_firstname = implode(' ', $name);
  277. $cr->con_quality = '';
  278. $cr->con_title = '';
  279. $cr->con_ccode1 = '';
  280. $cr->con_tel1 = '';
  281. }
  282. $cr->organization = trim($cr->organization);
  283. $cr->address = preg_replace('/\\n/', ', ', $cr->address);
  284. if(strlen($cr->address) > 255){
  285. $adress = $cr->address;
  286. $cr->address = substr($adress, 0, 250);
  287. $cr->premise = substr($adress, 250, 500);
  288. $sub = substr($adress, 500, 750);
  289. $cr->sub_premise = $sub ? $sub : '';
  290. }else{
  291. $cr->premise = '';
  292. $cr->sub_premise = '';
  293. }
  294. $cr->city = preg_replace('/\\n/', ' ', $cr->city);
  295. if($cr->country != ''){
  296. require_once DRUPAL_ROOT . '/includes/locale.inc';
  297. $countries = country_get_list();
  298. $c_founded = false;
  299. foreach ($countries as $cc => $c) {
  300. if(stristr($cr->country, $c)){
  301. $cr->country = $cc;
  302. $c_founded = true;
  303. break;
  304. }
  305. }
  306. if(!$c_founded){
  307. $nonISO_cc = array(
  308. 'liban' => 'LB',
  309. 'uk' => 'GB',
  310. 'usa' => 'US',
  311. 'korea, south' => 'KR',
  312. 'brésil' => 'BR',
  313. );
  314. foreach ($nonISO_cc as $n => $cc) {
  315. if(stristr($cr->country, $n)){
  316. $cr->country = $cc;
  317. $c_founded = true;
  318. break;
  319. }
  320. }
  321. if(!$c_founded){
  322. // dsm($cr->country, '$cr->country');
  323. $cr->country = null;
  324. }
  325. }
  326. }
  327. return TRUE;
  328. // return FALSE if you wish to skip a particular row
  329. }
  330. // public function prepare($node, stdClass $row) {
  331. // // dsm('-- prepare --');
  332. // // dsm($node, '$node');
  333. // // dsm($row, '$row');
  334. //
  335. // // $node->field_private_email = array('und'=>array($row->email_contact));
  336. //
  337. // }
  338. }