Advertisement:

Author Topic: Companies could post but users shouldn’t – how to do this?  (Read 146 times)

BritWeb

  • Hero Member
  • *****
  • Posts: 750
  • If it ain't broke, don't fix it.
Hi folks

I have the following requirement;

There are users who registered as ‘Company’ and ‘User’ on my site
  • Companies should be able to post a listing (only one allowed during their time of membership)
  • Users are not allowed to post any listing but they can only browse and contact companies
Please, could someone point me in the right direction to achieve this.


Thanks

WEBmods

  • Hero Member
  • *****
  • Posts: 840
  • aka patrickFromCroatia | Osclass Dev Services
Re: Companies could post but users shouldn’t – how to do this?
« Reply #1 on: April 24, 2019, 04:13:04 pm »
Hello,

I think you can add this code in publish item page to check if user is a company or not.

Code: [Select]
<?php
if(!osc_user_is_company()) {
    
// Not company, redirect or whatever
    
osc_add_flash_error_message(__('Only companies can post ads.')); // Add error message. 
    
osc_redirect_to(osc_base_url()); // Redirect to homepage.
}

Regards.

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Companies could post but users shouldn’t – how to do this?
« Reply #2 on: April 24, 2019, 05:58:13 pm »
Hi, I think this needs a little more, is my suggestion.

Is better add a function on functions.php file of your theme with header hook, and checking if is user are in publish page. For other hand, only is possible check if user is company or not, if user is logged in.
So, use admin settings to check the checkbox to that effect (only logged in users can publish).
Code: [Select]
function deny_users_to_publish(){
if(osc_is_web_user_logged_in() && !osc_user_is_company() && osc_is_publish_page()){
    osc_add_flash_error_message(__('Only companies can post ads.')); // Add error message.
    osc_redirect_to(osc_base_url()); // Redirect to homepage.
}
}
osc_add_hook('header',  'deny_users_to_publish');

You need take account if user not will change user type on profile settings. So you need to add/remove manually on user-profile.php file, the condition to dropdown menu (user type) and replace with simple text (you need update your translation):
Code: [Select]
<?php if (osc_user_field("b_company")==1){
echo 
'<span>'_e('Company''NAME_OF_YOUR_THEME') .'</span>';
 } else {
echo 
'<span>'_e('User''NAME_OF_YOUR_THEME') .'</span>';
}
?>

Take note, you need add dropdown menu (user type) on user-register.php file, inside form. Your users need choose user type on register, and that is valid forever, cannot be undone after register.

Using some javascript to require a value, and new option empty to force user choose an valid option.
On functions.php file:
Code: [Select]
function register_user_type(){
if(osc_is_register_page()){
echo '<script type="text/javascript">
$("#b_company").attr("required", "required");
var x = document.getElementById("b_company");
if (x.selectedIndex >= 0) {
  var option = document.createElement("option");
  option.text = "";
  var sel = x.options[x.selectedIndex];
  x.add(option, sel);
  document.getElementById("b_company").value = '';
}
</script>';
}
}
osc_add_hook('footer', 'register_user_type');
« Last Edit: April 24, 2019, 06:01:56 pm by fog »

BritWeb

  • Hero Member
  • *****
  • Posts: 750
  • If it ain't broke, don't fix it.
Re: Companies could post but users shouldn’t – how to do this?
« Reply #3 on: April 25, 2019, 03:00:18 am »
Thanks, guys. Let me try and get back to you.

Cheers!