Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GetResponse {
- /** API ключ */
- private $apiKey;
- /** API url */
- private $apiUrl;
- private $client;
- private $db;
- public function __construct() {
- $this->apiKey = $_SESSION['grProfile']['apiKey'];
- $this->apiUrl = $_SESSION['grProfile']['apiUrl'];
- $this->client = new jsonRPCClient($this->apiUrl);
- $this->db = new DB_MySQL();
- }
- /**
- * Считывает данные с xml файла
- * @param string $xml
- * @return array xml данные
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function parseXML($xml) {
- $xml = simplexml_load_file($xml);
- $data = array(
- 'campaign' => (string) $xml->config->campaign,
- 'campaign_target' => (string) $xml->config->campaign_target,
- 'date_start' => (string) $xml->config->date_start,
- 'date_end' => (string) $xml->config->date_end,
- 'cycle_day' => (string) $xml->config->cycle_day,
- 'completed' => (string) $xml->config->completed
- );
- return $data;
- }
- /**
- * Получает информацию о кампании
- * @param string $data
- * @return array данные по кампании
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getCampaign($data) {
- $campaignName = $data;
- $campaign = $this->client->get_campaigns(
- $this->apiKey,
- array('name' => array('EQUALS' => $campaignName))
- );
- return $campaign;
- }
- public function getCampaignById($id) {
- $campaign = $this->client->get_campaign(
- $this->apiKey,
- array('campaign' => $id)
- );
- return $campaign;
- }
- /**
- * Получает список всех кампаний
- * @return array список кампаний
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getCampaigns() {
- $campaign = $this->client->get_campaigns($this->apiKey);
- return $campaign;
- }
- public function getContacts($campaign) {
- $contacts = $this->client->get_contacts(
- $this->apiKey,
- array(
- 'campaigns' => array(
- $campaign
- )
- )
- );
- return $contacts;
- }
- /**
- * Получает информацию о контактах кампании $campaign, созданых в период с $data['date_start'] до $data['date_end']
- * @param string $campaign
- * @param array $data
- * @return array данные по контактах кампании $campaign
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getContactsByPeriod($data) {
- $campaignId = $data['campaign'];
- $today = date('Y-m-d');
- $date = new DateTime($today);
- $modifier = (($data['eachDay'] > 1) ? -$data['eachDay'].' days' : -$data['eachDay'].' day');
- $date->modify($modifier);
- $dateTo = $date->format('Y-m-d');
- $contacts = $this->client->get_contacts(
- $this->apiKey,
- array(
- 'campaigns' => array(
- $campaignId
- ),
- 'created_on' => array(
- 'FROM' => $dateTo,
- 'TO' => $dateTo
- )
- )
- );
- return $contacts;
- }
- /**
- * Перемещает контакты в кампании $campaign
- * @param array $contacts
- * @param string $campaign
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.1
- */
- public function moveContacts($contacts, $campaign) {
- //$campaignId = array_keys($campaign);
- //$campaignId = array_pop($campaignId);
- $contactsId = array_keys($contacts);
- foreach ($contactsId as $contact) {
- $move = $this->client->move_contact(
- $this->apiKey,
- array('contact' => $contact,
- 'campaign' => $campaign
- )
- );
- }
- }
- /**
- * Меняет значение completed в xml файле
- * @param string $xmlSorce
- * @throws Exception
- * @return bool
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function setComplete($xmlSorce) {
- $xml = simplexml_load_file($xmlSorce);
- $result = $xml->xpath('/root/config/completed');
- $result[0][0] = 1;
- if ($xml->asXML($xmlSorce) === false) {
- throw new Exception('Cannot save values into "'.$xmlSorce.'"',99);
- }
- return true;
- }
- /**
- * Добавляет контакты в цикл на день
- * @param integer $cycleDay
- * @param array $contacts
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function setContactCycle($cycleDay = null, $contacts) {
- $contactsId = array_keys($contacts);
- foreach ($contactsId as $contact) {
- $set = $this->client->set_contact_cycle(
- $this->apiKey,
- array('contact' => $contact,
- 'cycle_day' => $cycleDay
- )
- );
- }
- }
- /**
- * Создает процесс по работе с кампаниями
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function addProcess($data) {
- $date = date('Y-m-d');
- $query = "INSERT INTO `campaign_actions` (`id`, `grId`, `action`, `campaign`, `campaignTarget`, `cycleDay`, `eachDay`, `date`)
- VALUES (NULL, '{$data['grId']}', '{$data['action']}', '{$data['campaign']}', '{$data['campaignTarget']}', '{$data['cycleDay']}', '{$data['eachDay']}', '{$date}')";
- $result = $this->db->Execute($query);
- }
- /**
- * Редактирует процесс по работе с кампаниями
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function editProcess($data) {
- $query = "UPDATE `campaign_actions`
- SET `grId` = '{$data['grId']}', `action` = '{$data['action']}', `campaign` = '{$data['campaign']}',
- `campaignTarget` = '{$data['campaignTarget']}', `cycleDay` = '{$data['cycleDay']}', `eachDay` = '{$data['eachDay']}'
- WHERE `id` = '{$data['id']}'";
- $result = $this->db->Execute($query);
- }
- /**
- * Выводит информацию о процессе
- * @param int $id
- * @return array
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getProcess($id) {
- $query = "SELECT * FROM `campaign_actions` WHERE `id` = '{$id}'";
- $result = $this->db->Execute($query);
- $row = mysql_fetch_object($result);
- $process = array(
- 'id' => $row->id,
- 'action' => $row->action,
- 'campaign' => $row->campaign,
- 'campaignTarget' => $row->campaignTarget,
- 'cycleDay' => $row->cycleDay,
- 'eachDay' => $row->eachDay,
- 'date' => $row->date
- );
- return $process;
- }
- /**
- * Удаляет процесс
- * @param int $id - ID процесса
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function deleteProcess($id) {
- $query = "UPDATE `campaign_actions` SET `deleted` = '1' WHERE `id` = '{$id}'";
- $result = $this->db->Execute($query);
- }
- /**
- * VIEW Выводит список кампаний в виде списка <select>
- * @param array $campaigns
- * @return string список кампаний
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.1
- */
- public function printCampaigns($campaigns, $currentCampaign = null) {
- foreach ($campaigns as $campaignId => $campaignInfo) {
- $selected = (($currentCampaign !== null && $currentCampaign == $campaignId) ? ' selected' : '');
- $content .= '<option'.$selected.' value="'.$campaignId.'">'.$campaignInfo['name'].'</option>';
- }
- return $content;
- }
- /**
- * VIEW Вывод всех процессов
- * @return string список процессов
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getProcesses($grId) {
- $query = "SELECT * FROM `campaign_actions` WHERE `deleted` = '0' AND `grId` = '{$grId}' ORDER BY `id` DESC";
- $result = $this->db->Execute($query);
- $num = mysql_num_rows($result);
- if ($num > 0) {
- $content = '<table class="table table-hover">
- <tr>
- <th>Дата создания</th>
- <th>Действие</th>
- <th>Кампания(источник)</th>
- <th>Кампания(цель)</th>
- <th>Цикл на день</th>
- <th>Делается каждые</th>
- <td> </td>
- <td> </td>
- </tr>';
- while ($row = mysql_fetch_object($result)) {
- $action = (($row->action == '0') ? 'копирование' : 'перемещение');
- $campaign = $this->getCampaignById($row->campaign);
- $campaignTarget = $this->getCampaignById($row->campaignTarget);
- //$campaign = var_export($campaign, true);
- $content .= '<tr id="process_'.$row->id.'">
- <td>'.$row->date.'</td>
- <td>'.$action.'</td>
- <td>'.$campaign[$row->campaign]['name'].'</td>
- <td>'.$campaignTarget[$row->campaignTarget]['name'].'</td>
- <td>'.$row->cycleDay.'</td>
- <td>'.$row->eachDay.'</td>
- <td>
- <button onClick="locate(\'/getresponse/newForm/'.$row->id.'/\')" type="button" class="btn btn-success btn-sm">
- <span class="glyphicon glyphicon-pencil"></span>
- </button>
- </td>
- <td>
- <button onClick="deleteProcess('.$row->id.')" type="button" class="btn btn-danger btn-sm">
- <span class="glyphicon glyphicon-remove"></span>
- </button>
- </td>
- </tr>';
- }
- $content .= '</table>';
- return $content;
- }
- return 'На данный момент нет ниодного процесса';
- }
- /**
- * Вывод GetResponse профайлов в массив
- * @return array список профайлов
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- *
- */
- public function getResponseProfiles() {
- $data = array();
- $query = "SELECT * FROM `api_data` WHERE `deleted` = '0'";
- $result = $this->db->Execute($query);
- $num = mysql_num_rows($result);
- if ($num > 0) {
- while ($row = mysql_fetch_object($result)) {
- $data[] = array('id' => $row->id,
- 'name' => $row->name,
- 'apikey' => $row->apikey,
- 'apiurl' => $row->apiurl,
- 'default' => $row->default);
- }
- return $data;
- }
- else
- return false;
- }
- /**
- * VIEW Вывод всех профайлов
- * @param array $data
- * @return string
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function viewResponseProfiles($data) {
- if ($data === false) $content = 'На данный момент нет GetResponse профайлов';
- else {
- $cData = count($data);
- $content = '<table class="table table-hover">
- <tr>
- <th>Название</th>
- <th>API Key</th>
- <th>API Url</th>
- <td> </td>
- <td> </td>
- <td> </td>
- </tr>';
- for ($i = 0; $i < $cData; $i++) {
- $defaultColor = (($data[$i]['default'] == '0') ? 'btn-danger' : 'btn-success');
- $defaultIcon = (($data[$i]['default'] == '0') ? 'glyphicon-remove-circle' : 'glyphicon-ok-circle');
- $content .= '<tr id="profile_'.$data[$i]['id'].'">
- <td>'.$data[$i]['name'].'</td>
- <td>'.$data[$i]['apikey'].'</td>
- <td>'.$data[$i]['apiurl'].'</td>
- <td>
- <button onClick="locate(\'/getresponseProfiles/default/'.$data[$i]['id'].'/\')" type="button" class="btn '.$defaultColor.' btn-sm">
- <span class="glyphicon '.$defaultIcon.'"></span>
- </button>
- </td>
- <td>
- <button onClick="locate(\'/getresponseProfiles/newForm/'.$data[$i]['id'].'/\')" type="button" class="btn btn-primary btn-sm">
- <span class="glyphicon glyphicon-pencil"></span>
- </button>
- </td>
- <td>
- <button onClick="deleteProfile('.$data[$i]['id'].')" type="button" class="btn btn-danger btn-sm">
- <span class="glyphicon glyphicon-remove"></span>
- </button>
- </td>
- </tr>';
- }
- $content .= '</table>';
- }
- return $content;
- }
- /**
- * Удаляет профайл
- * @param int $id - ID профайла
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function deleteProfile($id) {
- $query = "UPDATE `api_data` SET `deleted` = '1' WHERE `id` = '{$id}'";
- $result = $this->db->Execute($query);
- }
- /**
- * Создает профайл
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function addProfile($data) {
- $query = "INSERT INTO `api_data` (`id`, `name`, `apikey`, `apiurl`, `default`, `deleted`)
- VALUES (NULL, '{$data['name']}', '{$data['apiKey']}', '{$data['apiUrl']}', '0', '0');";
- $result = $this->db->Execute($query);
- }
- /**
- * Выводит информацию о профайле
- * @param int $id
- * @return array
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getProfile($id) {
- $query = "SELECT * FROM `api_data` WHERE `id` = '{$id}'";
- $result = $this->db->Execute($query);
- $row = mysql_fetch_object($result);
- $profile = array(
- 'id' => $row->id,
- 'name' => $row->name,
- 'apiKey' => $row->apikey,
- 'apiUrl' => $row->apiurl,
- 'default' => $row->default
- );
- return $profile;
- }
- /**
- * Редактирует профайл
- * @return void
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function editProfile($data) {
- $query = "UPDATE `api_data` SET `name` = '{$data['name']}',
- `apikey` = '{$data['apiKey']}',
- `apiurl` = '{$data['apiUrl']}'
- WHERE `id` ='{$data['id']}'";
- $result = $this->db->Execute($query);
- }
- /**
- * Выводит список профайлов
- * @return string
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function viewProfilesList() {
- $profile = $this->getResponseProfiles();
- if ($profile === false) $content = '<ul class="dropdown-menu"><li>На данный момент нет GetResponse профайлов</li></ul>';
- else {
- $cData = count($profile);
- $content = '<ul class="dropdown-menu">';
- for ($i = 0; $i < $cData; $i++) {
- //TODO: проверить на работоспособность
- $icon = ( isset($_SESSION['grProfile']) ? (($profile[$i]['id'] == $_SESSION['grProfile']['id']) ? 'glyphicon-ok' : '') : (($profile[$i]['default'] == '1') ? 'glyphicon-ok' : '') );
- $content .= '<li>
- <a href="/changeProfile/'.$profile[$i]['id'].'/"><i class="glyphicon '.$icon.'"></i> '.$profile[$i]['name'].'</a>
- </li>';
- if (($i+1) < $cData) $content .= '<li class="divider"></li>';
- }
- $content .= '</ul>';
- }
- return $content;
- }
- /**
- * Выводит данные профайла по умолчанию
- * @return array
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getDefaultProfile() {
- $query = "SELECT * FROM `api_data` WHERE `default` = '1' AND `deleted` = '0'";
- $result = $this->db->Execute($query);
- $row = mysql_fetch_object($result);
- $data = array(
- 'id' => $row->id,
- 'name' => $row->name,
- 'apiKey' => $row->apikey,
- 'apiUrl' => $row->apiurl
- );
- return $data;
- }
- /**
- * Смена профайла
- * @param integer $id
- * @return array
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function changeProfile($id) {
- $query = "SELECT * FROM `api_data` WHERE `id` = '{$id}'";
- $result = $this->db->Execute($query);
- $row = mysql_fetch_object($result);
- $data = array(
- 'id' => $row->id,
- 'name' => $row->name,
- 'apiKey' => $row->apikey,
- 'apiUrl' => $row->apiurl
- );
- return $data;
- }
- /**
- * Выводит данные всех профайлов
- * @return array
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getProfiles() {
- $query = "SELECT * FROM `api_data` WHERE `deleted` = '0'";
- $result = $this->db->Execute($query);
- $num = mysql_num_rows($result);
- if ($num > 0) {
- $profiles = array();
- while ($row = mysql_fetch_object($result)) {
- $profiles[] = array(
- 'id' => $row->id,
- 'name' => $row->name,
- 'apiKey' => $row->apikey,
- 'apiUrl' => $row->apiurl
- );
- }
- } else return false;
- }
- /**
- * Вывод всех процессов в массив для запуска крона
- * @return array список процессов
- * @author Игорь Быра <ihorbyra@gmail.com>
- * @version 1.0
- */
- public function getProcessesForCron($grId) {
- $cronjob = array();
- $query = "SELECT * FROM `campaign_actions` WHERE `deleted` = '0' AND `grId` = '{$grId}' ORDER BY `id` DESC";
- $result = $this->db->Execute($query);
- $num = mysql_num_rows($result);
- if ($num > 0) {
- while ($row = mysql_fetch_object($result)) {
- $cronjob[] = array('id' => $row->id,
- 'campaign' => $row->campaign,
- 'campaignTarget' => $row->campaignTarget,
- 'cycleDay' => $row->cycleDay,
- 'eachDay' => $row->eachDay,
- 'date' => $row->date);
- }
- }
- return $cronjob;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment