Advertisement
vasylOk

Untitled

May 6th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.65 KB | None | 0 0
  1. <?php
  2. require_once( dirname( __FILE__ ) . '/../_init.php' );
  3.  
  4. $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "thehome_subs.csv";
  5. $domain_id = VerticalSites::THE_HOME_ID;
  6. $reader = new SplFileObject( $filename );
  7. $new_subscribers = $existed_subscribers = $subscriptions = $geotargets = [];
  8. while ( $row = $reader->fgetcsv() ){
  9.     $email = filter_var( Member::StrEmail( $row[ 2 ] ), FILTER_VALIDATE_EMAIL );
  10.     if( empty( $email ) ){
  11.         continue;
  12.     }
  13.  
  14.     $member = $conn->fetchRow(
  15.         "SELECT m.id, ms.id AS subscription_id
  16.        FROM member m LEFT JOIN member_subscribe ms ON m.id=ms.member_id AND ms.domain_id=? AND ms.mailing_list_id IN (?)
  17.        WHERE m.email=?",
  18.         array($domain_id, 7, $email )
  19.     );
  20.  
  21.     if( empty( $member ) ){
  22.         print "$email - new" . PHP_EOL;
  23.         $first_name = $row[ 0 ];
  24.         $last_name = $row[ 1 ];
  25.         $postal_code = $row [ 3 ];
  26.         $new_subscribers[ $email ] = [ 'email' => $email, 'name' => $first_name." ".$last_name, 'postal_code' => $postal_code ];
  27.         $result = Doctrine_Manager::connection()->fetchRow(
  28.             "SELECT g.id, g.city_id, g.zip, c.slug FROM geotarget g INNER JOIN city c ON g.city_id=c.id WHERE zip=? AND country='AU'",
  29.             [ ( int ) $postal_code ]
  30.         );
  31.         if (empty($result)) {
  32.             $result = Doctrine_Manager::connection()->fetchRow(
  33.                 "SELECT g.id, g.city_id, g.zip, c.slug FROM geotarget g INNER JOIN city c ON g.city_id=c.id WHERE zip=? AND country='AU'",
  34.                 [ 2000 ]
  35.             );
  36.         }
  37.  
  38.         $geotargets[ $email ] = $result;
  39.     }
  40.  
  41.     if( !empty( $member ) && !empty( $member[ "id" ] ) && empty( $member[ "subscription_id" ] ) ){
  42.         print "$email - existed" . PHP_EOL;
  43.         $existed_subscribers[ $email ] = ( int ) $member[ "id" ];
  44.         $result = Doctrine_Manager::connection()->fetchRow(
  45.             "SELECT g.id, g.city_id, g.zip, c.slug FROM geotarget g INNER JOIN city c ON g.city_id=c.id WHERE zip=? AND country='AU'",
  46.             [ ( int ) $postal_code ]
  47.         );
  48.         if (empty($result)) {
  49.             $result = Doctrine_Manager::connection()->fetchRow(
  50.                 "SELECT g.id, g.city_id, g.zip, c.slug FROM geotarget g INNER JOIN city c ON g.city_id=c.id WHERE zip=? AND country='AU'",
  51.                 [ 2000 ]
  52.             );
  53.         }
  54.  
  55.         $geotargets[ $email ] = $result;
  56.     }
  57. }
  58.  
  59. $members = [];
  60. $conn->beginTransaction();
  61. foreach ( $new_subscribers as $new_sub ){
  62.     $conn->exec(
  63.         "INSERT INTO member (email, name, sub_postal_code, role, created_at, updated_at, activated, imported_at, country)
  64.        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
  65.         array( $new_sub['email'], $new_sub['name'], $geotargets[ $new_sub['email'] ][ 'zip' ], Member::ROLE_SUBSCRIBER, date( "Y-m-d H:i:s" ),
  66.             date( "Y-m-d H:i:s" ), 1, date( "Y-m-d H:i:s" ), "AU" )
  67.     );
  68.     $member_id = ( int ) $conn->lastInsertId();
  69.     $members[ $member_id ] = array(
  70.         "id" => $member_id,
  71.         "is_new" => true
  72.     );
  73.     $conn->exec(
  74.         "INSERT INTO member_subscribe (member_id, mailing_list_id, geotarget_id, city_id, city_slug,
  75.        city_zip, domain_id, created_at, updated_at, frequency, country)
  76.        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  77.         array( $member_id, 7, $geotargets[ $new_sub['email'] ][ 'id' ], $geotargets[ $new_sub['email'] ][ 'city_id' ],
  78.             $geotargets[ $new_sub['email'] ][ 'slug' ], $geotargets[ $new_sub['email'] ][ 'zip' ], $domain_id,
  79.             date( "Y-m-d H:i:s" ), date( "Y-m-d H:i:s" ), MemberSubscribe::SUBSCRIBE_DAILY, "AU")
  80.     );
  81. }
  82. $conn->commit();
  83.  
  84. $conn->beginTransaction();
  85. foreach ( $existed_subscribers as $existed_sub_email => $sub_id ){
  86.     $conn->exec(
  87.         "INSERT INTO member_subscribe (member_id, mailing_list_id, geotarget_id, city_id, city_slug,
  88.        city_zip, domain_id, created_at, updated_at, frequency, country)
  89.        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  90.         array( $sub_id, 7, $geotargets[ $existed_sub_email ][ 'id' ], $geotargets[ $existed_sub_email ][ 'city_id' ],
  91.             $geotargets[ $existed_sub_email ][ 'slug' ], $geotargets[ $existed_sub_email ][ 'zip' ], $domain_id,
  92.             date( "Y-m-d H:i:s" ), date( "Y-m-d H:i:s" ), MemberSubscribe::SUBSCRIBE_DAILY, "AU")
  93.     );
  94.     $members[ $sub_id ] = array(
  95.         "id" => $sub_id,
  96.         "is_new" => false
  97.     );
  98. }
  99. $conn->commit();
  100.  
  101. $tm = new TransactionalMessages();
  102. $domain_vertical = Doctrine::getTable('DomainVerticalsTable')->findOneByVerticalIdAndDomainId( VerticalSites::VERTICAL_SITE_THEHOME_ID, VerticalSites::THE_HOME_ID );
  103. VerticalSites::$_now_site = VerticalSites::NowSite( $domain_vertical );
  104. VerticalSites::$_now_brand = Domains::findById( $domain_id );
  105. VerticalSites::$_now_template = Doctrine::getTable('Templates')->find( VerticalSites::$_now_brand->template_id );
  106. VerticalSites::$_now_sender_profile = $domain_vertical->SenderProfile;
  107. foreach ( $members as $mid => $member ){
  108.     if( $member[ "is_new" ] ){
  109.         MembersQueueTable::add( $member[ "id" ], MembersQueueTable::ACTION_ADD_MEMBER );
  110.         $user = Doctrine::getTable('Member')->findOneById($member[ "id" ]);
  111.         $city = Doctrine::getTable('City')->findOneById($geotargets[$user->email]['city_id']);
  112.         $tm->sendWelcome($user, $city);
  113.     }
  114.     $subscriptions = Doctrine_Query::create()->from( "MemberSubscribe" )->where( "member_id=?", $member[ "id" ] )
  115.         ->addWhere( "domain_id=?", $domain_id)
  116.         ->execute();
  117.     MembersQueueTable::add( $member[ "id" ], MembersQueueTable::ACTION_ADD_SUBSCRIPTIONS,
  118.         array( "subscriptions" => $subscriptions ) );
  119. }
  120.  
  121. //$databaseManager->shutdown();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement