init
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
include_once __DIR__ . "/mysql-database-service.php";
|
||||
|
||||
class CampaignModel extends MySqlDatabaseService
|
||||
{
|
||||
protected $_table = 'campaign';
|
||||
protected $_primary_key = 'id';
|
||||
protected $_return_type = 'array';
|
||||
protected $_allowed_fields = [
|
||||
'id', 'name', 'file_id', 'data', 'user_id', 'created_at', 'updated_at'
|
||||
];
|
||||
protected $_label_fields = [
|
||||
'ID', 'Name', 'File ID', 'Data', 'User ID', 'Created At', 'Updated At'
|
||||
];
|
||||
protected $_use_timestamps = true;
|
||||
protected $_created_field = 'created_at';
|
||||
protected $_updated_field = 'updated_at';
|
||||
protected $_validation_rules = [
|
||||
['name', 'Name', 'required'],
|
||||
['file_id', 'File ID', 'required'],
|
||||
['data', 'Data', ''],
|
||||
['user_id', 'User ID', 'required'],
|
||||
['created_at', 'Created At', 'required'],
|
||||
['updated_at', 'Updated At', 'required']
|
||||
];
|
||||
|
||||
protected $_validation_edit_rules = [
|
||||
['name', 'Name', 'required'],
|
||||
['file_id', 'File ID', 'required'],
|
||||
['data', 'Data', ''],
|
||||
['user_id', 'User ID', 'required'],
|
||||
['updated_at', 'Updated At', 'required']
|
||||
];
|
||||
|
||||
protected $_validation_messages = [
|
||||
['name', 'Name ', 'required'],
|
||||
['file_id', 'File ID', 'required'],
|
||||
['data', 'Data', ''],
|
||||
['user_id', 'User ID', 'required'],
|
||||
['updated_at', 'Updated At', 'required']
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function get_mapping()
|
||||
{
|
||||
return [
|
||||
// TODO: ADD MAPPING
|
||||
];
|
||||
}
|
||||
|
||||
public function csvToObject($csvString) {
|
||||
$lines = explode("\n", $csvString);
|
||||
$headers = array_map(function($header) {
|
||||
return str_replace(' ', '_', trim(strtolower($header)));
|
||||
}, explode(',', $lines[0])); // Convert headers to snake case
|
||||
|
||||
$object = [];
|
||||
|
||||
foreach ($headers as $header) {
|
||||
$object[$header] = [];
|
||||
}
|
||||
|
||||
for ($i = 1; $i < count($lines) - 1; $i++) {
|
||||
$values = explode(',', $lines[$i]);
|
||||
for ($j = 0; $j < count($headers) && $j < count($values); $j++) {
|
||||
$value = trim($values[$j]);
|
||||
if ($headers[$j] == 'duration' && $value === "-") {
|
||||
$object[$headers[$j]][] = '00:00';
|
||||
continue;
|
||||
}
|
||||
$object[$headers[$j]][] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function getFilteredData($campaign, $filters) {
|
||||
$config = MkdConfig::get_instance()->get_config();
|
||||
$userModel = new UserModel();
|
||||
$user = $userModel->get($_SESSION['user']);
|
||||
|
||||
$oauth = new \Lib\Google\GoogleOAuth2(
|
||||
$config['google_client_id'],
|
||||
$config['google_client_secret'],
|
||||
$config['google_redirect_uri']
|
||||
);
|
||||
|
||||
$oauth->setRefreshToken($user->drive_refresh_token);
|
||||
$oauth->refreshAccessToken();
|
||||
|
||||
$drive = new \Lib\Google\GoogleDrive($oauth);
|
||||
|
||||
// Download and parse the file
|
||||
$content = $drive->downloadFile($campaign->file_id, 'text/csv');
|
||||
$data = $this->csvToObject($content);
|
||||
|
||||
// Apply filters
|
||||
$validRows = range(0, count($data['date']) - 1);
|
||||
|
||||
if ($filters['campaign_name']) {
|
||||
$validRows = array_filter($validRows, function($i) use ($data, $filters) {
|
||||
return $data['campaign_name'][$i] === $filters['campaign_name'];
|
||||
});
|
||||
}
|
||||
|
||||
if ($filters['ad_set_name']) {
|
||||
$validRows = array_filter($validRows, function($i) use ($data, $filters) {
|
||||
return $data['ad_set_name'][$i] === $filters['ad_set_name'];
|
||||
});
|
||||
}
|
||||
|
||||
if ($filters['ad_name']) {
|
||||
$validRows = array_filter($validRows, function($i) use ($data, $filters) {
|
||||
return $data['ad_name'][$i] === $filters['ad_name'];
|
||||
});
|
||||
}
|
||||
|
||||
// Filter the data
|
||||
$filteredData = [];
|
||||
foreach ($data as $column => $values) {
|
||||
$filteredData[$column] = array_intersect_key($values, array_flip($validRows));
|
||||
}
|
||||
|
||||
return $filteredData;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user