You are here

class CFNodeSubject in Changed Fields API 7

Same name and namespace in other branches
  1. 7.3 src/CFNodeSubject.php \CFNodeSubject
  2. 7.2 includes/changed_fields.core.inc \CFNodeSubject

Class CFNodeSubject.

Hierarchy

Expanded class hierarchy of CFNodeSubject

File

includes/changed_fields.core.inc, line 82
changed_fields.core.inc file.

View source
class CFNodeSubject implements CFSubject {

  /**
   * @var stdClass
   */
  private $node;

  /**
   * @var array
   */
  private $info;

  /**
   * @var array
   */
  private $changedFields;

  /**
   * @var CFDefaultFieldComparator
   */
  private $fieldComparator;

  /**
   * @var array
   */
  private $observers;

  /**
   * @param stdClass $node
   * @param array $info
   * @param CFDefaultFieldComparator $fieldComparator
   */
  public function __construct(stdClass $node, array $info, CFDefaultFieldComparator $fieldComparator) {
    $this->node = $node;
    $this->info = $info;
    $this->changedFields = array();
    $this->fieldComparator = $fieldComparator;
  }

  /**
   * {@inheritdoc}
   */
  public function addObserver(CFObserver $observer) {
    $this->observers[$observer
      ->getId()] = $observer;
  }

  /**
   * {@inheritdoc}
   */
  public function removeObserver(CFObserver $observer) {
    unset($this->observers[$observer
      ->getId()]);
  }

  /**
   * {@inheritdoc}
   */
  public function checkNodeFields() {
    foreach ($this->info as $nodeType => $fields) {
      if (isset($this->node->original) && $this->node->type == $nodeType) {
        $changedFields = array();
        foreach ($fields as $fieldName) {
          if ($fieldName == 'title') {
            $oldValue = $this->node->original->{$fieldName};
            $newValue = $this->node->{$fieldName};
            $fieldInfo = array(
              'type' => 'title',
            );
          }
          else {
            $oldValue = field_get_items('node', $this->node->original, $fieldName);
            $newValue = field_get_items('node', $this->node, $fieldName);
            $fieldInfo = field_info_field($fieldName);
          }
          $result = $this->fieldComparator
            ->runFieldComparison($fieldInfo, $oldValue, $newValue);
          if (is_array($result)) {
            $changedFields[$fieldName] = $result;
          }
        }
        if (!empty($changedFields)) {
          $this->changedFields = $changedFields;
          foreach ($this->observers as $observer) {
            $observer
              ->update($this);
          }
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getNode() {
    return $this->node;
  }

  /**
   * {@inheritdoc}
   */
  public function getChangedFields() {
    return $this->changedFields;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CFNodeSubject::$changedFields private property
CFNodeSubject::$fieldComparator private property
CFNodeSubject::$info private property
CFNodeSubject::$node private property
CFNodeSubject::$observers private property
CFNodeSubject::addObserver public function Add observer to list. Overrides CFSubject::addObserver
CFNodeSubject::checkNodeFields public function Notify all registered observers if needed. Overrides CFSubject::checkNodeFields
CFNodeSubject::getChangedFields public function Returns changed fields. Overrides CFSubject::getChangedFields
CFNodeSubject::getNode public function Returns node object. Overrides CFSubject::getNode
CFNodeSubject::removeObserver public function Remove observer from list. Overrides CFSubject::removeObserver
CFNodeSubject::__construct public function