javascriptphpzend-frameworksocialengine

Echo text based on level_id for SocialEngine 4.x/Zend Framework


I must confess, I am quite new to working with php inside a javascript block. As a temporary fix until I can write something more dynamic, I need to pass text based on the user level, if they are not logged in, I am using the else to identify as such. I have a variable called member_level inside a javascript block, I have the following code below inside double quotes but it doesn't like the formatting. I just cannot see what is wrong. I am sorry if this question is worded wrong, just on my last candle and can't see what I am doing wrong

    var chat_role = "<?php if($this->viewer()->level_id == '1'){
                echo Super Admin;
  } else if($this->viewer()->level_id == '3'){
                echo Moderator;
  } else if($this->viewer()->level_id == '4'){
                echo Free;
  } else if($this->viewer()->level_id == '5'){
                echo Public;
  } else if($this->viewer()->level_id == '9'){
                echo Premium;
  } else if($this->viewer()->level_id == '13'){
                echo Corporate;
  } else if($this->viewer()->level_id == '15'){
                echo Lifetime;
  } else if($this->viewer()->level_id == '16'){
                echo Standard Admin;
  } else if($this->viewer()->level_id == '17'){
                echo New Member;
  } else {
                echo Non Logged In;
  }
endif ?>
";

What is it I am missing? I am truly not asking someone to write the code, just give me an idea of where I went wrong.


Solution

  • Use this code instead:

    var chat_role = "<?php echo (isset($this->viewer()->level_id) && $this->viewer()->level_id > 0 ? Engine_Api::_()->getItem('authorization_level', $this->viewer()->level_id)->getTitle(): 'Non Logged In'); ?>";