You are here

class RateButton in Rate 7.2

Hierarchy

Expanded class hierarchy of RateButton

File

classes/button.inc, line 3

View source
class RateButton {

  /**
   * Button identification.
   *
   * Can be strings or numbers, like 'up' or '5' for the last fivestar star.
   *
   * @var string
   */
  protected $id;

  /**
   * Label.
   *
   * @var string
   */
  protected $label;

  /**
   * VotingAPI value.
   *
   * @var int
   */
  protected $value;

  /**
   * Rate token. This token is used by the client to vote on this button.
   *
   * @var string
   */
  protected $token;

  /**
   * HTML mode for button label.
   *
   * @var bool
   */
  protected $html;

  /**
   * Classes for link.
   *
   * @var string
   */
  protected $class;

  /**
   * Create a new button.
   *
   * @param string $id
   * @param string $label
   * @param int $value
   * @param string $token
   */
  public function __construct($id, $label, $value, $token, $html = FALSE, $class = '') {
    $this->id = $id;
    $this->label = $label;
    $this->value = $value;
    $this->token = $token;
    $this->html = $html;
    $this->class = $class;
  }

  /**
   * Get HTML code for button.
   */
  public function __toString() {
    $classes = array(
      'rate-button',
      "button{$this->id}",
    );
    if ($this->class) {
      $classes[] = $this->class;
    }
    return theme('link', array(
      'text' => $this->label,
      'path' => $_GET['q'],
      'options' => array(
        'attributes' => array(
          'rel' => 'nofollow',
          'class' => $classes,
          'data-token' => $this->token,
        ),
        'query' => array(
          'rate' => $this->token,
        ),
        'html' => $this->html,
      ),
    ));
  }

  /**
   * Check if the user has clicked this button.
   */
  public function hasClicked() {
    if (isset($_GET['rate']) && $_GET['rate'] == $this->token) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Get VotingAPI value.
   */
  public function getValue() {
    return $this->value;
  }

  /**
   * Get label.
   */
  public function getLabel() {
    return $this->label;
  }

  /**
   * Add class.
   *
   * @param string $class
   */
  public function addClass($class) {
    $classes = array_map('trim', explode(' ', $this->class));
    $classes[] = $class;
    $this->class = implode(' ', array_filter(array_unique($classes)));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RateButton::$class protected property Classes for link.
RateButton::$html protected property HTML mode for button label.
RateButton::$id protected property Button identification.
RateButton::$label protected property Label.
RateButton::$token protected property Rate token. This token is used by the client to vote on this button.
RateButton::$value protected property VotingAPI value.
RateButton::addClass public function Add class.
RateButton::getLabel public function Get label.
RateButton::getValue public function Get VotingAPI value.
RateButton::hasClicked public function Check if the user has clicked this button.
RateButton::__construct public function Create a new button.
RateButton::__toString public function Get HTML code for button.