You are here

function _itoggle_ajax_toggle_entity in iToggle 7.2

Same name and namespace in other branches
  1. 7 itoggle.module \_itoggle_ajax_toggle_entity()

Change an entity property's value.

Parameters

string: The entity $type.

string: The entity $property.

int $id: The entity'd id.

int $value: The new value of the entity property.

Return value

boolean Whether we managed to change the property value.

See also

itoggle_ajax_callback()

1 call to _itoggle_ajax_toggle_entity()
itoggle_ajax_callback in ./itoggle.pages.inc
Page callback. iToggle widget AJAX callback.

File

./itoggle.pages.inc, line 151
iToggle ajax functions.

Code

function _itoggle_ajax_toggle_entity($type, $property, $id, $value) {
  $access = FALSE;

  // Check permissions.
  switch ($type) {
    case 'node':
      $access = user_access('bypass node access') || user_access('administer nodes');
      break;
    case 'user':
      $access = user_access('administer users');
      break;
    case 'taxonomy_term':
    case 'taxomomy_vocabulary':
      $access = user_access('administer taxonomy');
      break;
    default:
      $info = itoggle_get_entity_info();

      // Try to use access callback if available.
      if (isset($info[$type]['access callback'])) {
        $entity = entity_load_single($type, $id);
        $access = call_user_func($info[$type]['access callback'], 'edit', $entity);
      }
      else {
        $access = user_access('administer site configuration');
      }
  }
  if (!$access && $type === 'node') {
    if ($node = node_load($id)) {
      if (module_exists('override_node_options')) {
        switch ($property) {
          case 'status':
            $prop = 'published';
            break;
          case 'promote':
            $prop = 'promote to front page';
            break;
          case 'sticky':
            $prop = 'sticky';
            break;
          default:

            // Prevent errors, access check will fail anyway.
            $prop = '';
            break;
        }
        $access = user_access("override {$node->type} {$prop} option");
      }

      // If still no access move onto check perms by "edit any [type] content".
      if (!$access) {
        $access = user_access("edit any {$node->type} content");
      }
    }
  }
  if ($access === TRUE) {
    if ($entity = entity_load_single($type, $id)) {
      $entity->{$property} = $value;
      try {
        $return = entity_save($type, $entity) !== FALSE;
      } catch (Exception $e) {

        // Fail silently.
        $return = FALSE;
      }
      return $return !== FALSE;
    }
  }
  return FALSE;
}