Created
November 19, 2014 12:51
-
-
Save ineersa/af80138f62f30429114a 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 | |
class ZendeskImportCommand extends CConsoleCommand | |
{ | |
private $users; | |
private $apps; | |
private $list = 3; | |
/** | |
* @var $zendesk ZenDesk | |
*/ | |
private $zendesk; | |
public function init() | |
{ | |
set_time_limit(0); | |
ini_set("memory_limit","1024M"); | |
$this->apps = CHtml::listData(Applications::model()->findAll(),'app_id','name'); | |
$this->zendesk = Yii::app()->zendesk; | |
} | |
public function run($args) | |
{ | |
echo "Started ->".date("Y-m-d H:i:s")."\r\n"; | |
$page = 1; | |
while(true){ | |
$data = $this->zendesk->users("","role[]=end-user&page={$page}"); | |
foreach($data->users as $user){ | |
if ($user->email != ""){ | |
if (!$this->checkEmail($user->email)){ | |
$apps = $this->identifyApp($user->email); | |
if ( $apps !== false){ | |
$this->importUser($user->email,$apps); | |
} else { | |
MailingUsers::saveUser($user->email,$this->list); | |
} | |
} | |
} | |
} | |
if ($data->next_page === null){ | |
break; | |
} else { | |
$page++; | |
} | |
} | |
echo "Ended ->".date("Y-m-d H:i:s")."\r\n"; | |
} | |
private function importUser($email,$apps) | |
{ | |
MailingUsers::saveUser($email,$this->list); | |
$id = MailingUsers::loadId($email,$this->list); | |
if ($id){ | |
if (count($apps)>0 && is_array($apps)){ | |
foreach ($apps as $app_id){ | |
MailingTags::setTag($app_id,$this->apps[$app_id],$id,$this->list,3); | |
} | |
} | |
} | |
} | |
private function identifyApp($email) | |
{ | |
$apps = array(); | |
$criteria = $this->zendesk->findTicketsByEmailCriteria($email); | |
$tickets = $this->zendesk->search($criteria); | |
if (is_array($tickets->results) && count($tickets->results)>0){ | |
foreach($tickets->results as $ticket){ | |
$app = $this->checkSubjectForApps($ticket->raw_subject,$ticket->description); | |
if ($app && !in_array($app,$apps)){ | |
$apps[] = $app; | |
} | |
} | |
} else return false; | |
if (count($apps)>0) | |
return $apps; | |
else return false; | |
} | |
private function checkSubjectForApps($subject,$description) | |
{ | |
foreach($this->apps as $id=>$name){ | |
if (mb_strpos($subject,$name,0,"UTF-8")!==false || mb_strpos($description,$name,0,"UTF-8")!==false){ | |
return $id; | |
} | |
} | |
return false; | |
} | |
private function checkEmail($email) | |
{ | |
$model = MailingUsers::model()->find("email = :email",array(":email" => $email)); | |
if ($model === null){ | |
return false; | |
} else return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment