/** * 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); } }
/** * Default constructor. * * @param null|int $id * Comment id if a comment edit form is to be loaded and null if comment * add form is to be loaded. */ public function __construct($id) { if (!user_access('post comments')) { $this->setErrors("User is not allowed to post comments."); $this->setInitialized(FALSE); return; } $args = func_get_args(); array_shift($args); $nid = array_shift($args); $pid = array_shift($args); $classname = get_called_class(); $class = new \ReflectionClass($classname); $class_shortname = $class->getShortName(); $class_fullname = "RedTest\\entities\\Comment\\" . substr($class_shortname, 0, -4); $commentObject = new $class_fullname($id, $nid, $pid); if (!$commentObject->getInitialized()) { $this->setErrors($commentObject->getErrors()); $this->setInitialized(FALSE); return; } $this->setEntityObject($commentObject); $nid = $commentObject->getNidValues(); $node = node_load($nid); if ($node->comment != COMMENT_NODE_OPEN && is_null($id)) { $this->setErrors("Node {$nid} does not allow posting of comments."); $this->setInitialized(FALSE); return; } $type = Utils::makeSnakeCase(substr($class_shortname, 0, -11)); parent::__construct('comment_node_' . $type . '_form', (object) array('nid' => $nid, 'pid' => $pid)); }
/** * 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); }
/** * 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); }
/** * Default constructor of the node form. You should not be invoking NodeForm * directly. Create a form for your content type that extends NodeForm and * invoke that. The access level has to be kept public here because access * level of parent class has to be match that of child class. * * @param null|int $nid * Node id if an existing node form is to be loaded. If a new node form is * to be created, then keep it empty. */ public function __construct($nid = NULL) { $classname = get_called_class(); $class = new \ReflectionClass($classname); $class_shortname = $class->getShortName(); $class_fullname = "RedTest\\entities\\Node\\" . Utils::makeTitleCase(substr($class_shortname, 0, -4)); $type = Utils::makeSnakeCase(substr($class_shortname, 0, -4)); $nodeObject = new $class_fullname($nid); $this->setEntityObject($nodeObject); if (!is_null($this->getEntityObject()->getEntity())) { $this->includeFile('inc', 'node', 'node.pages'); parent::__construct($type . '_node_form', $this->getEntityObject()->getEntity()); } $this->setInitialized(TRUE); }
/** * 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); }
/** * 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); }
/** * 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); } }
/** * 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); }
/** * Default constructor of the taxonomy term form. You should not be invoking * TaxonomyFormTerm directly. Create a form for your vocabulary that extends * TaxonomyFormTerm and invoke that. The access level has to be kept public * here because access level of parent class has to be match that of child * class. * * @param null|int $tid * Taxonomy term id if a taxonomy term edit form is to be loaded. If a * taxonomy term add form is to be created, then keep it empty. */ public function __construct($tid = NULL) { $classname = get_called_class(); $class = new \ReflectionClass($classname); $class_shortname = $class->getShortName(); $vocabulary_name = Utils::makeSnakeCase(substr($class_shortname, 0, -4)); if (!is_null($tid) && is_numeric($tid)) { // Tid is not null and is numeric. $term = taxonomy_term_load($tid); if ($term->vocabulary_machine_name == $vocabulary_name) { $this->vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name); $base_path = "RedTest\\entities\\TaxonomyTerm\\"; $class_fullname = $base_path . substr($class_shortname, 0, -4); $termObject = new $class_fullname($tid); $this->setEntityObject($termObject); $this->includeFile('inc', 'taxonomy', 'taxonomy.admin'); parent::__construct('taxonomy_form_term', $term, $this->vocabulary); $this->setInitialized(TRUE); return; } else { // Vocabulary name of the provided term does not match the class it was called from. Return with a FAIL response. $this->setErrors("Vocabulary of the provided term does not match the class it was called from."); $this->setInitialized(FALSE); return; } } else { // Proper tid is not provided. Create a dummy term object. $base_path = "RedTest\\entities\\TaxonomyTerm\\"; $class_fullname = $base_path . substr($class_shortname, 0, -4); $termObject = new $class_fullname(); $this->setEntityObject($termObject); } // tid is not provided or is not numeric. $this->vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name); $this->includeFile('inc', 'taxonomy', 'taxonomy.admin'); parent::__construct('taxonomy_form_term', array(), $this->vocabulary); $this->setInitialized(TRUE); }
/** * Magic method. This function will be executed when a matching function is * not found. Currently this supports three kinds of functions: * getEntityType(), get<FieldName>() and has<FieldName><View|Edit>Access(). * * @param string $name * Called function name. * @param string $arguments * Function arguments. * * @return mixed $output * Output depends on which function ultimately gets called. */ public function __call($name, $arguments) { if ($name == 'getEntityType') { return $this->getObjectEntityType(); } elseif ($name == 'hasAccess') { return call_user_func_array(array($this, 'hasObjectAccess'), $arguments); } elseif ($this->isHasFieldAccessFunction($name)) { // Function name starts with "has" and ends with "Access". Function name // is not one of "hasCreateAccess", "hasUpdateAccess", "hasViewAccess" or // "hasDeleteAccess" otherwise code execution would not have reached this // function. This means that we are checking if a field is accessible. $name = substr($name, 3, -6); $op = ''; $field_name = ''; if (Utils::endsWith($name, 'View')) { $op = 'view'; $field_name = Utils::makeSnakeCase(substr($name, 0, -4)); } elseif (Utils::endsWith($name, 'Update')) { $op = 'edit'; $field_name = Utils::makeSnakeCase(substr($name, 0, -6)); } if (in_array($op, array('view', 'edit'))) { return $this->hasFieldAccess($field_name, $op); } } elseif ($this->isGetFieldValuesFunction($name)) { // Function name starts with "get" and ends with "Values". This means that // we need to return value of a field. array_unshift($arguments, Utils::makeSnakeCase(substr($name, 3, -6))); return call_user_func_array(array($this, 'getFieldValues'), $arguments); } elseif (strpos($name, 'view') === 0) { // Function name starts with "view". array_unshift($arguments, Utils::makeSnakeCase(substr($name, 4))); return call_user_func_array(array($this, 'viewField'), $arguments); } elseif ($this->isCheckFieldValuesFunction($name)) { // Function name starts with "check" and ends with "Values". $field_name = Utils::makeSnakeCase(substr($name, 5, -6)); array_unshift($arguments, $field_name); return call_user_func_array(array($this, 'checkFieldValues'), $arguments); } elseif (strpos($name, "check") === 0 && strrpos($name, 'Views') == strlen($name) - 5) { // Function name starts with "check" and ends with "Values". $field_name = Utils::makeSnakeCase(substr($name, 5, -5)); array_unshift($arguments, $field_name); return call_user_func_array(array($this, 'checkFieldViews'), $arguments); } }
/** * Magic method. This function will be executed when a matching function is * not found. Currently this supports four kinds of functions: * fill<FieldName>RandomValues(), fill<FieldName>Values, * has<FieldName>Access, is<FieldName>Required. * * @param string $name * Called function name. * @param string $arguments * Function arguments. * * @return mixed $output * Output depends on which function ultimately gets called. */ public function __call($name, $arguments) { if ($this->isFillFieldRandomValuesFunction($name)) { // Function name starts with "fill" and ends with "RandomValues". $field_name = Utils::makeSnakeCase(substr($name, 4, -12)); array_unshift($arguments, $field_name); return call_user_func_array(array($this, 'fillFieldRandomValues'), $arguments); } elseif ($this->isFillFieldValuesFunction($name)) { // Function name starts with "fill" and ends with "Values". $field_name = Utils::makeSnakeCase(substr($name, 4, -6)); $arguments = array_shift($arguments); return $this->fillFieldValues($field_name, $arguments); } elseif (strpos($name, 'has') === 0 && strrpos($name, 'Access') == strlen($name) - 6) { // Function name starts with "has" and ends with "Access". Function name // is not one of "hasCreateAccess", "hasUpdateAccess", "hasViewAccess" or // "hasDeleteAccess" otherwise code execution would not have reached this // function. This means that we are checking if a field is accessible. $field_name = Utils::makeSnakeCase(substr($name, 3, -6)); return $this->hasFieldAccess($field_name); } elseif (strpos($name, 'is') === 0 && strrpos($name, 'Required') == strlen($name) - 8) { // Function name starts with "is" and ends with "Required". We are // checking if a field is required or not. $field_name = Utils::makeSnakeCase(substr($name, 2, -8)); $arguments = array_shift($arguments); return $this->isRequired($field_name, $arguments); } }