<?php
// GET CORRECT LOCALE VALUE
function bpr_field($data, $locale = '') {
if ($locale == '') {
$locale = osc_current_user_locale();
}
$value = @$data[$locale];
if($value == '') {
$value = @$data[osc_language()];
if($value == '') {
$aLocales = osc_get_locales();
foreach($aLocales as $locale) {
$value = @$data[@$locale['pk_c_code']];
if($value != '') {
break;
}
}
}
}
return (string) $value;
}
// GET COMPANIES LIST
function bpr_show_companies() {
require osc_base_path() . 'oc-content/plugins/business_profile/form/home.php';
}
// GET USER BUSINESS TYPE
function bpr_get_type($user_id = -1) {
if($user_id < 0) {
$user_id = osc_logged_user_id();
}
if($user_id > 0) {
$seller = ModelBPR::newInstance()->getSellerByUserId($user_id);
if(isset($seller['pk_i_id']) && $seller['pk_i_id'] > 0) {
if($seller['b_enabled'] == 1) {
return $seller['i_type'];
}
}
}
return -1;
}
// CHECK IF USER IS BUSINESS ONE
function bpr_is_business($user_id = -1) {
if($user_id < 0) {
$user_id = osc_logged_user_id();
}
if($user_id > 0) {
$seller = ModelBPR::newInstance()->getSellerByUserId($user_id);
if(isset($seller['pk_i_id']) && $seller['pk_i_id'] > 0) {
if($seller['b_enabled'] == 1) {
return true;
}
}
}
return false;
}
// REDIRECT PUBLIC PROFILE TO BUSINESS PROFILE IF EXISTS
function bpr_public_to_business() {
$location = Rewrite::newInstance()->get_location();
$section = Rewrite::newInstance()->get_section();
if($location == 'user' && $section = 'pub_profile') {
if(Params::getParam('username') <> '' || Params::getParam('id') > 0) {
$user = User::newInstance()->findByPrimaryKey(Params::getParam('id'));
if(!isset($user['pk_i_id'])) {
$user = User::newInstance()->findByUsername(Params::getParam('username'));
}
if(isset($user['pk_i_id']) && $user['pk_i_id'] > 0) {
$seller = ModelBPR::newInstance()->getSellerByUserId($user['pk_i_id']);
if(isset($seller['pk_i_id']) && $seller['b_enabled'] == 1) {
header('Location:' . osc_route_url('bpr-seller', array('identifier' => $seller['s_identifier'])));
exit;
}
}
}
}
}
osc_add_hook('init', 'bpr_public_to_business');
// CHECK USER IF PREMIUM
function bpr_control_user($seller) {
if(isset($seller['pk_i_id'])) {
if(bpr_check_premium()) {
$groups = explode(',', bpr_check_premium());
$user_group = osp_get_user_group($seller['fk_i_user_id']);
if(in_array($user_group, $groups)) {
return true;
}
}
}
return false;
}
// CHECK IF PREMIUM GROUPS ARE SET
function bpr_check_premium() {
if(function_exists('osp_param')) {
if(osp_param('groups_enabled') == 1) {
if(bpr_param('premium_groups') <> '') {
return bpr_param('premium_groups');
}
}
}
return false;
}
// GET PREMIUM GROUPS
function bpr_premium_groups() {
if(bpr_check_premium()) {
$return = array();
$groups = explode(',', bpr_check_premium());
if(count($groups) > 0) {
foreach($groups as $id) {
$return[] = ModelOSP::newInstance()->getGroup($id);
}
return $return;
}
}
return false;
}
// CHECK IF PROFILES USER IS IN PREMIUM GROUP
function bpr_control_premium($seller) {
if(isset($seller['pk_i_id'])) {
if(bpr_check_premium()) {
$groups = explode(',', bpr_check_premium());
$user_group = osp_get_user_group($seller['fk_i_user_id']);
// Check group attribute -> update profile type if in (1,2,3)
if(in_array($user_group, $groups)) {
$u_group = ModelOSP::newInstance()->getGroup($user_group);
if(($u_group['i_attr'] <> $seller['i_type']) && ($u_group['i_attr'] == 1 || $u_group['i_attr'] == 2 || $u_group['i_attr'] == 3)) {
ModelBPR::newInstance()->updateProfileType($seller['pk_i_id'], $u_group['i_attr']);
}
if(in_array($user_group, $groups) && $seller['b_enabled'] == 1) {
return true;
}
}
// Deactivate
if(!in_array($user_group, $groups) && $seller['b_enabled'] == 1) {
ModelBPR::newInstance()->deactivateProfile($seller['pk_i_id']);
return true;
}
// Activate
if(in_array($user_group, $groups) && $seller['b_enabled'] == 0 && bpr_param('auto_validate') == 1) {
ModelBPR::newInstance()->activateProfile($seller['pk_i_id']);
// Check group attribute -> update profile type if in (1,2,3)
if(in_array($user_group, $groups)) {
$u_group = ModelOSP::newInstance()->getGroup($user_group);
if(($u_group['i_attr'] <> $seller['i_type']) && ($u_group['i_attr'] == 1 || $u_group['i_attr'] == 2 || $u_group['i_attr'] == 3)) {
ModelBPR::newInstance()->updateProfileType($seller['pk_i_id'], $u_group['i_attr']);
}
}
return true;
}
}
}
return false;
}
// UPDATE COLORS BASED ON COMPANY ONE
function bpr_update_theme_colors() {
$html = '';
$location = Rewrite::newInstance()->get_location();
$section = Rewrite::newInstance()->get_section();
$identifier = Params::getParam('identifier');
$seller = ModelBPR::newInstance()->getSellerByIdentifier($identifier);
if($location == 'bpr' && $section == 'seller' && isset($seller['s_color']) && bpr_validate_color($seller['s_color']) && bpr_param('selectors') <> '') {
$html .= '<style>';
$html .= bpr_param('selectors') . ' {background:' . bpr_validate_color($seller['s_color']) . '!important;}';
$html .= '</style>';
}
echo $html;
}
osc_add_hook('footer', 'bpr_update_theme_colors', 10);
// VALIDATE HEX COLOR
function bpr_validate_color($color) {
if(preg_match('/^#[a-f0-9]{6}$/i', $color)){
return $color;
} else if(preg_match('/^[a-f0-9]{6}$/i', $color)) {
return '#' . $color;
}
return false;
}
// GET COMPANY ITEMS URL
function bpr_company_items_url($user_id = -1) {
if($user_id < 0) {
$user_id = osc_item_user_id();
}
return osc_search_url(array('user' => $user_id));
}
// GET COMPANIES URL
function bpr_companies_url() {
return osc_route_url('bpr-list');
}
// GET COMPANY URL
function bpr_company_url($user_id = -1) {
if($user_id < 0) {
$user_id = osc_item_user_id();
}
if($user_id > 0) {
$seller = ModelBPR::newInstance()->getSellerByUserId($user_id);
if(isset($seller['s_identifier']) && $seller['s_identifier'] <> '') {
return osc_route_url('bpr-seller', array('identifier' => $seller['s_identifier']));
}
}
return false;
}
// GET COMPANY COLOR
function bpr_company_color($user_id = -1) {
if($user_id < 0) {
$user_id = osc_item_user_id();
}
if($user_id > 0) {
$seller = ModelBPR::newInstance()->getSellerByUserId($user_id);
if(isset($seller['s_color']) && $seller['s_color'] <> '' && bpr_validate_color($seller['s_color'])) {
return bpr_validate_color($seller['s_color']);
}
}
return false;
}
// GET COVER IMAGE
function bpr_get_img($img = 'none.png', $type = 'icon') {
if($img == '') {
$img = 'none.png';
}
$img_url = osc_base_url() . 'oc-content/plugins/business_profile/img/';
$img_path = osc_base_path() . 'oc-content/plugins/business_profile/img/' . $type . '/';
if(strpos($img, '?')) {
$img_name = substr($img, 0, strpos($img, '?'));
} else {
$img_name = $img;
}
if(file_exists($img_path . $img_name)) {
return $img_url . $type . '/' . $img;
} else {
return $img_url . $type .'_default.' . ($type == 'icon' ? 'png' : 'jpg');
}
}
// GET USER IMAGE
function bpr_get_user_img($user_id = -1) {
if($user_id < 0) {
$user_id = osc_item_user_id();
}
$seller = ModelBPR::newInstance()->getSellerByUserId($user_id);
if(isset($seller['s_icon']) && $seller['s_icon'] <> '') {
return bpr_get_img($seller['s_icon'], 'icon');
} else {
return bpr_get_img('none.png', 'icon');
}
}
// META TITLE
function bpr_meta_title($tag) {
$location = Rewrite::newInstance()->get_location();
$section = Rewrite::newInstance()->get_section();
if($location == 'bpr') {
if($section == 'home') {
$tag = __('Companies', 'business_profile') . ' - ' . osc_page_title();
} else if ($section == 'seller') {
$identifier = Params::getParam('identifier');
$seller = ModelBPR::newInstance()->getSellerByIdentifier($identifier);
$user = User::newInstance()->findByPrimaryKey($seller['fk_i_user_id']);
if(@$user['s_name'] <> '') {
$tag = sprintf(__('%s\'s business profile', 'business_profile'), $user['s_name']) . ' - ' . osc_page_title();
} else {
$tag = __('Business profile does not exists', 'business_profile') . ' - ' . osc_page_title();
}
}
}
return $tag;
}
osc_add_filter('meta_title_filter', 'bpr_meta_title', 1);
// META DESCRIPTION
function bpr_meta_description($tag) {
$location = Rewrite::newInstance()->get_location();
$section = Rewrite::newInstance()->get_section();
if($location == 'bpr') {
if($section == 'home') {
$tag = __('Companies and corporates selling on our classifieds.', 'business_profile');
} else if ($section == 'seller') {
$identifier = Params::getParam('identifier');
$seller = ModelBPR::newInstance()->getSellerByIdentifier($identifier);
$user = User::newInstance()->findByPrimaryKey($seller['fk_i_user_id']);
View::newInstance()->_exportVariableToView('user', $user);
$tag = osc_user_info();
}
}
return $tag;
}
osc_add_filter('meta_description_filter', 'bpr_meta_description', 1);
// DRAW ITEM
function bpr_draw_item($c = NULL, $view = 'gallery', $premium = false, $class = false) {
$filename = 'loop-single';
if(bpr_draw_check()) {
require WebThemes::newInstance()->getCurrentThemePath() . 'loop-single.php';
}
}
// CHECK IF ITEMS CAN BE DRAWN
function bpr_draw_check() {
if(file_exists(WebThemes::newInstance()->getCurrentThemePath() . 'loop-single.php')) {
return true;
}
return false;
}
// GET DAY SHORTCUT
function bpr_day($id, $type = 'short') {
if($id == 1) {
return ($type == 'short' ? __('Mo', 'business_profile') : __('Monday', 'business_profile'));
} else if($id == 2) {
return ($type == 'short' ? __('Tu', 'business_profile') : __('Tuesday', 'business_profile'));
} else if($id == 3) {
return ($type == 'short' ? __('We', 'business_profile') : __('Wednesday', 'business_profile'));
} else if($id == 4) {
return ($type == 'short' ? __('Th', 'business_profile') : __('Thursday', 'business_profile'));
} else if($id == 5) {
return ($type == 'short' ? __('Fr', 'business_profile') : __('Friday', 'business_profile'));
} else if($id == 6) {
return ($type == 'short' ? __('Sa', 'business_profile') : __('Saturday', 'business_profile'));
} else if($id == 7) {
return ($type == 'short' ? __('Su', 'business_profile') : __('Sunday', 'business_profile'));
}
}
// GET SOCIAL ICON
function bpr_soc_icon($id, $type = 'icon') {
if($id == 'fb') {
return ($type == 'icon' ? 'fa-facebook' : __('Facebook', 'business_profile'));
} else if($id == 'tw') {
return ($type == 'icon' ? 'fa-twitter' : __('Twitter', 'business_profile'));
} else if($id == 'vm') {
return ($type == 'icon' ? 'fa-vimeo' : __('Vimeo', 'business_profile'));
} else if($id == 'yt') {
return ($type == 'icon' ? 'fa-youtube' : __('Youtube', 'business_profile'));
} else if($id == 'li') {
return ($type == 'icon' ? 'fa-linkedin' : __('LinkedIn', 'business_profile'));
} else if($id == 'ig') {
return ($type == 'icon' ? 'fa-instagram' : __('Instagram', 'business_profile'));
} else if($id == 'pn') {
return ($type == 'icon' ? 'fa-pinterest-p' : __('Pinterest', 'business_profile'));
} else if($id == 'gp') {
return ($type == 'icon' ? 'fa-google-plus' : __('Google Plus', 'business_profile'));
} else { // 'ot' option
return ($type == 'icon' ? 'fa-share' : __('Share', 'business_profile'));
}
}
// GET USER TYPE
function bpr_user_type($type) {
if($type == 1) {
echo __('Basic', 'business_profile');
} else if ($type == 2) {
echo __('Pro', 'business_profile');
} else if ($type == 3) {
echo __('VIP', 'business_profile');
}
}
// CREATE LOCALE SELECT BOX
function bpr_locale_box( $file, $param = '', $id = -1 ) {
$html = '';
$locales = OSCLocale::newInstance()->listAllEnabled();
$current = bpr_get_locale();
if($id > 0 && $param <> '') {
$id_string = '&' . $param . '=' . $id;
}
$html .= '<select rel="' . osc_admin_base_url(true) . '?page=plugins&action=renderplugin&file=business_profile/admin/' . $file . $id_string . '" class="mb-select mb-select-locale" id="bprLocale" name="bprLocale">';
foreach( $locales as $l ) {
$html .= '<option value="' . $l['pk_c_code'] . '" ' . ($current == $l['pk_c_code'] ? 'selected="selected"' : '') . '>' . $l['s_name'] . '</option>';
}
$html .= '</select>';
return $html;
}
// GET CURRENT OR DEFAULT ADMIN LOCALE
function bpr_get_locale() {
$locales = OSCLocale::newInstance()->listAllEnabled();
if(Params::getParam('bprLocale') <> '') {
$current = Params::getParam('bprLocale');
} else {
$current = (osc_current_user_locale() <> '' ? osc_current_user_locale() : osc_current_admin_locale());
$current_exists = false;
// check if current locale exist in front-office
foreach( $locales as $l ) {
if($current == $l['pk_c_code']) {
$current_exists = true;
}
}
if( !$current_exists ) {
$i = 0;
foreach( $locales as $l ) {
if( $i==0 ) {
$current = $l['pk_c_code'];
}
$i++;
}
}
}
return $current;
}
// CORE FUNCTIONS
function bpr_param($name) {
return osc_get_preference($name, 'plugin-business_profile');
}
if(!function_exists('mb_param_update')) {
function mb_param_update( $param_name, $update_param_name, $type = NULL, $plugin_var_name ) {
$val = '';
if( $type == 'check') {
// Checkbox input
if( Params::getParam( $param_name ) == 'on' ) {
$val = 1;
} else {
if( Params::getParam( $update_param_name ) == 'done' ) {
$val = 0;
} else {
$val = ( osc_get_preference( $param_name, $plugin_var_name ) != '' ) ? osc_get_preference( $param_name, $plugin_var_name ) : '';
}
}
} else {
// Other inputs (text, password, ...)
if( Params::getParam( $update_param_name ) == 'done' && Params::existParam($param_name)) {
$val = Params::getParam( $param_name );
} else {
$val = ( osc_get_preference( $param_name, $plugin_var_name) != '' ) ? osc_get_preference( $param_name, $plugin_var_name ) : '';
}
}
// If save button was pressed, update param
if( Params::getParam( $update_param_name ) == 'done' ) {
if(osc_get_preference( $param_name, $plugin_var_name ) == '') {
osc_set_preference( $param_name, $val, $plugin_var_name, 'STRING');
} else {
$dao_preference = new Preference();
$dao_preference->update( array( "s_value" => $val ), array( "s_section" => $plugin_var_name, "s_name" => $param_name ));
osc_reset_preferences();
unset($dao_preference);
}
}
return $val;
}
}
// CHECK IF RUNNING ON DEMO
function bpr_is_demo() {
if(osc_logged_admin_username() == 'admin') {
return false;
} else if(isset($_SERVER['HTTP_HOST']) && (strpos($_SERVER['HTTP_HOST'],'mb-themes') !== false || strpos($_SERVER['HTTP_HOST'],'abprofitrade') !== false)) {
return true;
} else {
return false;
}
}
if(!function_exists('message_ok')) {
function message_ok( $text ) {
$final = '<div class="flashmessage flashmessage-ok flashmessage-inline">';
$final .= $text;
$final .= '</div>';
echo $final;
}
}
if(!function_exists('message_error')) {
function message_error( $text ) {
$final = '<div class="flashmessage flashmessage-error flashmessage-inline">';
$final .= $text;
$final .= '</div>';
echo $final;
}
}
if( !function_exists('osc_is_contact_page') ) {
function osc_is_contact_page() {
$location = Rewrite::newInstance()->get_location();
$section = Rewrite::newInstance()->get_section();
if( $location == 'contact' ) {
return true ;
}
return false ;
}
}
// COOKIES WORK
if(!function_exists('mb_set_cookie')) {
function mb_set_cookie($name, $val) {
Cookie::newInstance()->set_expires( 86400 * 30 );
Cookie::newInstance()->push($name, $val);
Cookie::newInstance()->set();
}
}
if(!function_exists('mb_get_cookie')) {
function mb_get_cookie($name) {
return Cookie::newInstance()->get_value($name);
}
}
if(!function_exists('mb_drop_cookie')) {
function mb_drop_cookie($name) {
Cookie::newInstance()->pop($name);
}
}
?>