You are here

class FacebookPublicationEntity in Facebook Autopost 7

The class used for Facebook publication entities.

Hierarchy

Expanded class hierarchy of FacebookPublicationEntity

1 string reference to 'FacebookPublicationEntity'
fb_autopost_entity_entity_info in fb_autopost_entity/fb_autopost_entity.module
Implements hook_entity_info().

File

fb_autopost_entity/fb_autopost_entity.module, line 387
Module implementation file

View source
class FacebookPublicationEntity extends Entity {

  /**
   * The Facebook publication id.
   *
   * @var integer
   */
  public $fbpid;

  /**
   * The name of the Facebook publication type.
   *
   * @var string
   */
  public $type;

  /**
   * The Facebook publication label.
   *
   * @var string
   */
  public $label;

  /**
   * The user id of the Facebook publication owner.
   *
   * @var integer
   */
  public $uid;

  /**
   * The Unix timestamp when the Facebook publication was created.
   *
   * @var integer
   */
  public $created;

  /**
   * The Unix timestamp when the Facebook publication was most recently saved.
   *
   * @var integer
   */
  public $changed;

  /**
   * The ID string returned from Facebook on publication.
   *
   * @var string
   */
  public $facebook_id;

  /**
   * Entity constructor.
   */
  public function __construct($values = array()) {
    if (isset($values['user'])) {
      $this
        ->setUser($values['user']);
      unset($values['user']);
    }
    if (isset($values['type']) && is_object($values['type'])) {
      $values['type'] = $values['type']->type;
    }
    if (!isset($values['label']) && isset($values['type']) && ($type = facebook_publication_get_types($values['type']))) {

      // Initialize the label with the type label, so newly created publications
      // have that as interim label.
      $values['label'] = $type->label;
    }
    parent::__construct($values, 'facebook_publication');
  }

  /**
   * Returns the user owning this Facebook publication.
   */
  public function user() {
    return user_load($this->uid);
  }

  /**
   * Sets a new user owning this Facebook publication.
   *
   * @param object $account
   *   The user account object or the user account id (uid).
   */
  public function setUser($account) {
    $this->uid = is_object($account) ? $account->uid : $account;
  }

  /**
   * Gets the associated Facebook publication type object.
   *
   * @return FacebookPublicationTypeEntity
   *   Returns the type entity.
   */
  public function type() {
    return facebook_publication_get_types($this->type);
  }

  /**
   * Gets the associated facebook_id.
   *
   * @return string
   *   String representing the Facebook ID.
   */
  public function facebookid() {
    return $this->facebook_id;
  }

  /**
   * Returns the full url() for the Facebook publication.
   */
  public function url() {
    $uri = $this
      ->uri();
    return url($uri['path'], $uri);
  }

  /**
   * Returns the drupal path to this Facebook publication.
   */
  public function path() {
    $uri = $this
      ->uri();
    return $uri['path'];
  }

  /**
   * Gets the default URI for the publication.
   */
  public function defaultUri() {
    return array(
      // TODO: Define this URL in hook_menu.
      'path' => 'facebook-publication/' . $this->fbpid,
    );
  }

  /**
   * Build entity content based on $view_mode and $lagcode.
   */
  public function buildContent($view_mode = 'full', $langcode = NULL) {
    $content = array();

    // Assume newly create objects are still empty.
    if (!empty($this->is_new)) {
      $content['empty']['#markup'] = '<em class="facebook-pulbication-no-data">' . t('There is no Facebook publication data yet.') . '</em>';
    }
    return entity_get_controller($this->entityType)
      ->buildContent($this, $view_mode, $langcode, $content);
  }

  /**
   * Save the entity.
   */
  public function save() {

    // Care about setting created and changed values. But do not automatically
    // set a created values for already existing Facebook publications.
    if (empty($this->created) && (!empty($this->is_new) || !$this->fbpid)) {
      $this->created = REQUEST_TIME;
    }
    $this->changed = REQUEST_TIME;
    parent::save();
  }

  /**
   * Get the property names out of the field names associated to this entity.
   *
   * @param array $field_names
   *   A numeric array containing the field names to translate.
   *
   * @return array
   *   A keyed array with field name => property name.
   */
  public function getFieldProperties($field_names = NULL) {
    if (empty($field_names)) {

      // Get all the field names associated with this entity bundle.
      $field_names = array_keys(field_info_instances('facebook_publication', $publication->type));
    }
    $output = array();
    foreach ($field_names as $field_name) {

      // Remove 'field_facebook_' prefix from field names.
      $output[$field_name] = substr($field_name, strlen('field_facebook_'));
    }

    // Allow other modules to alter this default behavior.
    drupal_alter('field_property_data', $output);
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
FacebookPublicationEntity::$changed public property The Unix timestamp when the Facebook publication was most recently saved.
FacebookPublicationEntity::$created public property The Unix timestamp when the Facebook publication was created.
FacebookPublicationEntity::$facebook_id public property The ID string returned from Facebook on publication.
FacebookPublicationEntity::$fbpid public property The Facebook publication id.
FacebookPublicationEntity::$label public property The Facebook publication label.
FacebookPublicationEntity::$type public property The name of the Facebook publication type.
FacebookPublicationEntity::$uid public property The user id of the Facebook publication owner.
FacebookPublicationEntity::buildContent public function Build entity content based on $view_mode and $lagcode. Overrides Entity::buildContent
FacebookPublicationEntity::defaultUri public function Gets the default URI for the publication. Overrides Entity::defaultUri
FacebookPublicationEntity::facebookid public function Gets the associated facebook_id.
FacebookPublicationEntity::getFieldProperties public function Get the property names out of the field names associated to this entity.
FacebookPublicationEntity::path public function Returns the drupal path to this Facebook publication.
FacebookPublicationEntity::save public function Save the entity. Overrides Entity::save
FacebookPublicationEntity::setUser public function Sets a new user owning this Facebook publication.
FacebookPublicationEntity::type public function Gets the associated Facebook publication type object.
FacebookPublicationEntity::url public function Returns the full url() for the Facebook publication.
FacebookPublicationEntity::user public function Returns the user owning this Facebook publication.
FacebookPublicationEntity::__construct public function Entity constructor. Overrides Entity::__construct