/** * {@inheritdoc} * * Pipe the 'meters' entry point to the relevant meter handler. */ public function viewEntities($ids_string) { $ids = array_unique(array_filter(explode(',', $ids_string))); $output = array(); $account = $this->getAccount(); foreach ($ids as $id) { $node = node_load($id); if ($node->type == 'iec_meter') { $resource = 'iec_meters'; } elseif ($node->type == 'modbus_meter') { $resource = 'modbus_meters'; } // The resource, by convention, is the plural form of the content-type // (for 'modbus_meter', it'll be 'modbus_meters'). $resource = $node->type . 's'; $handler = restful_get_restful_handler($resource); // Pipe the account. $handler->setAccount($account); // Get the meter. $output[] = $handler->viewEntity($id); } // Prepare summary data for the formatter. $this->prepareSummary($this); return $output; }
/** * Login a user and return a JSON along with the authentication cookie. * * @return array * Array with the public fields populated. */ public function loginAndRespondWithCookie() { // Login the user. $account = $this->getAccount(); $this->loginUser(); $version = $this->getVersion(); $handler = restful_get_restful_handler('users', $version['major'], $version['minor']); $output = $handler ? $handler->viewEntity($account->uid) : array(); $output += restful_csrf_session_token(); return $output; }
/** * Get the meters list of the account. */ function getMeters(\EntityMetadataWrapper $wrapper) { $nid = $wrapper->getIdentifier(); $query = new EntityFieldQuery(); $result = $query->entityCondition('entity_type', 'node')->entityCondition('bundle', 'iec_meter')->propertyCondition('status', NODE_PUBLISHED)->fieldCondition(OG_AUDIENCE_FIELD, 'target_id', $nid)->propertyOrderBy('nid')->execute(); if (empty($result['node'])) { return; } $nids = array_keys($result['node']); $handler = restful_get_restful_handler('iec_meters'); return $handler->get(implode(',', $nids)); }
/** * {@inheritdoc} * * Pipe the 'notifications' entry point to the relevant notification handler. */ public function viewEntities($ids_string) { $ids = array_unique(array_filter(explode(',', $ids_string))); $output = array(); $account = $this->getAccount(); foreach ($ids as $id) { $message = message_load($id); if (preg_match('/^demand_/', $message->type)) { $resource = $message->type; } $handler = restful_get_restful_handler($resource); // Pipe the account. $handler->setAccount($account); // Get the meter. $output[] = $handler->viewEntity($id); } return $output; }
/** * Overrides ResfulEntityBase::createOrUpdateSubResourceItems(). * * When creating a new tag, Send the group ID to the "tags" resource. */ protected function createOrUpdateSubResourceItems(\RestfulInterface $handler, $value, $field_info) { $request = $this->getRequest(); // Return the entity ID that was created. if ($field_info['cardinality'] == 1) { // Single value. return $this->createOrUpdateSubResourceItem($value, $handler); } // Multiple values. $return = array(); foreach ($value as $value_item) { // Add group ID to the field "og_vocabulary" (categories, tags). if ($field_info['field_name'] == 'og_vocabulary' && is_array($value_item)) { $handler = restful_get_restful_handler('tags'); $value_item['group'] = $request['group']; } $return[] = $this->createOrUpdateSubResourceItem($value_item, $handler); } return $return; }
/** * Overrides RestfulEntityBase::getList(). */ public function getList() { $entity_type = $this->entityType; $result = $this ->getQueryForList() ->execute(); if (empty($result[$entity_type])) { return; } $account = $this->getAccount(); $request = $this->getRequest(); $ids = array_keys($result[$entity_type]); // Pre-load all entities. $entities = entity_load($entity_type, $ids); $return = array(); $handlers = array(); $resources_info = $this->getBundles(); foreach ($entities as $entity) { // Call each handler by its registered bundle. list($id,, $bundle) = entity_extract_ids($this->getEntityType(), $entity); if (empty($handlers[$bundle])) { $version = $this->getVersion(); $handlers[$bundle] = restful_get_restful_handler($resources_info[$bundle], $version['major'], $version['minor']); } $bundle_handler = $handlers[$bundle]; $bundle_handler->setAccount($account); $bundle_handler->setRequest($request); $return[] = $bundle_handler->viewEntity($id); } return $return; }
/** * Delete cached entities from all the cache bins associated to restful * resources. * * @param string $cid * The wildcard cache id to invalidate. */ public static function invalidateEntityCache($cid) { $plugins = restful_get_restful_plugins(); foreach ($plugins as $plugin) { $handler = restful_get_restful_handler($plugin['resource'], $plugin['major_version'], $plugin['minor_version']); if (method_exists($handler, 'cacheInvalidate')) { $version = $handler->getVersion(); // Get the uid for the invalidation. try { $uid = $handler->getAccount(FALSE)->uid; } catch (\RestfulUnauthorizedException $e) { // If no user could be found using the handler default to the logged in // user. $uid = $GLOBALS['user']->uid; } $version_cid = 'v' . $version['major'] . '.' . $version['minor'] . '::' . $handler->getResourceName() . '::uu' . $uid; $handler->cacheInvalidate($version_cid . '::' . $cid); } } }
/** * Helper function; Create an entity from a a sub-resource. * * @param string $property_name * The property name to set. * @param mixed $value * The value passed in the request. * @param array $field_info * The field info array. * @param string $public_field_name * The name of the public field to set. * * @return mixed * The value to set using the wrapped property. */ protected function createEntityFromReference($property_name, $value, $field_info, $public_field_name) { $public_fields = $this->getPublicFields(); if (empty($public_fields[$public_field_name]['resource'])) { // Field is not defined as "resource", which means it only accepts an // integer as a valid value. return $value; } if ($field_info['cardinality'] == 1 && !is_array($value)) { return $value; } // In case we have multiple bundles, we opt for the first one. $resource = reset($public_fields[$public_field_name]['resource']); $handler = restful_get_restful_handler($resource['name'], $resource['major_version'], $resource['minor_version']); return $this->createOrUpdateSubResourceItems($handler, $value, $field_info); }