Ejemplo n.º 1
0
 /**
  * Default constructor for the node object. Do not call this class directly.
  * Create a separate class for each content type and use its constructor.
  *
  * @param int $nid
  *   Nid if an existing node is to be loaded.
  */
 public function __construct($nid = NULL)
 {
     $class = new \ReflectionClass(get_called_class());
     $type = Utils::makeSnakeCase($class->getShortName());
     if (!is_null($nid) && is_numeric($nid)) {
         $node = node_load($nid);
         if (!$node) {
             $this->setErrors("Node with nid {$nid} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
         if ($node->type != $type) {
             $this->setErrors("Node's type doesn't match the class.");
             $this->setInitialized(FALSE);
             return;
         }
         parent::__construct($node);
     } else {
         global $user;
         $node = (object) array('title' => NULL, 'type' => $type, 'language' => LANGUAGE_NONE, 'is_new' => TRUE, 'name' => $user->name);
         node_object_prepare($node);
         parent::__construct($node);
     }
     $this->setInitialized(TRUE);
 }
Ejemplo n.º 2
0
 /**
  * Default constructor for the Commerce Product object. Do not call this
  * class directly. Create a separate class for each product type and use its
  * constructor.
  *
  * @param int $product_id
  *   Product id if an existing product is to be loaded.
  */
 public function __construct($product_id = NULL)
 {
     $class = new \ReflectionClass(get_called_class());
     $type = Utils::makeSnakeCase($class->getShortName());
     if (!is_null($product_id)) {
         $product = NULL;
         if (is_numeric($product_id)) {
             $product = commerce_product_load($product_id);
         }
         if ($product && $product->type == $type) {
             parent::__construct($product);
             return;
         }
         // SKU might have been passed instead.
         $product = commerce_product_load_by_sku($product_id);
         if ($product && $product->type == $type) {
             parent::__construct($product);
             return;
         }
         if (!$product) {
             $this->setErrors("Product with id or sku {$product_id} and type {$type} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
     } else {
         $product = commerce_product_new($type);
         parent::__construct($product);
     }
 }
Ejemplo n.º 3
0
 /**
  * Default constructor for the comment object.
  *
  * @param int|null $cid
  *   Cid if an existing comment is to be loaded and null if a new comment is
  *   to be loaded.
  */
 public function __construct($cid = NULL)
 {
     $args = func_get_args();
     array_shift($args);
     $nid = array_shift($args);
     $pid = array_shift($args);
     if (is_null($cid)) {
         if (!isset($nid) || !is_numeric($nid)) {
             $this->setErrors('Provide nid for a comment add form.');
             $this->setInitialized(FALSE);
             return;
         }
         $node = node_load($nid);
         $comment = (object) array('nid' => $nid);
     } else {
         $comment = comment_load($cid);
         $comment_nid = $comment->nid;
         if (!is_null($nid) && $comment_nid != $nid) {
             $this->setErrors('Id of the node associated with the comment and provided node id do not match.');
             $this->setInitialized(FALSE);
             return;
         }
         $node = node_load($comment_nid);
     }
     if (!$node) {
         $this->setErrors("Node {$nid} doesn't exist.");
         $this->setInitialized(FALSE);
         return;
     }
     if (!is_null($pid)) {
         $parent_comment = comment_load($pid);
         if (!$parent_comment) {
             $this->setInitialized(FALSE);
             $this->setErrors("Comment {$pid} does not exist.");
             return;
         }
         if ($parent_comment->nid != $node->nid) {
             $this->setInitialized(FALSE);
             $this->setErrors("Node id associated with the parent comment and the one that is provided do not match.");
             return;
         }
         $comment->pid = $pid;
     }
     $classname = get_called_class();
     $class = new \ReflectionClass($classname);
     $class_shortname = $class->getShortName();
     $type = Utils::makeSnakeCase(substr($class_shortname, 0, -7));
     if ($node->type != $type) {
         $this->setErrors("Classes of comment and the node do not match. Class of comment is {$type} while that of node is " . $node->type . ".");
         $this->setInitialized(TRUE);
         return;
     }
     parent::__construct($comment);
 }
Ejemplo n.º 4
0
 /**
  * Default constructor for the term object.
  *
  * @param int $tid
  *   TaxonomyTerm id if an existing term is to be loaded.
  */
 public function __construct($tid = NULL)
 {
     $class = new \ReflectionClass(get_called_class());
     $vocabulary_name = Utils::makeSnakeCase($class->getShortName());
     if (!is_null($tid) && is_numeric($tid)) {
         $term = taxonomy_term_load($tid);
         if ($term->vocabulary_machine_name == $vocabulary_name) {
             parent::__construct($term);
         }
     } else {
         $term = (object) array('name' => '', 'description' => '', 'format' => NULL, 'vocabulary_machine_name' => $vocabulary_name, 'tid' => NULL, 'weight' => 0);
         parent::__construct($term);
     }
     $this->setInitialized(TRUE);
 }
Ejemplo n.º 5
0
 /**
  * Default constructor for the node object. Do not call this class directly.
  * Create a separate class for each content type and use its constructor.
  *
  * @param int $nid
  *   Nid if an existing node is to be loaded.
  */
 public function __construct($nid = NULL)
 {
     $class = new \ReflectionClass(get_called_class());
     $type = Utils::makeSnakeCase($class->getShortName());
     if (!is_null($nid) && is_numeric($nid)) {
         $node = node_load($nid);
         if ($node->type == $type) {
             parent::__construct($node);
         }
     } else {
         global $user;
         $node = (object) array('title' => NULL, 'type' => $type, 'language' => LANGUAGE_NONE, 'is_new' => TRUE, 'name' => $user->name);
         node_object_prepare($node);
         parent::__construct($node);
     }
     $this->setInitialized(TRUE);
 }
 /**
  * Default constructor for the Commerce Customer Profile.
  *
  * @param int $order_id
  *   Order id if an existing order is to be loaded.
  */
 public function __construct($profile_id = NULL)
 {
     if (!is_null($profile_id) && is_numeric($profile_id)) {
         $profile = commerce_customer_profile_load($profile_id);
         if (!$profile) {
             $this->setErrors("Profile with id {$profile} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
     } else {
         global $user;
         $class = new \ReflectionClass(get_called_class());
         $type = Utils::makeSnakeCase($class->getShortName());
         $profile = commerce_customer_profile_new($type, $user->uid);
     }
     parent::__construct($profile);
 }
Ejemplo n.º 7
0
 /**
  * Default constructor for the Commerce Recurring object.
  *
  * @param int $recurring_id
  *   Recurring id if an existing recurring entity is to be loaded.
  */
 public function __construct($recurring_id = NULL)
 {
     $class = new \ReflectionClass(get_called_class());
     $type = Utils::makeSnakeCase($class->getShortName());
     if (!is_null($recurring_id)) {
         $rec_entity = NULL;
         if (is_numeric($recurring_id)) {
             $rec_entity = commerce_recurring_load($recurring_id);
         }
         if (!empty($rec_entity)) {
             parent::__construct($rec_entity);
             return;
         }
     } else {
         $rec_entity = commerce_recurring_new(array('type' => $type));
         parent::__construct($rec_entity);
     }
 }
Ejemplo n.º 8
0
 /**
  * Default constructor for the Commerce License object.
  *
  * @param int $license_id
  *   License id if an existing license entity is to be loaded.
  */
 public function __construct($license_id = NULL)
 {
     $args = func_get_args();
     array_shift($args);
     $order_id = array_shift($args);
     if (is_null($order_id)) {
         $order_id = 0;
     }
     $order = commerce_order_load($order_id);
     if (!is_null($order_id)) {
         $license = commerce_license_get_order_licenses($order_id);
         if (!$license) {
             $this->setErrors("License for order id {$order_id} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
     }
     parent::__construct($license);
 }
Ejemplo n.º 9
0
 /**
  * Default constructor for the Commerce Line Item object. Do not call this
  * class directly. Create a separate class for each line item type and use
  * its constructor.
  *
  * You can also pass a second argument, and it will be interpreted as
  * order_id. It is used for creating a new line item.
  *
  * @param int $line_item_id
  *   Product id if an existing product is to be loaded.
  */
 public function __construct($line_item_id = NULL)
 {
     $args = func_get_args();
     array_shift($args);
     $order_id = array_shift($args);
     if (is_null($order_id)) {
         $order_id = 0;
     }
     if (!is_null($line_item_id) && is_numeric($line_item_id)) {
         $line_item = commerce_line_item_load($line_item_id);
         if (!$line_item) {
             $this->setErrors("Line item with id {$line_item_id} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
     } else {
         $class = new \ReflectionClass(get_called_class());
         $type = Utils::makeSnakeCase($class->getShortName());
         $line_item = commerce_line_item_new($type, $order_id);
     }
     parent::__construct($line_item);
 }
Ejemplo n.º 10
0
 /**
  * Default constructor for the Commerce Coupon object. Do not call this
  * class directly.
  *
  * @param int $coupon_id
  *   Coupon id if an existing coupon is to be loaded.
  * @param int $code
  *   Coupon code if an existing coupon is to be loaded.
  */
 public function __construct($coupon_id = NULL, $code = NULL)
 {
     if (!is_null($coupon_id)) {
         $coupon = commerce_coupon_load($coupon_id);
         if (!$coupon) {
             $this->setErrors("Coupon with id {$coupon} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
     } else {
         if (!is_null($code)) {
             $coupon = commerce_coupon_load_by_code($code);
             if (!$coupon) {
                 $this->setErrors("Coupon with code {$code} does not exist.");
                 $this->setInitialized(FALSE);
                 return;
             }
         } else {
             $coupon = commerce_coupon_create('discount_coupon');
         }
     }
     parent::__construct($coupon);
 }
Ejemplo n.º 11
0
 public static function getValues(Entity $entityObject, $field_name, $post_process = FALSE)
 {
     $field = $entityObject->getFieldItems($field_name);
     return $field;
 }
Ejemplo n.º 12
0
 /**
  * Returns information about field instance.
  *
  * @param Entity $entityObject
  *   Entity for which the field instance is to be returned.
  * @param string $field_name
  *   Field name.
  *
  * @return array|null
  *   An array of field instance information if the instance exists. NULL if
  *   the field instance does not exist.
  */
 public static function getFieldInstance(Entity $entityObject, $field_name)
 {
     list(, , $bundle) = entity_extract_ids($entityObject->getEntityType(), $entityObject->getEntity());
     $instance = field_info_instance($entityObject->getEntityType(), $field_name, $bundle);
     return $instance;
 }
Ejemplo n.º 13
0
 public static function getFileGenericValues(Entity $entityObject, $field_name, $post_process = FALSE)
 {
     $field = $entityObject->getFieldItems($field_name);
     if (!$post_process) {
         return $field;
     }
     $output = array();
     foreach ($field as $fid => $file) {
         $output[] = $fid;
     }
     return Utils::normalize($output);
 }
Ejemplo n.º 14
0
 /**
  * Magic method.
  *
  * @param string $name
  *   Function name that is called.
  * @param array $arguments
  *   An array of arguments.
  *
  * @return mixed
  *   Depends on which function is invoked.
  */
 public function __call($name, $arguments)
 {
     if ($name == 'logout') {
         static::logoutFunction();
     } else {
         return parent::__call($name, $arguments);
     }
 }
Ejemplo n.º 15
0
 /**
  * Reload the order from the database. This is necessary so that
  * commerce_cart_commerce_order_load() function can get called, which in turn
  * invokes the rule "Override price for recurring" provided by the recurring
  * framework.
  */
 public function reload()
 {
     // Unsetting $refreshed in commerce_cart_commerce_order_load() is needed so
     // that commerce_cart_order_refresh() can be called.
     drupal_static_reset('commerce_cart_commerce_order_load');
     // Temporarily set the value of commerce_cart_refresh_frequency to 0 so that
     // the function commerce_cart_order_can_refresh() which is called in
     // commerce_cart_commerce_order_load() returns TRUE and
     // commerce_cart_order_refresh() can actually get called.
     $commerce_cart_refresh_frequency = variable_get('commerce_cart_refresh_frequency', 0);
     global $conf;
     $conf['commerce_cart_refresh_frequency'] = 0;
     // Load the order from the database which should call the "Override price
     // for recurring" rule provided by recurring framework.
     parent::reload();
     // Set value of commerce_cart_refresh_frequency variable back to what it was
     // earlier.
     $conf['commerce_cart_refresh_frequency'] = $commerce_cart_refresh_frequency;
 }