class EntityLegalDocument in Entity Legal 7
Same name and namespace in other branches
- 7.2 entity_legal.entity.inc \EntityLegalDocument
Legal Document entity with revision support.
Hierarchy
- class \Entity implements EntityInterface
- class \EntityLegalDocument
Expanded class hierarchy of EntityLegalDocument
1 string reference to 'EntityLegalDocument'
- entity_legal_entity_info in ./
entity_legal.module - Implements hook_entity_info().
File
- ./
entity_legal.entity.inc, line 10 - Entity API main classes used by entity_legal module.
View source
class EntityLegalDocument extends Entity {
/**
* {@inheritdoc}
*/
public function __construct(array $values = array(), $entity_type = NULL) {
Entity::__construct($values, ENTITY_LEGAL_DOCUMENT_ENTITY_NAME);
}
/**
* Get an acceptance form for this legal document.
*
* @return array
* The drupal acceptance form.
*/
public function getAcceptanceForm() {
return drupal_get_form('entity_legal_document_acceptance_form', $this);
}
/**
* Get a version of this document.
*
* @param bool $version_name
* If set, load the version otherwise load the default version or create
* one.
*
* @return EntityLegalDocumentVersion
* The legal document version.
*/
public function getVersion($version_name = FALSE) {
// If a version name is supplied, load it.
if ($version_name) {
return entity_load_single(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, $version_name);
}
// If no version name is supplied but a published version exists, return it.
$published_version = $this
->getPublishedVersion();
if (!$version_name && $published_version) {
return $published_version;
}
$all_versions = $this
->getAllVersions();
// If no versions exist return a new one.
if (empty($all_versions) && !$version_name) {
return $this
->getNewVersion();
}
// Return the first version.
if (!$version_name && !$published_version) {
return array_pop($all_versions);
}
}
/**
* Get the label of the legal document entity.
*
* @param bool $sanitize
* Whether or not to sanitize the label, defaults to TRUE.
*
* @return string
* The label string.
*/
public function label($sanitize = FALSE) {
$label_text = isset($this->label) ? $this->label : '';
if ($sanitize) {
$label_text = check_plain($label_text);
}
return $label_text;
}
/**
* Get a new version of this legal document.
*
* @return LegalDocumentVersion
* The legal document version entity.
*/
public function getNewVersion() {
return entity_create(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, array(
'document_name' => $this
->identifier(),
));
}
/**
* Get all versions of this legal document entity.
*
* @return array
* All versions of this legal document entity.
*/
public function getAllVersions() {
return entity_get_controller($this->entityType)
->getAllVersions($this);
}
/**
* Get the current published version of this document.
*
* @return bool|EntityLegalDocumentVersion
* The current legal document version or FALSE if none found.
*/
public function getPublishedVersion() {
$published_version = FALSE;
drupal_alter('entity_legal_published_version', $this->published_version, $this);
if (!empty($this->published_version)) {
$published_version = entity_load_single(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, $this->published_version);
}
return $published_version;
}
/**
* Set the published document version.
*
* @param EntityLegalDocumentVersion $version_entity
* The legal document version to set as the published version.
*
* @return bool
* Whether or not the published version was set successfully.
*/
public function setPublishedVersion(EntityLegalDocumentVersion $version_entity) {
// If the version entity is not of this bundle, fail.
if ($version_entity->document_name != $this
->identifier()) {
return FALSE;
}
$this->published_version = $version_entity
->identifier();
return TRUE;
}
/**
* Get the title of the attached version.
*
* @return bool|string
* The title of the published version or FALSE if no title found.
*/
public function getVersionLabel() {
$version_entity = $this
->getPublishedVersion();
if ($version_entity) {
return $version_entity
->label(TRUE);
}
else {
return FALSE;
}
}
/**
* Specifies the default uri, which is picked up by uri() by default.
*/
protected function defaultURI() {
return array(
'path' => 'legal/document/' . str_replace('_', '-', $this
->identifier()),
);
}
/**
* Use the entity name as the identifier.
*/
public function identifier() {
return $this->name;
}
/**
* URI callback.
*/
public function uri() {
return $this
->defaultURI();
}
/**
* Get the label to be shown on the acceptance checkbox.
*
* @return string
* The label to be shown on the acceptance checkbox.
*/
public function getAcceptanceLabel() {
$label = '';
$published_version = $this
->getPublishedVersion();
if ($published_version) {
$label = $published_version->acceptance_label;
}
return check_plain($label);
}
/**
* Get a legal document setting.
*
* Functions similar to variable get by allowing default values but differs
* by checking for a variable keys existence
*
* @param string $setting_group
* The group of settings the setting belongs to.
* @param string $setting_key
* The settings key within the settings group.
* @param mixed $default
* The default value to return if the setting is not found.
*
* @return mixed
* The value of hte setting.
*/
public function getSetting($setting_group, $setting_key, $default = FALSE) {
if (!isset($this->settings)) {
return $default;
}
if (!isset($this->settings[$setting_group])) {
return $default;
}
if (!isset($this->settings[$setting_group][$setting_key])) {
return $default;
}
return $this->settings[$setting_group][$setting_key];
}
/**
* Checks to see if a given user can agree to this document.
*
* @param bool $new_user
* Whether or not the user to check is a new user signup or not.
* @param object $account
* The user account to check the access permissions of. Defaults to the
* current user if none is provided.
*
* @return bool
* Can a user agree to this document.
*/
public function userMustAgree($new_user = FALSE, $account = NULL) {
// User cannot agree unless there is a published version.
if (!$this
->getPublishedVersion()) {
return FALSE;
}
if ($new_user) {
return !empty($this->require_signup);
}
else {
return !empty($this->require_existing) && user_access($this
->getPermissionExistingUser(), $account);
}
}
/**
* Check if the given user has agreed to the current version of this document.
*
* @param object $account
* The Drupal user account to check. Default logged in user if not provided.
*
* @return bool
* Whether or not the user has agreed to the current version.
*/
public function userHasAgreed($account = NULL) {
if (empty($account)) {
global $user;
$account = $user;
}
return count($this
->getAcceptances($account)) > 0;
}
/**
* Get the acceptances for this entity legal document revision.
*
* @param bool|object $account
* The Drupal user account to check for, or get all acceptances if FALSE.
* @param bool $published
* Get acceptances only for the currently published version.
*
* @return array
* The acceptance entities keyed by acceptance id.
*/
public function getAcceptances($account = FALSE, $published = TRUE) {
$acceptances = array();
$versions = array();
if ($published) {
$versions[] = $this
->getPublishedVersion();
}
else {
$versions = $this
->getAllVersions();
}
foreach ($versions as $version) {
$acceptances += $version
->getAcceptances($account);
}
return $acceptances;
}
/**
* Get the permission name for any user viewing this agreement.
*
* @return string
* The user permission, used with user_access.
*/
public function getPermissionView() {
return 'legal view ' . $this->name;
}
/**
* Get the permission name for new users accepting this document.
*
* @return string
* The user permission, used with user_access.
*/
public function getPermissionExistingUser() {
return 'legal re-accept ' . $this->name;
}
/**
* Get the acceptance delivery method for a given user type.
*
* @param bool $new_user
* Get the method for new signups or existing accounts.
*
* @return string
* The acceptance delivery method.
*/
public function getAcceptanceDeliveryMethod($new_user = FALSE) {
$setting_group = $new_user ? 'new_users' : 'existing_users';
return $this
->getSetting($setting_group, 'require_method');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Entity:: |
protected | property | 1 | |
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
protected | property | ||
Entity:: |
public | function |
Builds a structured array representing the entity's content. Overrides EntityInterface:: |
1 |
Entity:: |
public | function |
Returns the bundle of the entity. Overrides EntityInterface:: |
|
Entity:: |
protected | function | Defines the entity label if the 'entity_class_label' callback is used. | 1 |
Entity:: |
protected | function | Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info(). | |
Entity:: |
public | function |
Permanently deletes the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the info of the type of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the type of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Exports the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Gets the raw, translated value of a property or field. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Checks if the entity has a certain exportable status. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the internal, numeric identifier. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Checks whether the entity is the default revision. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Permanently saves the entity. Overrides EntityInterface:: |
|
Entity:: |
protected | function | Set up the object instance on construction or unserializiation. | |
Entity:: |
public | function |
Generate an array for rendering the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function |
Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface:: |
|
Entity:: |
public | function | Magic method to only serialize what's necessary. | |
Entity:: |
public | function | Magic method to invoke setUp() on unserialization. | |
EntityLegalDocument:: |
protected | function | Specifies the default uri, which is picked up by uri() by default. | |
EntityLegalDocument:: |
public | function | Get the acceptance delivery method for a given user type. | |
EntityLegalDocument:: |
public | function | Get an acceptance form for this legal document. | |
EntityLegalDocument:: |
public | function | Get the label to be shown on the acceptance checkbox. | |
EntityLegalDocument:: |
public | function | Get the acceptances for this entity legal document revision. | |
EntityLegalDocument:: |
public | function | Get all versions of this legal document entity. | |
EntityLegalDocument:: |
public | function | Get a new version of this legal document. | |
EntityLegalDocument:: |
public | function | Get the permission name for new users accepting this document. | |
EntityLegalDocument:: |
public | function | Get the permission name for any user viewing this agreement. | |
EntityLegalDocument:: |
public | function | Get the current published version of this document. | |
EntityLegalDocument:: |
public | function | Get a legal document setting. | |
EntityLegalDocument:: |
public | function | Get a version of this document. | |
EntityLegalDocument:: |
public | function | Get the title of the attached version. | |
EntityLegalDocument:: |
public | function |
Use the entity name as the identifier. Overrides Entity:: |
|
EntityLegalDocument:: |
public | function |
Get the label of the legal document entity. Overrides Entity:: |
|
EntityLegalDocument:: |
public | function | Set the published document version. | |
EntityLegalDocument:: |
public | function |
URI callback. Overrides Entity:: |
|
EntityLegalDocument:: |
public | function | Check if the given user has agreed to the current version of this document. | |
EntityLegalDocument:: |
public | function | Checks to see if a given user can agree to this document. | |
EntityLegalDocument:: |
public | function |
Overrides Entity:: |