diff --git commerce.info commerce.info index 40862e6..d94b88a 100644 --- commerce.info +++ commerce.info @@ -1,6 +1,7 @@ ; $Id$ name = Commerce description = Defines features and functions common to the Commerce modules. +dependencies[] = entity package = Commerce core = 7.x diff --git modules/store/commerce_store.admin.inc modules/store/commerce_store.admin.inc new file mode 100644 index 0000000..1050ae3 --- /dev/null +++ modules/store/commerce_store.admin.inc @@ -0,0 +1,95 @@ +path]['description'] = 'Manage stores, including fields.'; + return $items; + } +} + +/** + * Generates the message type editing form. + */ +function commerce_store_form($form, &$form_state, $store, $op = 'edit') { + if ($op == 'clone') { + $store->label .= ' (cloned)'; + $store->name .= '_clone'; + } + + $form['description'] = array( + '#title' => t('Description'), + '#type' => 'textfield', + '#default_value' => $store->description, + '#description' => t('The human-readable description of this store.'), + '#required' => TRUE, + ); + // Machine-readable type name. + $form['name'] = array( + '#type' => 'machine_name', + '#default_value' => isset($store->name) ? $store->name : '', + '#disabled' => entity_has_status('commerce_store', $store, ENTITY_IN_CODE), + '#machine_name' => array( + 'exists' => 'commerce_store_load', + 'source' => array('description'), + ), + '#description' => t('A unique machine-readable name for this store. It must only contain lowercase letters, numbers, and underscores.'), + ); + + field_attach_form('commerce_store', $store, $form, $form_state); + + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save store'), + '#weight' => 40, + ); + + if (!entity_has_status('commerce_store', $store, ENTITY_IN_CODE) && $op != 'add') { + $form['actions']['delete'] = array( + '#type' => 'submit', + '#value' => t('Delete store'), + '#weight' => 45, + '#limit_validation_errors' => array(), + '#submit' => array('commerce_store_form_submit_delete') + ); + } + return $form; +} + +/** + * Validation callback. + */ +function commerce_store_form_validate($form, &$form_state) { + entity_form_field_validate('commerce_store', $form, $form_state); +} + +/** + * Form API submit callback for the type form. + */ +function commerce_store_form_submit(&$form, &$form_state) { + $commerce_store = entity_ui_form_submit_build_entity($form, $form_state); + // Save and go back. + $commerce_store->save(); + $form_state['redirect'] = 'admin/structure/commerce-store'; +} + +/** + * Form API submit callback for the delete button. + */ +function commerce_store_form_submit_delete(&$form, &$form_state) { + $form_state['redirect'] = 'admin/structure/commerce-store/manage/' . $form_state['commerce_store']->name . '/delete'; +} diff --git modules/store/commerce_store.info modules/store/commerce_store.info new file mode 100644 index 0000000..1252552 --- /dev/null +++ modules/store/commerce_store.info @@ -0,0 +1,9 @@ +; $Id$ +name = Store +description = Defines the Store entity and associated features. +package = Commerce +dependencies[] = commerce +dependencies[] = entity +core = 7.x + +files[] = commerce_store.admin.inc \ No newline at end of file diff --git modules/store/commerce_store.install modules/store/commerce_store.install new file mode 100644 index 0000000..536e72d --- /dev/null +++ modules/store/commerce_store.install @@ -0,0 +1,59 @@ + 'The base table for stores.', + 'fields' => array( + 'id' => array( + 'description' => 'The primary identifier for a store.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'name' => array( + 'description' => 'The unified identifier for a store.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'description' => array( + 'description' => 'Description for this store.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), + 'created' => array( + 'description' => 'The Unix timestamp when the order was created.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + 'changed' => array( + 'description' => 'The Unix timestamp when the order was most recently saved.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + ) + entity_exportable_schema_fields(), + 'primary key' => array('id'), + 'unique keys' => array( + 'name' => array('name'), + ), + 'foreign keys' => array( + 'owner' => array( + 'table' => 'users', + 'columns' => array('uid' => 'uid'), + ), + ), + ); + + return $schema; +} diff --git modules/store/commerce_store.module modules/store/commerce_store.module new file mode 100644 index 0000000..140e090 --- /dev/null +++ modules/store/commerce_store.module @@ -0,0 +1,249 @@ + t('Store'), + 'controller class' => 'EntityAPIController', + 'entity class' => 'Store', + 'base table' => 'commerce_store', + 'fieldable' => TRUE, + 'entity keys' => array( + 'id' => 'id', + 'label' => 'description', + 'name' => 'name', + ), + 'exportable' => TRUE, + 'export' => array( + 'default hook' => 'default_commerce_store', + ), + 'module' => 'commerce_store', + 'access callback' => 'commerce_store_access', + // Enable the entity API's admin UI. + 'admin ui' => array( + 'path' => 'admin/structure/commerce-store', + 'file' => 'commerce_store.admin.inc', + 'controller class' => 'StoreUIController', + ), + ); + + return $items; +} + +/** + * Implements hook_permission(). + */ +function commerce_store_permission() { + $permissions = array( + 'administer stores' => array( + 'title' => t('Administer stores'), + 'restrict access' => TRUE, + ), + ); + + return $permissions; +} + +/** + * Implements hook_default_commerce_store(). + */ +function commerce_store_default_commerce_store() { + return commerce_store_create('default', array('description' => t('Default store'))); +} + +/** + * A class used for message types. + */ +class Store extends Entity { + + public $name; + + public function __construct($values = array()) { + parent::__construct($values, 'commerce_store'); + + // Set creation date. + if (!isset($this->timestamp)) { + $this->timestamp = time(); + } + } +} + +/** + * Creates a new store. + * + * If a store already exists, an exception will be thrown. + * + * @return Store + * Returns a new store object. + */ +function commerce_store_create($name, $values = array()) { + // Make sure the message type doesn't already exist, to prevent duplicate key + // error. + if (commerce_store_load($name)) { + throw new Exception('Commerce store ' . check_plain($name) . ' already exists.'); + } + $values['name'] = $name; + $return = entity_create('commerce_store', $values); + + return $return; +} + +/** + * Store loader. + * + * @param $type_name + * (optional) The name for this message type. If no type is given all existing + * types are returned. + * + * @return Store + * Returns a fully-loaded store definition if a name is passed. + * Else an array containing all stores is returned. + */ +function commerce_store_load($name = NULL) { + // Replace dashes with underscores so this can be used as menu argument + // loader too. + $types = entity_load('commerce_store', isset($name) ? array(strtr($name, array('-' => '_'))) : FALSE); + if (isset($name)) { + return isset($types[$name]) ? $types[$name] : FALSE; + } + return $types; +} + +/** + * Inserts or updates a store object into the database. + * + * @param $message + * The message object to be inserted. + * + * @return + * Failure to write a record will return FALSE. Otherwise SAVED_NEW or + * SAVED_UPDATED is returned depending on the operation performed. + */ +function commerce_store_save($message) { + return entity_save('commerce_store', $message); +} + +/** + * Deletes an existing store. + * + * @param $message + * The message object to be deleted. + */ +function commerce_store_delete($message) { + return entity_delete('commerce_store', $message); +} + +/** + * Checks store access for various operations. + * + * @param $op + * The operation being performed. One of 'view', 'update', 'create' or + * 'delete'. + * @param $order + * Optionally an order to check access for. + * @param $account + * The user to check for. Leave it to NULL to check for the current user. + */ +function commerce_store_access($op, $order = NULL, $account = NULL) { + // TODO + return TRUE; +} + +/** + * Implements hook_field_schema(). + */ +function commerce_store_field_schema($field) { + $columns = array( + 'id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ), + ); + return array( + 'columns' => $columns, + 'indexes' => array('id' => array('id')), + ); +} + + + +/** + * Implements hook_field_info(). + */ +function commerce_store_field_info() { + return array( + 'commerce_store_reference' => array( + 'label' => t('Store reference'), + 'description' => t('This field stores the ID of a related store as an integer value.'), + 'settings' => array(), + 'instance_settings' => array(), + 'default_widget' => 'options_select', + 'default_formatter' => 'commerce_store_default', + 'property_type' => 'commerce_store', + 'property_callbacks' => array('commerce_store_property_info_callback'), + ), + ); +} + +/** + * Implements hook_field_validate(). + */ +function commerce_store_field_validate($entity_type, $object, $field, $instance, $langcode, $items, &$errors) { +} + +/** + * Implements hook_field_is_empty(). + */ +function commerce_store_field_is_empty($item, $field) { + return empty($item['id']); +} + +/** + * Implements hook_field_formatter_info(). + */ +function commerce_store_field_formatter_info() { + return array( + 'commerce_store_default' => array( + 'label' => t('Store view'), + 'description' => t('Display the line items via a default View.'), + 'field types' => array('commerce_store_reference'), + // TODO: Implement view. + 'settings' => array( + 'view' => 'commerce_store_table|default', + ), + ), + ); +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function commerce_store_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function commerce_store_field_formatter_settings_summary($field, $instance, $view_mode) { +} + +/** + * Implements hook_field_formatter_view(). + */ +function commerce_store_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $result[0] = array( + '#markup' => $entity->view(), + ); + + return $result; +} \ No newline at end of file