Created
September 18, 2009 09:12
-
-
Save eugenepyvovarov/188959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(0); | |
require '../../common/wlGenerator.php'; | |
include_once '../../common/triggermail.php'; | |
require 'SnapFish.php'; | |
include_once 'snapfish_order_completed.php'; | |
/** | |
* WLHandler class | |
* Work list handler for Snapfish | |
* This work list generator will create a work list | |
* Extends Work List Helper class. | |
**/ | |
class WLHandler extends WorkListHelper { | |
private $snapfish; | |
/** | |
* Constructor function of WLHandler | |
* Calls the WorkListHelper constructor. | |
* | |
* @access public | |
* @return void | |
**/ | |
public function __construct() { | |
parent::__construct(); | |
$this->host = 'devdb.scancafe.com'; | |
$this->username = 'dbtest_remote'; | |
$this->key = 'tn4Y#MR9Gy'; | |
$this->db = 'scancafe_scancafe'; | |
mysql_pconnect($this->host, $this->username, $this->key); | |
mysql_select_db($this->db); | |
} | |
public function _getOrdersforUpload() { | |
declare(ticks=1); | |
// setup signal handlers | |
// pcntl_signal(SIGTERM, "sig_handler"); | |
// pcntl_signal(SIGHUP, "sig_handler"); | |
$time_start = microtime(true); | |
$children = array(); | |
$max_threads_count = 35; | |
$query = "SELECT | |
swm.id master_id, swm.partner_code, swm.order_id order_id, swm.required, swm.available, swm.transferred, swm.status, si.merch_image, si.scancafe_tracking_id | |
FROM | |
scafe_wl_master swm, scafe_invoice si | |
WHERE | |
swm.order_id = si.id AND | |
swm.required = swm.available AND | |
swm.available = swm.transferred AND | |
swm.status = '30' AND | |
si.merch_image = '110' | |
LIMIT 1"; | |
//takes order ready for upload | |
$result = $this->select($this->query($query)); | |
if(!isset($result) || empty($result)){ | |
$this->logError('No orders ready for upload.'); | |
return False; | |
} | |
//takes the order | |
$order_data = $result[0]; | |
//initiates the Snapfish Wrapper | |
$snfsWrapper = new Wrapper_SnapFish(); | |
//initiates order online process | |
$orderOnlineObj = new OrderOnline(); | |
$master_id = $order_data->master_id; | |
$order_id = $order_data->order_id; | |
$scancafe_tracking_id = $order_data->scancafe_tracking_id; | |
/*retrieve customer details for the $order_id*/ | |
$customerIDSet = $this->fetch(array('customer_id'), array('id' => $order_id), 'invoice'); | |
if(!$customerIDSet) { | |
$this->logError('Error while fetching record for the order :'.$order_id); | |
return False; | |
} | |
$customer_id = $customerIDSet[0]->customer_id; | |
$fetch_array_fields = array('email', 'first_name', 'sfly_userid'); | |
$table = 'customer'; | |
$where_str['id'] = $customer_id; | |
/*retrieve customer details for the $customer_id*/ | |
$customerResultSet = $this->fetch($fetch_array_fields, $where_str, $table); | |
if(!$customerResultSet) { | |
$this->logError('Error while fetching record for the customer :'.$customer_id); | |
return False; | |
} | |
$customer_name = $customerResultSet[0]->first_name; | |
$login = $customer_email = $customerResultSet[0]->email; | |
$sfly_userid = $customerResultSet[0]->sfly_userid; | |
$password = $this->decryptUserPassword($sfly_userid); | |
$phone = ''; | |
//Login to Snapfish | |
$params = array('email' => '[email protected]',//$login, | |
'password' => 'password',//$password, | |
'subscriberId' => 1000000,//$sfly_userid, | |
'partnerId' => 'TST'); | |
$snfsWrapper->initialize($params); | |
$snfs_login = $snfsWrapper->authorize(); | |
if($snfs_login === false) { | |
//update scafe_invoice merch_image field to 120 | |
$invoice_ary = array(); | |
$table = 'invoice'; | |
$invoice_ary['merch_image'] = '120'; | |
$whr_update['id'] = $order_id; | |
$invoiceResultSet = $this->update($invoice_ary, $whr_update, $table); | |
$invoice_id = mysql_affected_rows(); | |
if(empty($invoice_id)){ | |
echo "Error while updating the status for the order: '".$order_id."'\r\n"; | |
exit; | |
} | |
else{ | |
//send mail for Authorization Failed. | |
$client = new Triggermail_Client(); | |
$mailTempName="Snapfish - 5A - Upload Incomplete"; | |
$mailTo="[email protected]"; | |
$firstname="Richa"; | |
$sendData = $client->send($mailTempName, $mailTo, array('first_name' => $firstname, 'order_number' => $order_id)); | |
} | |
echo "Snapfish Authorisation failed!\r\n"; | |
return False; | |
} | |
echo "Successful Login to Snapfish\r\n"; | |
//get the albums | |
$albums_ary_temp = $snfsWrapper->getAlbums(); | |
$albums_ary = array(); | |
foreach($albums_ary_temp as $title => $id) { | |
$albums_ary[$title] = array('id' => $id); | |
} | |
if($albums_ary === false) { | |
$this->logError("Error while getting album list from snapfish. Error : ".$snfsWrapper->errorMessage."' \r\n"); | |
return false; | |
} | |
$wlGenObj = new WLGenerator(); | |
$wlDetails = $wlGenObj->_getWLDetails($master_id); | |
$albumUniqueName = ""; | |
$albumUniqueId = ""; | |
$j = 1; | |
for($x = 0;$x< count($wlDetails);$x++){ | |
$amount = count(scandir(dirname(__FILE__).'/lock')) - 2; | |
if($x==0 and $amount >= $max_threads_count) { | |
print "Please delete lock files so process can start \r\n"; | |
} | |
if($amount < $max_threads_count){ | |
flush(); | |
mysql_connect($this->host, $this->username, $this->key, true); | |
mysql_select_db($this->db); | |
$details_key = $x; | |
$details_data = $wlDetails[$x]; | |
$album_name = $album_data['album_name'] = $details_data->album_name; | |
$image_location = $details_data->target_url; | |
$image_id = $details_data->image_id; | |
$album_id_ary = $this->fetch(array('id'), array('album_name' => "'".$album_name."'"), 'albums'); | |
// $album_data = $wlGenObj->_albumImages($album_id_ary); | |
// $album_values = $album_data[0]; | |
// print_r($album_data); | |
// $imgs_count = count($album_values); | |
// | |
$imgs_count = mysql_result(mysql_query('SELECT COUNT(*) FROM scafe_wl_details WHERE album_name = \''.$album_name.'\''),0); | |
$album_id = $album_id_ary[0]->id; | |
//check if Album name is similar to the previous one | |
if (!isset($albums_ary[$album_name])){ | |
$albumName = 'SC3_'.$album_name.'_'.date('d-m_H-i'); | |
$snfs_album = $snfsWrapper->createAlbum($albumName, array('numberOfImages' => $imgs_count)); | |
if($snfs_album !==false ){ | |
$this->logError('Created album '.$album_name . "\r\n"); | |
} | |
$albums_ary[$album_name]['id'] = $snfs_album; | |
$albums_ary[$album_name]['session'] = $snfsWrapper->currentSession; | |
} elseif(!isset($albums_ary[$album_name]['session'])) { | |
$snfs_album = $snfsWrapper->getAlbumById($albums_ary[$album_name], array('numberOfImages' => $imgs_count)); | |
$albums_ary[$album_name]['session'] = $snfsWrapper->currentSession; | |
} else { | |
$snfsWrapper->currentSession = $albums_ary[$album_name]['session']; | |
} | |
if(!isset($albums_ary[$album_name]['sequence'])) { | |
$albums_ary[$album_name]['sequence'] = $imgs_count; | |
} else { | |
$albums_ary[$album_name]['sequence'] = $albums_ary[$album_name]['sequence'] - 1; | |
} | |
print "Number of images in '".$album_name."' - ".$albums_ary[$album_name]['sequence']."\n\r"; | |
$sequenceNumber = $albums_ary[$album_name]['sequence']; | |
$albumUniqueName = $album_name; | |
$file_on_local = str_replace('http://storage.scancafe.com', '/var/www/html', $image_location); | |
// $file_on_local = '/var/www/_default/scafe_wl/snapfish/images/test'.$j.'.jpg'; | |
$j++; | |
if($snfs_album === false) { | |
echo "Error while creating album for order: '".$order_id."'\r\n"; | |
exit; | |
} | |
$pid = pcntl_fork(); | |
exec('touch '.dirname(__FILE__).'/lock/'.$x); | |
if ($pid == -1) { | |
die("could not fork"); | |
} else if ($pid) { | |
//exit(); // we are the parent | |
$children[$x] = $pid; | |
continue; | |
} else { | |
// we are the child | |
$snfs_photo = $snfsWrapper->uploadImage($file_on_local, array('sequenceNumber' => $sequenceNumber )); | |
if($snfs_photo !== true){ | |
echo "Error while uploading image '".$file_on_local."' for the order: '".$order_id."'"; | |
$this->logError("Error while uploading image '".$file_on_local."' for the order: '".$order_id."'"); | |
$update_wld_ary['upload_status'] = 2; | |
} else { | |
echo "[".$album_name."][".$sequenceNumber."] image '".$file_on_local."'uploaded!"."\r\n"; | |
$this->logError("Image '".$file_on_local."' uploaded successfully!"."\r\n"); | |
flush(); | |
$update_wld_ary['upload_status'] = 1; | |
$now = date('Y-m-d'); | |
$paid_album_ary = array(); | |
$paid_album_ary['customer_id'] = $customer_id; | |
$paid_album_ary['album_id'] = $album_id; | |
$paid_album_ary['order_tracking_id'] = $scancafe_tracking_id; | |
$paid_album_ary['paid_date'] = $now; | |
$paid_album_ary['order_id'] = $order_id; | |
if($albumUniqueId != $album_id) | |
{ | |
$albumUniqueId = $album_id; | |
$album_updates = $orderOnlineObj->makeOrderOnline($paid_album_ary); | |
if(isset($album_updates) && !empty($album_updates)) | |
{ | |
// echo "Albums Paid Status updated successfully\r\n"; | |
} | |
} | |
} | |
$whr_wld_update['image_id'] = $image_id; | |
$wldResultSet = $this->update($update_wld_ary, $whr_wld_update, 'wlDetails'); | |
exec('rm '.dirname(__FILE__).'/lock/'.$x); | |
exit(0); | |
} | |
} else { | |
$x--; | |
} | |
} | |
foreach ($children as $key => $pid) { | |
// We are the parent, so we shall wait till all children will die. | |
// Otherwise there will be bunch of zombies. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment