06-14-2015, 11:01 PM
Hi;
I load a custom library like this and call a method there. But for some reason it loads it twice. For example all stats are stored twice, or if I put a simple mail function there, it sends it twice. What am I doing wrong besides being a good Catholic? Thanks
I load a custom library like this and call a method there. But for some reason it loads it twice. For example all stats are stored twice, or if I put a simple mail function there, it sends it twice. What am I doing wrong besides being a good Catholic? Thanks
PHP Code:
$this->load->library('siter_page_sampler_library');
$this->siter_page_sampler_library->loader($short_url);
PHP Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class siter_page_sampler_library
{
private $not_http = array('the_phone_number', 'the_customer_service', 'the_office_phone', 'the_sms', 'the_skype','the_email');
public function loader($short_url)
{
$CI = & get_instance();
$query = $CI->db->get_where('samplers', array(
'short_url' => $short_url
));
if ($query->num_rows() != 1)
{
return false;
}
else
{
$CI->load->model('model_auth');
$CI->load->model('custom_buttons/Custom_Button_Bucket');
$CI->load->model('custom_buttons/Custom_Button_Attachment');
$CI->load->model('custom_buttons/Custom_Button_Phone');
$CI->load->model('custom_buttons/Custom_Button_URL');
$CI->load->model('custom_buttons/Custom_Button_Video');
$CI->load->model('custom_buttons/Custom_Button_Text');
$CI->load->model('sampler');
$CI->load->model('sampler_template');
$CI->load->model('user_uploaded_image/Background_Image');
$CI->load->model('user_uploaded_image/Logo_Image');
$CI->load->model('user_uploaded_image/sampler_Image');
$data['siter_buttons'] = $CI->model_auth->siter_buttons();
$data['meta'] = '<meta name="type" content="bc">';
$details['card_details'] = $CI->model_auth->card_details($short_url);
$val = $details['card_details'][0];
/**
* @var sampler $sampler
*/
$sampler = new $CI->sampler();
$sampler->setId($val->CID);
$sampler->load();
$data['sampler'] = $sampler;
/**
* @var sampler_Template $sampler_template
*/
$sampler_template = new $CI->sampler_template();
$sampler_template->setId($sampler->getFkCardTemplateId());
$sampler_template->load();
$data['sampler_template'] = $sampler_template;
$columns = get_object_vars($val);
foreach($columns as $val => $row)
{
$data[$val] = (empty($row)) ? '' : $row;
}
// print_r($data['button_sort']);echo "<hr />";
$CID = isset($columns['CID']) ? $columns['CID'] : 0;
if ($sampler->getButtonFormat()) $dominant = $sampler;
else $dominant = $sampler_template;
if ($dominant->getButtonFormat() == 2) $grid = FALSE;
else $grid = TRUE;
if ($grid == TRUE)
{
$data['ul_class'] = 'buttons grid';
$data['icon_css'] = ".fa {display: none;}";
$data['button_format'] = 'grid-button';
$data['background_color_button_holder'] = "transparent";
}
else
{
$data['ul_class'] = 'buttons list';
$data['icon_css'] = "";
}
$data['button_background_color'] = $dominant->getButtonBackgroundColor();
$data['button_text_color'] = $dominant->getButtonTextColor();
$data['bg_header_color'] = $dominant->getBgHeaderColor();
$data['bg_main_color'] = $dominant->getBgMainColor();
$data['short_url'] = $sampler->getShortUrl();
if ($sampler->getsamplerFile())
{
$sampler_img = new $CI->sampler_Image();
$sampler_img->load($sampler->getsamplerFile());
$data['sampler_picture'] = $sampler_img->url;
}
else
{
$data['sampler_picture'] = '';
}
if ($sampler->getLogoName() || $sampler_template->getLogo())
{
$logo = $sampler_template->getLogo();
if ($sampler->getLogoName()) $logo = $sampler->getLogoName();
$logo_img = new $CI->Logo_Image();
$logo_img->load($logo);
$data['logo_picture'] = $logo_img->url;
}
else
{
$data['logo_picture'] = '';
}
$data['user_emailAddress'] = $sampler->getEmail();
if ($columns['request_meeting'] == '0000-00-00')
{
$data['request_meeting'] = "";
}
else
{
$data['request_meeting'] = $columns['request_meeting'];
}
if ($columns['request_meeting'] == '0000-00-00')
{
$data['calendar'] = "";
}
else
{
$data['calendar'] = $columns['calendar'];
}
// Images
$data['siter_buttons'] = $CI->model_auth->siter_buttons();
$external_links = array(
'twitter',
'facebook',
'linkedin',
'instagram',
'website',
'promotion',
'google_plus',
'add_weblink'
);
$data['request_meeting_form_display'] = "none";
if ($sampler->getBgImage() || $sampler_template->getBackgroundImage())
{
$bg = $sampler_template->getBackgroundImage();
if ($sampler->getBgImage()) $bg = $sampler->getBgImage();
$bg_img = new $CI->Background_Image();
$bg_img->load($bg);
$data['bg_image'] = $bg_img->url;
}
else
{
$data['bg_image'] = '';
}
foreach($data['siter_buttons'] as $val => $row)
{
if ($columns['button_style'] == 1)
{
$data['button_format_div_type'] = 'light';
$data['button_class'] = 'light';
$row->button_image = base_url("components/img/phone/dark_" . $row->dark_image . ".png");
}
elseif ($columns['button_style'] == 2)
{
$data['button_format_div_type'] = 'dark';
$data['button_class'] = 'dark';
$row->button_image = base_url("components/img/phone/light_" . $row->dark_image . ".png");
}
else
{
$data['button_format_div_type'] = 'light';
$data['button_class'] = 'light';
$row->button_image = base_url("components/img/phone/dark_" . $row->dark_image . ".png");
}
if (strlen($columns[$row->card_details_column]) < 1)
{
unset($data['siter_buttons'][$val]);
}
else
{
if ($row->button_id != 'the_email')
{
if(isset($columns[$row->card_details_column]))
{
$row->link.= $columns[$row->card_details_column];
}
if(strpos($row->link, 'http') === false )
{
foreach($this->not_http as $no_http)
{
if(strpos($row->link, $row->link) === false)
{
$row->link = "http://" . $row->link;
}
}
}
}
if ($row->button_id == "the_add_contact")
{
$row->link = str_replace($columns[$row->card_details_column], "", $row->link);
$row->link = $row->link . $columns['short_url'];
}
if ($row->button_id == "the_address")
{
$row->link = "http://www.google.com/maps/preview?q=" . $columns['address'];
}
$time_zone = timezone_identifiers_list();
$timezone_identifiers = array();
foreach($time_zone as $i)
{
$data['timezone_identifiers'][$i] = $i;
}
$data['selected_region'] = (empty($CI->input->post['timezone'])) ? 'Australia/Brisbane' : $CI->input->post('timezone');
}
}
$siter_buttons = array();
foreach($data['siter_buttons'] as $val => $row)
{
$siter_buttons[$row->button_id] = $row;
}
$data['siter_buttons'] = $siter_buttons;
$data['sent_successfully'] = "";
$data['scroll'] = "";
if ($_POST)
{
$CI->load->library('form_validation');
$CI->form_validation->set_error_delimiters('<div style = "background-color:white; color:red; padding:5px 10px; border:1px solid #999999; border-radius: 5px;; margin-bottom:2px">', '</div>');
// $CI->form_validation->set_rules('title', 'Title', 'required');
$CI->form_validation->set_rules('email', 'Email', 'valid_email|required');
$CI->form_validation->set_rules('date', 'Date', 'required');
$CI->form_validation->set_rules('time', 'Time', 'required');
$CI->form_validation->set_rules('timezone', 'Timezone', 'required');
if ($CI->form_validation->run() == FALSE)
{
$data['request_meeting_form_display'] = "block";
}
else
{
$CI->load->helper('file');
$starts = date('Y-m-d', strtotime($CI->input->post('date'))) . " " . $CI->input->post('time');
// echo "<hr />";
$starts = date('Ymd\THis', strtotime($starts));
$location = $CI->input->post('timezone');
$description = "Meeting requested";
$title = $CI->input->post('title');
$string = "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT\nDTSTART:" . date("Ymd\THis\Z", strtotime($starts)) . "\nDTEND:" . date("Ymd\THis\Z", strtotime($starts)) . "\nLOCATION:" . $location . "\nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:" . date("Ymd\THis\Z") . "\nSUMMARY:" . $title . "\nDESCRIPTION:" . $description . "\nPRIORITY:1\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10080M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR\n";
$file_name = uniqid();
if (!write_file('uploads/' . $file_name . '.ics', $string))
{
echo "Couldn't write";
}
else
{
$CI->load->helper('download');
$ics_file = read_file('uploads/' . $file_name . '.ics');
// force_download($file_name.'.ics', $ics_file);
}
$data['sent_successfully'] = " Request meeting has been successfully sent to " . html_escape($CI->input->post('email'));
$CI->load->library('email');
$config['mailtype'] = 'html';
$CI->email->initialize($config);
$CI->email->from('[email protected]', 'siter');
$CI->email->to($details['card_details'][0]->email);
$CI->email->subject('You have a meeting request');
$CI->email->message('Hi<br /><br />You have received a meeting request via your siter Business Card from <strong>' . $CI->input->post('email') . '</strong>.<br /><br />
Title: ' . $CI->input->post('title') . '<br /><br />
Date: ' . $CI->input->post('date') . '<br /><br />
Time: ' . $CI->input->post('time') . ' (' . $CI->input->post('timezone') . ')<br /><br />
Thank you<br /><br />
<a href = "http://www.siter.com">siter Team</a>');
$config['mailtype'] = 'html';
if (file_exists('uploads/' . $file_name . '.ics'))
{
$CI->email->attach('uploads/' . $file_name . '.ics');
}
else
{
echo "File doesnt exists";
}
$CI->email->send();
}
}
// Detect text color for header
$colour = '#' . $data['header_colour'];
$CI->load->library('colordetector');
$rgb = $CI->colordetector->HTMLToRGB($colour);
$hsl = $CI->colordetector->RGBToHSL($rgb);
if ($hsl->lightness > 100)
{
$data['header_text_color_detected'] = "222222";
}
else
{
$data['header_text_color_detected'] = "CCCCCC";
}
// Detect text color for body
$colour = '#' . $data['bg_header_color'];
$rgb = $CI->colordetector->HTMLToRGB($colour);
$hsl = $CI->colordetector->RGBToHSL($rgb);
$hsl->lightness;
if ($hsl->lightness > 100)
{
$data['card_text_color_detected'] = "222222";
}
else
{
$data['card_text_color_detected'] = "CCCCCC";
}
$data['id'] = $CID;
$custom_buttons = $CI->Custom_Button_Bucket->getCustomButtons('sampler', $data['id']);
foreach($custom_buttons as $custom_button)
{
$data['siter_buttons']['custom-button-' . $custom_button->getId() ] = $custom_button;
}
$CI->siter_page_sampler_library->save_stats($CID);
$data['not_http'] = $this->not_http;
$data['sampler_id'] = $CID;
$CI->load->view('sampler_page_view2', $data);
}
}
private function save_stats($CID)
{
$CI = & get_instance();
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";
$ub = "Unknown";
// $matches = array();
// First get the platform?
if (preg_match('/linux/i', $u_agent))
{
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent))
{
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent))
{
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif (preg_match('/Firefox/i', $u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif (preg_match('/Chrome/i', $u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif (preg_match('/Safari/i', $u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif (preg_match('/Opera/i', $u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif (preg_match('/Netscape/i', $u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}
// finally get the correct version number
$known = array(
'Version',
$ub,
'other'
);
$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches))
{
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1)
{
// we will have two since we are not using 'other' argument yet
// see if version is before or after the name
if (strripos($u_agent, "Version") < strripos($u_agent, $ub))
{
$version = $matches['version'][0];
}
else
{
// $version= $matches['version'][1];
$version = "?";
}
}
else
{
$version = $matches['version'][0];
}
// check if we have a number
if ($version == null || $version == "")
{
$version = "?";
}
$devices = array('iphone', 'ipad', 'android', 'windows', 'macintosh');
$device = 'other';
foreach($devices as $val)
{
if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']),$val) !== false)
{
$device = $val;
if($device =='macintosh')
{
$device = "Mac";
}
}
}
$browser = array(
'userAgent' => $_SERVER['HTTP_USER_AGENT'],
'name' => $bname,
'version' => $version,
'platform' => $platform
);
$data_stat = array(
'device' => $device,
'item_type' => 'sampler',
'date' => date('Y-m-d'),
'time' => time() ,
'item_id' => $CID,
'userAgent' => $browser['userAgent'],
'name' => $browser['name'],
'version' => $browser['version'],
'platform' => $browser['platform']
);
$CI->db->insert('statistics', $data_stat);
}
}