12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856 |
- <?php
- /**
- * @file
- * The product module for Ubercart.
- *
- * Provides information that is common to all products, and user-defined product
- * classes for more specification.
- */
- /**
- * Implements hook_menu().
- */
- function uc_product_menu() {
- $items = array();
- $items['admin/store/products'] = array(
- 'title' => 'Products',
- 'description' => 'Administer products, classes, and more.',
- 'page callback' => 'drupal_goto',
- 'page arguments' => array('admin/store/products/view'),
- 'access arguments' => array('administer products'),
- 'weight' => -2,
- 'position' => 'left',
- );
- // admin/store/products/view is provided by Views.
- $items['admin/store/products/classes'] = array(
- 'title' => 'Manage classes',
- 'description' => 'Create and edit product node types.',
- 'access arguments' => array('administer product classes'),
- 'page callback' => 'uc_product_class_default',
- 'weight' => -2,
- 'file' => 'uc_product.admin.inc',
- );
- $items['admin/store/settings/products'] = array(
- 'title' => 'Products',
- 'description' => 'Configure product settings.',
- 'access arguments' => array('administer products'),
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('uc_product_settings_form'),
- 'file' => 'uc_product.admin.inc',
- );
- // Insert subitems into the edit node page for product types.
- $items['node/%node/edit/product'] = array(
- 'title' => 'Product',
- 'access callback' => 'uc_product_edit_access',
- 'access arguments' => array(1),
- 'weight' => -10,
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'file' => 'uc_product.admin.inc',
- );
- $features = module_invoke_all('uc_product_feature');
- if (!empty($features)) {
- $items['node/%node/edit/features'] = array(
- 'title' => 'Features',
- 'page callback' => 'uc_product_features',
- 'page arguments' => array(1),
- 'access callback' => 'uc_product_feature_access',
- 'access arguments' => array(1),
- 'weight' => 10,
- 'type' => MENU_LOCAL_TASK,
- 'file' => 'uc_product.admin.inc',
- );
- $items['node/%node/edit/features/%/%'] = array(
- 'page callback' => 'uc_product_feature_edit',
- 'page arguments' => array(1, 4, 5),
- 'access callback' => 'uc_product_feature_access',
- 'access arguments' => array(1),
- 'file' => 'uc_product.admin.inc',
- );
- $items['node/%node/edit/features/%/%uc_product_feature/delete'] = array(
- 'title' => 'Delete feature',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('uc_product_feature_confirm_delete', 1, 4, 5),
- 'access callback' => 'uc_product_feature_access',
- 'access arguments' => array(1),
- 'file' => 'uc_product.admin.inc',
- );
- }
- $items['admin/store/settings/products/defaults'] = array(
- 'access arguments' => array('administer products'),
- 'page callback' => 'uc_product_image_defaults',
- 'file' => 'uc_product.admin.inc',
- );
- $items['admin/store/products/classes/%uc_product_class'] = array(
- 'title' => 'Product class',
- 'access arguments' => array('administer product classes'),
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('uc_product_class_form', 4),
- 'file' => 'uc_product.admin.inc',
- );
- $items['admin/store/products/classes/%uc_product_class/edit'] = array(
- 'title' => 'Edit',
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => -5,
- 'file' => 'uc_product.admin.inc',
- );
- $items['admin/store/products/classes/%uc_product_class/delete'] = array(
- 'access callback' => 'uc_product_class_delete_access',
- 'access arguments' => array(4),
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('uc_product_class_delete_confirm', 4),
- 'file' => 'uc_product.admin.inc',
- );
- return $items;
- }
- /**
- * Menu access callback for deleting product classes.
- */
- function uc_product_class_delete_access($class) {
- return user_access('administer product classes') && !$class->locked;
- }
- /**
- * Implements hook_admin_paths().
- */
- function uc_product_admin_paths() {
- $paths = array(
- 'node/*/edit/product' => TRUE,
- 'node/*/edit/features' => TRUE,
- 'node/*/edit/features/*' => TRUE,
- );
- return $paths;
- }
- /**
- * Implements hook_permission().
- */
- function uc_product_permission() {
- $perms = array(
- 'administer products' => array(
- 'title' => t('Administer products'),
- ),
- 'administer product classes' => array(
- 'title' => t('Administer product classes'),
- ),
- 'administer product features' => array(
- 'title' => t('Administer product features'),
- ),
- 'administer own product features' => array(
- 'title' => t('Administer own product features'),
- ),
- );
- return $perms;
- }
- /**
- * Menu access callback for 'node/%node/edit/features'.
- */
- function uc_product_feature_access($node) {
- global $user;
- return uc_product_is_product($node) && $node->type != 'product_kit' &&
- ((user_access('administer product features') || (user_access('administer own product features') && ($user->uid == $node->uid))));
- }
- /**
- * Implements hook_theme().
- */
- function uc_product_theme() {
- return array(
- 'uc_product_model' => array(
- 'variables' => array('model' => '', 'view_mode' => 'full', 'attributes' => array()),
- 'file' => 'uc_product.theme.inc',
- ),
- 'uc_product_add_to_cart' => array(
- 'variables' => array('form' => NULL, 'view_mode' => 'full'),
- 'file' => 'uc_product.theme.inc',
- ),
- 'uc_product_price' => array(
- 'render element' => 'element',
- 'file' => 'uc_product.theme.inc',
- ),
- 'uc_product_weight' => array(
- 'variables' => array('amount' => 0, 'units' => NULL, 'view_mode' => 'full', 'attributes' => array()),
- 'file' => 'uc_product.theme.inc',
- ),
- 'uc_product_dimensions' => array(
- 'variables' => array('length' => 0, 'width' => 0, 'height' => 0, 'units' => NULL, 'view_mode' => 'full', 'attributes' => array()),
- 'file' => 'uc_product.theme.inc',
- ),
- 'uc_product_image' => array(
- 'variables' => array('images' => array(), 'view_mode' => 'full'),
- 'file' => 'uc_product.theme.inc',
- ),
- 'uc_product_feature_add_form' => array(
- 'render element' => 'form',
- 'file' => 'uc_product.admin.inc',
- ),
- );
- }
- /**
- * Implements hook_field_formatter_info().
- */
- function uc_product_field_formatter_info() {
- return array(
- 'uc_product_image' => array(
- 'label' => t('Ubercart product'),
- 'description' => t('The first image is displayed with the "uc_product" image style, and subsequent images are displayed below it with the "uc_thumbnail" image style.'),
- 'field types' => array('image'),
- ),
- );
- }
- /**
- * Implements hook_field_formatter_view().
- */
- function uc_product_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
- $element = array();
- switch ($display['type']) {
- case 'uc_product_image':
- $element[0] = array(
- '#theme' => 'uc_product_image',
- '#images' => $items,
- );
- break;
- }
- return $element;
- }
- /**
- * Implements hook_node_info().
- *
- * Creates node types for each product class and other product modules.
- */
- function uc_product_node_info($reset = FALSE) {
- static $types = array();
- $title_label = t('Name');
- if (empty($types) || $reset) {
- $types = array();
- $defaults = module_invoke_all('uc_product_default_classes');
- $types['product'] = isset($defaults['product']) ? $defaults['product'] : array();
- $types['product'] += array(
- 'name' => t('Product'),
- 'base' => 'uc_product',
- 'description' => t('Use <em>products</em> to represent items for sale on the website, including all the unique information that can be attributed to a specific model number.'),
- 'title_label' => $title_label,
- );
- $classes = uc_product_class_load(NULL, $reset);
- foreach ($classes as $class) {
- $class = (array) $class;
- $class['base'] = 'uc_product';
- $types[$class['pcid']] = $class + array(
- 'title_label' => $title_label,
- );
- }
- }
- return $types;
- }
- /**
- * Implements hook_node_type_update().
- *
- * Ensure product class names and descriptions are synchronised if the
- * content type is updated.
- */
- function uc_product_node_type_update($info) {
- $existing_type = !empty($info->old_type) ? $info->old_type : $info->type;
- db_update('uc_product_classes')
- ->fields(array(
- 'pcid' => $info->type,
- 'name' => $info->name,
- 'description' => $info->description,
- ))
- ->condition('pcid', $existing_type)
- ->execute();
- // Reset static variable data and load new info.
- uc_product_node_info(TRUE);
- }
- /**
- * Implements hook_forms().
- *
- * Register an "add to cart" form for each product to prevent id collisions.
- */
- function uc_product_forms($form_id, $args) {
- $forms = array();
- if (isset($args[0]->nid) && isset($args[0]->type)) {
- $product = $args[0];
- if (in_array($product->type, array_keys(uc_product_node_info()))) {
- $forms['uc_product_add_to_cart_form_' . $product->nid] = array('callback' => 'uc_product_add_to_cart_form');
- $forms['uc_catalog_buy_it_now_form_' . $product->nid] = array('callback' => 'uc_catalog_buy_it_now_form');
- }
- }
- return $forms;
- }
- /**
- * Menu access callback for 'node/%node/edit/product'.
- */
- function uc_product_edit_access($node) {
- // Re-inherit access callback for 'node/%node/edit'.
- return uc_product_is_product($node) && node_access('update', $node);
- }
- /**
- * Implements hook_form().
- *
- * @ingroup forms
- */
- function uc_product_form($node, $form_state) {
- $type = node_type_get_type($node);
- $form['title'] = array(
- '#type' => 'textfield',
- '#title' => check_plain($type->title_label),
- '#required' => TRUE,
- '#default_value' => $node->title,
- '#maxlength' => 255,
- '#weight' => -5,
- );
- $form['base'] = array(
- '#type' => 'fieldset',
- '#title' => t('Product information'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#weight' => -10,
- '#attributes' => array('class' => array('product-field')),
- '#group' => 'additional_settings',
- '#attached' => array(
- 'js' => array(
- 'vertical-tabs' => drupal_get_path('module', 'uc_product') . '/uc_product.js',
- ),
- ),
- );
- $form['base']['model'] = array(
- '#type' => 'textfield',
- '#title' => t('SKU'),
- '#required' => TRUE,
- '#default_value' => $node->model,
- '#description' => t('Product SKU/model.'),
- '#weight' => 0,
- '#size' => 32,
- );
- $form['base']['prices'] = array(
- '#type' => 'container',
- '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
- '#weight' => 5,
- );
- $form['base']['prices']['list_price'] = array(
- '#type' => 'uc_price',
- '#title' => t('List price'),
- '#default_value' => $node->list_price,
- '#description' => t('The listed MSRP.'),
- '#weight' => 0,
- );
- $form['base']['prices']['cost'] = array(
- '#type' => 'uc_price',
- '#title' => t('Cost'),
- '#default_value' => $node->cost,
- '#description' => t("Your store's cost."),
- '#weight' => 1,
- );
- $form['base']['prices']['sell_price'] = array(
- '#type' => 'uc_price',
- '#title' => t('Sell price'),
- '#required' => TRUE,
- '#default_value' => $node->sell_price,
- '#description' => t('Customer purchase price.'),
- '#weight' => 2,
- );
- $form['base']['shippable'] = array(
- '#type' => 'checkbox',
- '#title' => t('Product is shippable.'),
- '#default_value' => $node->shippable,
- '#weight' => 10,
- );
- $form['base']['weight'] = array(
- '#type' => 'container',
- '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
- '#states' => array(
- 'invisible' => array(
- 'input[name="shippable"]' => array('checked' => FALSE),
- ),
- ),
- '#weight' => 15,
- );
- $form['base']['weight']['weight'] = array(
- '#type' => 'textfield',
- '#title' => t('Weight'),
- '#default_value' => $node->weight,
- '#size' => 10,
- '#element_validate' => array('uc_store_validate_number'),
- );
- $units = array(
- 'lb' => t('Pounds'),
- 'kg' => t('Kilograms'),
- 'oz' => t('Ounces'),
- 'g' => t('Grams'),
- );
- $form['base']['weight']['weight_units'] = array(
- '#type' => 'select',
- '#title' => t('Units'),
- '#default_value' => $node->weight_units,
- '#options' => $units,
- );
- $form['base']['dimensions'] = array(
- '#type' => 'container',
- '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
- '#states' => array(
- 'invisible' => array(
- 'input[name="shippable"]' => array('checked' => FALSE),
- ),
- ),
- '#weight' => 20,
- );
- $form['base']['dimensions']['dim_length'] = array(
- '#type' => 'textfield',
- '#title' => t('Length'),
- '#default_value' => $node->length,
- '#size' => 10,
- '#element_validate' => array('uc_store_validate_number'),
- );
- $form['base']['dimensions']['dim_width'] = array(
- '#type' => 'textfield',
- '#title' => t('Width'),
- '#default_value' => $node->width,
- '#size' => 10,
- '#element_validate' => array('uc_store_validate_number'),
- );
- $form['base']['dimensions']['dim_height'] = array(
- '#type' => 'textfield',
- '#title' => t('Height'),
- '#default_value' => $node->height,
- '#size' => 10,
- '#element_validate' => array('uc_store_validate_number'),
- );
- $form['base']['dimensions']['length_units'] = array(
- '#type' => 'select',
- '#title' => t('Units'),
- '#options' => array(
- 'in' => t('Inches'),
- 'ft' => t('Feet'),
- 'cm' => t('Centimeters'),
- 'mm' => t('Millimeters'),
- ),
- '#default_value' => $node->length_units,
- );
- $form['base']['pkg_qty'] = array(
- '#type' => 'uc_quantity',
- '#title' => t('Maximum package quantity'),
- '#default_value' => $node->pkg_qty,
- '#description' => t('At most, how many of these items can fit in your largest box? Orders that exceed this value will be split into multiple packages when retrieving shipping quotes.'),
- '#weight' => 25,
- '#states' => array(
- 'invisible' => array(
- 'input[name="shippable"]' => array('checked' => FALSE),
- ),
- ),
- );
- if (variable_get('uc_product_add_to_cart_qty', FALSE)) {
- $form['base']['default_qty'] = array(
- '#type' => 'uc_quantity',
- '#title' => t('Default quantity to add to cart'),
- '#default_value' => $node->default_qty,
- '#description' => t('Use 0 to disable the quantity field next to the add to cart button.'),
- '#weight' => 27,
- '#allow_zero' => TRUE,
- );
- }
- else {
- $form['base']['default_qty'] = array(
- '#type' => 'value',
- '#value' => $node->default_qty,
- );
- }
- $form['base']['ordering'] = array(
- '#type' => 'weight',
- '#title' => t('List position'),
- '#description' => t("Specify a value to set this product's position in product lists.<br />Products in the same position will be sorted alphabetically."),
- '#delta' => 25,
- '#default_value' => $node->ordering,
- '#weight' => 30,
- );
- return $form;
- }
- /**
- * Implements hook_prepare().
- */
- function uc_product_prepare($node) {
- $defaults = array(
- 'model' => '',
- 'list_price' => 0,
- 'cost' => 0,
- 'sell_price' => 0,
- 'weight' => 0,
- 'weight_units' => variable_get('uc_weight_unit', 'lb'),
- 'length' => 0,
- 'width' => 0,
- 'height' => 0,
- 'length_units' => variable_get('uc_length_unit', 'in'),
- 'pkg_qty' => 1,
- 'default_qty' => 1,
- 'shippable' => variable_get('uc_product_shippable_' . $node->type, 1),
- 'ordering' => 0,
- );
- foreach ($defaults as $key => $value) {
- if (!isset($node->$key)) {
- $node->$key = $value;
- }
- }
- }
- /**
- * Implements hook_insert().
- */
- function uc_product_insert($node) {
- if (isset($node->dim_length)) {
- $node->length = $node->dim_length;
- }
- if (isset($node->dim_width)) {
- $node->width = $node->dim_width;
- }
- if (isset($node->dim_height)) {
- $node->height = $node->dim_height;
- }
- drupal_write_record('uc_products', $node);
- }
- /**
- * Implements hook_update().
- */
- function uc_product_update($node) {
- if (isset($node->dim_length)) {
- $node->length = $node->dim_length;
- }
- if (isset($node->dim_width)) {
- $node->width = $node->dim_width;
- }
- if (isset($node->dim_height)) {
- $node->height = $node->dim_height;
- }
- if (!empty($node->revision)) {
- drupal_write_record('uc_products', $node);
- }
- else {
- drupal_write_record('uc_products', $node, 'vid');
- }
- }
- /**
- * Implements hook_load().
- */
- function uc_product_load($nodes) {
- $vids = array();
- foreach ($nodes as $node) {
- $vids[$node->nid] = $node->vid;
- }
- $result = db_query('SELECT nid, model, list_price, cost, sell_price, weight, weight_units, length, width, height, length_units, pkg_qty, default_qty, ordering, shippable FROM {uc_products} WHERE vid IN (:vids)', array(':vids' => $vids));
- foreach ($result as $node) {
- foreach ($node as $field => $value) {
- $nodes[$node->nid]->$field = $value;
- }
- $nodes[$node->nid]->price = $nodes[$node->nid]->sell_price;
- }
- }
- /**
- * Gets a specific, cloned, altered variant of a product node.
- *
- * Generally, you should always use uc_product_load_variant() instead,
- * except when node_load() cannot be invoked, e.g. when implementing
- * hook_node_load().
- *
- * @param $node
- * The product node to alter. Throws an exception if this is already a
- * product variant.
- * @param $data
- * Optional data to add to the product before invoking the alter hooks.
- *
- * @return
- * An variant of the product, altered based on the provided data.
- */
- function _uc_product_get_variant($node, $data = FALSE) {
- if (!empty($node->variant)) {
- throw new Exception(t('Cannot create a variant of a variant.'));
- }
- $node = clone $node;
- if (!empty($data)) {
- $node->data = $data;
- }
- // Ensure that $node->data is an array (user module leaves it serialized).
- if (isset($node->data) && !is_array($node->data)) {
- $node->data = unserialize($node->data);
- }
- drupal_alter('uc_product', $node);
- $node->variant = TRUE;
- if (!isset($node->data['module'])) {
- $node->data['module'] = 'uc_product';
- }
- return $node;
- }
- /**
- * Loads a specific altered variant of a product node.
- *
- * The (possibly cached) base product remains unaltered.
- *
- * @param $nid
- * The nid of the product to load.
- * @param $data
- * Optional data to add to the product before invoking the alter hooks.
- *
- * @return
- * A variant of the product, altered based on the provided data, or FALSE
- * if the node is not found.
- */
- function uc_product_load_variant($nid, $data = FALSE) {
- if ($node = node_load($nid)) {
- return _uc_product_get_variant($node, $data);
- }
- else {
- return FALSE;
- }
- }
- /**
- * Implements hook_uc_product_alter().
- *
- * Invokes rules event to allow product modifications.
- */
- function uc_product_uc_product_alter(&$node) {
- if (module_exists('rules')) {
- rules_invoke_event('uc_product_load', $node);
- }
- }
- /**
- * Implements hook_delete().
- */
- function uc_product_delete(&$node) {
- $features = uc_product_feature_load_multiple($node->nid);
- foreach ($features as $feature) {
- uc_product_feature_delete($feature->pfid);
- }
- db_delete('uc_products')
- ->condition('nid', $node->nid)
- ->execute();
- }
- /**
- * Dynamically replaces parts of a product view based on form input.
- *
- * If a module adds an input field to the add-to-cart form which affects some
- * aspect of a product (e.g. display price or weight), it should attach an
- * #ajax callback to that form element, and use this function in the callback
- * to build updated content for the affected fields.
- *
- * @param $form_state
- * The current form state. This must contain a 'variant' entry in the
- * 'storage' array which represents the product as configured by user input
- * data. In most cases, this is provided automatically by
- * uc_product_add_to_cart_form_validate().
- * @param $keys
- * An array of keys in the built product content which should be replaced
- * (e.g. 'display_price').
- *
- * @return
- * An array of Ajax commands.
- */
- function uc_product_view_ajax_commands($form_state, $keys) {
- $commands = array();
- if (variable_get('uc_product_update_node_view', FALSE) && !empty($form_state['storage']['variant'])) {
- $node_div = '.uc-product-' . $form_state['storage']['variant']->nid;
- $build = node_view($form_state['storage']['variant']);
- foreach ($keys as $key) {
- if (isset($build[$key])) {
- $id = $node_div . '.' . str_replace('_', '-', $key);
- $commands[] = ajax_command_replace($id, drupal_render($build[$key]));
- }
- }
- }
- return $commands;
- }
- /**
- * Implements hook_view().
- */
- function uc_product_view($node, $view_mode) {
- // Give modules a chance to alter this product. If it is a variant, this will
- // have been done already by uc_product_load_variant(), so we check a flag to
- // be sure not to alter twice -- cf. entity_prepare_view().
- $variant = empty($node->variant) ? _uc_product_get_variant($node) : $node;
- // Skip the add to cart form in comment reply forms.
- if (arg(0) != 'comment' && arg(1) != 'reply') {
- // Build the 'add to cart' form, and use the updated variant based on data
- // provided by the form (e.g. attribute default options).
- if (module_exists('uc_cart') && $variant->nid && empty($variant->data['display_only'])) {
- $add_to_cart_form = drupal_get_form('uc_product_add_to_cart_form_' . $variant->nid, $variant);
- if (variable_get('uc_product_update_node_view', FALSE)) {
- $variant = $add_to_cart_form['node']['#value'];
- }
- }
- }
- $node->content['display_price'] = array(
- '#theme' => 'uc_product_price',
- '#value' => isset($variant->price) ? $variant->price : $variant->sell_price,
- '#suffixes' => array(),
- '#attributes' => array(
- 'class' => array(
- 'display-price',
- 'uc-product-' . $node->nid,
- ),
- ),
- );
- $node->content['model'] = array(
- '#theme' => 'uc_product_model',
- '#model' => $variant->model,
- '#view_mode' => $view_mode,
- );
- $node->content['list_price'] = array(
- '#theme' => 'uc_product_price',
- '#title' => t('List price:'),
- '#value' => $variant->list_price,
- '#attributes' => array(
- 'class' => array(
- 'list-price',
- ),
- ),
- );
- $node->content['cost'] = array(
- '#theme' => 'uc_product_price',
- '#title' => t('Cost:'),
- '#value' => $variant->cost,
- '#attributes' => array(
- 'class' => array(
- 'cost',
- 'uc-product-' . $node->nid,
- ),
- ),
- '#access' => user_access('administer products'),
- );
- $node->content['sell_price'] = array(
- '#theme' => 'uc_product_price',
- '#title' => t('Price:'),
- '#value' => $variant->sell_price,
- '#attributes' => array(
- 'class' => array(
- 'sell-price',
- ),
- ),
- );
- $node->content['weight'] = array(
- '#theme' => 'uc_product_weight',
- '#amount' => $variant->weight,
- '#units' => $variant->weight_units,
- '#view_mode' => $view_mode,
- '#attributes' => array(
- 'class' => array(
- 'uc-product-' . $node->nid,
- ),
- ),
- );
- $node->content['dimensions'] = array(
- '#theme' => 'uc_product_dimensions',
- '#length' => $variant->length,
- '#width' => $variant->width,
- '#height' => $variant->height,
- '#units' => $variant->length_units,
- '#view_mode' => $view_mode,
- );
- if (isset($add_to_cart_form)) {
- $node->content['add_to_cart'] = array(
- '#theme' => 'uc_product_add_to_cart',
- '#view_mode' => $view_mode,
- '#form' => $add_to_cart_form,
- );
- }
- $node->content['#node'] = $variant;
- return $node;
- }
- /**
- * Implements hook_preprocess_node().
- *
- * Product classes default to using node--product.tpl.php if they don't have
- * their own template.
- *
- * @see theme()
- * @see MODULE_preprocess_HOOK()
- */
- function uc_product_preprocess_node(&$variables) {
- if (uc_product_is_product($variables['type'])) {
- array_unshift($variables['theme_hook_suggestions'], 'node__product');
- }
- }
- /**
- * Implements hook_preprocess_html().
- *
- * Adds a body class to product node pages.
- *
- * @see html.tpl.php
- */
- function uc_product_preprocess_html(&$variables) {
- if ($node = menu_get_object()) {
- if (uc_product_is_product($node)) {
- $variables['classes_array'][] = 'uc-product-node';
- }
- }
- }
- /**
- * Implements hook_form_alter().
- *
- * @see uc_product_save_continue_submit()
- */
- function uc_product_form_alter(&$form, &$form_state, $form_id) {
- // Add a button to product node forms to continue editing after saving.
- if (uc_product_is_product_form($form)) {
- $form['actions']['save_continue'] = array(
- '#type' => 'submit',
- '#value' => t('Save and continue'),
- '#weight' => 7,
- '#submit' => array('node_form_submit', 'uc_product_save_continue_submit'),
- );
- }
- }
- /**
- * After the node is saved, redirects to the node edit page.
- *
- * Some modules add local tasks to product edit forms, but only after it has an
- * nid. Redirecting facilitates the common workflow of continuing to those
- * tasks.
- */
- function uc_product_save_continue_submit($form, &$form_state) {
- $form_state['redirect'] = 'node/' . $form_state['nid'] . '/edit';
- }
- /**
- * Implements hook_form_FORM_ID_alter().
- *
- * Adds a default image field setting to product content types.
- */
- function uc_product_form_node_type_form_alter(&$form, &$form_state) {
- $type = $form['#node_type'];
- if (!uc_product_is_product($type->type)) {
- return;
- }
- $form['uc_product'] = array(
- '#type' => 'fieldset',
- '#title' => t('Ubercart product settings'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'additional_settings',
- '#attached' => array(
- 'js' => array(
- 'vertical-tabs' => drupal_get_path('module', 'uc_product') . '/uc_product.js',
- ),
- ),
- );
- // Shippable.
- $form['uc_product']['uc_product_shippable'] = array(
- '#type' => 'checkbox',
- '#title' => t('Product and its derivatives are shippable.'),
- '#default_value' => variable_get('uc_product_shippable_' . $type->type, 1),
- '#description' => t('This setting can still be overridden on the node form.'),
- '#return_value' => 1,
- '#weight' => -5,
- );
- // Image field.
- $instances = field_info_instances('node', $type->type);
- $options = array('' => t('None'));
- foreach ($instances as $field_name => $instance) {
- if (strpos($instance['widget']['type'], 'image') !== FALSE) {
- $options[$field_name] = $instance['label'];
- }
- }
- $form['uc_product']['uc_image'] = array(
- '#type' => 'select',
- '#title' => t('Product image field'),
- '#default_value' => variable_get('uc_image_' . $type->type, NULL),
- '#options' => $options,
- '#description' => t('The selected field will be used on Ubercart pages to represent the products of this content type.'),
- '#weight' => -4,
- );
- }
- /**
- * Implements hook_field_extra_fields().
- *
- * Adds the "Product information".
- */
- function uc_product_field_extra_fields() {
- $extra = array();
- foreach (uc_product_types() as $type) {
- $extra['node'][$type] = array(
- 'display' => array(
- 'display_price' => array(
- 'label' => t('Display price'),
- 'description' => t('High-visibility sell price.'),
- 'weight' => -1,
- ),
- 'model' => array(
- 'label' => t('SKU'),
- 'description' => t('Product SKU/model.'),
- 'weight' => 0,
- ),
- 'list_price' => array(
- 'label' => t('List price'),
- 'description' => t('The listed MSRP.'),
- 'weight' => 2,
- ),
- 'cost' => array(
- 'label' => t('Cost'),
- 'description' => t("Your store's cost."),
- 'weight' => 3,
- ),
- 'sell_price' => array(
- 'label' => t('Sell price'),
- 'description' => t('Customer purchase price.'),
- 'weight' => 4,
- ),
- 'weight' => array(
- 'label' => t('Weight'),
- 'description' => t('Physical weight (lbs, kg, etc.).'),
- 'weight' => 5,
- ),
- 'dimensions' => array(
- 'label' => t('Dimensions'),
- 'description' => t('Physical dimensions of the packaged product.'),
- 'weight' => 6,
- ),
- 'add_to_cart' => array(
- 'label' => t('Add to cart form'),
- 'description' => t('Add to cart form'),
- 'weight' => 10,
- ),
- ),
- );
- }
- return $extra;
- }
- /**
- * Implements hook_field_delete_instance().
- *
- * Resets a content type's default image field setting when that field instance
- * is removed.
- */
- function uc_product_field_delete_instance($instance) {
- if ($instance['entity_type'] == 'node' && $instance['field_name'] == variable_get('uc_image_' . $instance['bundle'], NULL)) {
- variable_del('uc_image_' . $instance['bundle']);
- }
- }
- /**
- * Implements hook_uc_product_types().
- */
- function uc_product_uc_product_types() {
- return array_keys(uc_product_node_info());
- }
- /**
- * Implements hook_uc_store_status().
- *
- * Displays the status of the product image handlers.
- *
- * @see uc_product_image_defaults()
- */
- function uc_product_uc_store_status() {
- module_load_include('inc', 'content', 'includes/content.crud');
- // Check for filefields on products.
- if ($field = variable_get('uc_image_product', '')) {
- $instances = field_info_instances('node', 'product');
- $field_check = (bool) count($instances[$field]);
- }
- else {
- $field_check = FALSE;
- }
- if ($field_check) {
- $status = 'ok';
- $description = t('Product image support has been automatically configured by Ubercart.');
- }
- else {
- $status = 'warning';
- $description = t('<a href="!url">Click here</a> to automatically configure the following items for core image support:', array('!url' => url('admin/store/settings/products/defaults')));
- $items[] = t('The Image file field has not been created for products.');
- $description .= theme('item_list', array('items' => $items)) . t('(This action is not required and should not be taken if you do not need images or have implemented your own image support.)');
- }
- return array(array('status' => $status, 'title' => t('Images'), 'desc' => $description));
- }
- /**
- * Implements hook_uc_cart_display().
- */
- function uc_product_uc_cart_display($item) {
- $node = node_load($item->nid);
- $element = array();
- $element['nid'] = array('#type' => 'value', '#value' => $node->nid);
- $element['module'] = array('#type' => 'value', '#value' => 'uc_product');
- $element['remove'] = array('#type' => 'submit', '#value' => t('Remove'));
- if (node_access('view', $node) && $uri = entity_uri('node', $node)) {
- $element['title'] = array(
- '#markup' => l($item->title, $uri['path'], $uri['options']),
- );
- }
- else {
- $element['title'] = array('#markup' => $item->title);
- }
- $element['#total'] = $item->price * $item->qty;
- $element['#suffixes'] = array();
- $element['data'] = array('#type' => 'hidden', '#value' => serialize($item->data));
- $element['qty'] = array(
- '#type' => 'uc_quantity',
- '#title' => t('Quantity'),
- '#title_display' => 'invisible',
- '#default_value' => $item->qty,
- '#allow_zero' => TRUE,
- );
- $element['description'] = array('#markup' => '');
- if ($description = uc_product_get_description($item)) {
- $element['description']['#markup'] = $description;
- }
- return $element;
- }
- /**
- * Implements hook_uc_update_cart_item().
- */
- function uc_product_uc_update_cart_item($nid, $data = array(), $qty, $cid = NULL) {
- if (!$nid) return NULL;
- $cid = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
- $efq = new EntityFieldQuery();
- $result = $efq->entityCondition('entity_type', 'uc_cart_item')
- ->propertyCondition('cart_id', $cid)
- ->propertyCondition('nid', $nid)
- ->propertyCondition('data', serialize($data))
- ->execute();
- if (!empty($result['uc_cart_item'])) {
- $item_entity = entity_load_single('uc_cart_item', current(array_keys($result['uc_cart_item'])));
- if ($item_entity->qty != $qty) {
- $item_entity->qty = $qty;
- entity_save('uc_cart_item', $item_entity);
- }
- }
- }
- /**
- * Implements hook_uc_add_to_cart_data().
- */
- function uc_product_uc_add_to_cart_data($form_values) {
- if (isset($form_values['nid'])) {
- $node = node_load($form_values['nid']);
- return array(
- 'shippable' => isset($node->shippable) ? $node->shippable : variable_get('uc_product_shippable_' . $node->type, 1),
- 'type' => $node->type
- );
- }
- else {
- return array(
- 'shippable' => variable_get('uc_product_shippable_product', 1),
- 'type' => 'product',
- );
- }
- }
- /**
- * Implements hook_uc_product_class().
- */
- function uc_product_uc_product_class($pcid, $op) {
- switch ($op) {
- case 'insert':
- db_update('node_type')
- ->fields(array(
- 'base' => 'uc_product',
- 'custom' => 0,
- ))
- ->condition('type', $pcid)
- ->execute();
- $result = db_query("SELECT n.vid, n.nid FROM {node} n LEFT JOIN {uc_products} p ON n.vid = p.vid WHERE n.type = :pcid AND p.vid IS NULL", array(':pcid' => $pcid));
- foreach ($result as $node) {
- $node->weight_units = variable_get('uc_weight_unit', 'lb');
- $node->length_units = variable_get('uc_length_unit', 'in');
- $node->pkg_qty = 1;
- $node->default_qty = 1;
- $node->shippable = 1;
- uc_product_insert($node);
- }
- break;
- }
- }
- /**
- * Form builder for uc_catalog_buy_it_now_form().
- *
- * @see uc_product_forms()
- * @see uc_catalog_buy_it_now_form_validate()
- * @see uc_catalog_buy_it_now_form_submit()
- * @ingroup forms
- */
- function uc_catalog_buy_it_now_form($form, &$form_state, $node) {
- $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Add to cart'),
- '#id' => 'edit-submit-' . $node->nid,
- '#attributes' => array(
- 'class' => array('list-add-to-cart'),
- ),
- );
- uc_form_alter($form, $form_state, __FUNCTION__);
- return $form;
- }
- /**
- * Redirects to the product page if attributes need to be selected.
- *
- * @see uc_catalog_buy_it_now_form()
- * @see uc_catalog_buy_it_now_form_submit()
- */
- function uc_catalog_buy_it_now_form_validate($form, &$form_state) {
- if (module_exists('uc_attribute')) {
- $attributes = uc_product_get_attributes($form_state['values']['nid']);
- if (!empty($attributes)) {
- drupal_set_message(t('This product has options that need to be selected before purchase. Please select them in the form below.'), 'error');
- drupal_goto('node/' . $form_state['values']['nid']);
- }
- }
- }
- /**
- * Form submission handler for uc_catalog_buy_it_now_form().
- *
- * @see uc_catalog_buy_it_now_form()
- * @see uc_catalog_buy_it_now_form_validate()
- */
- function uc_catalog_buy_it_now_form_submit($form, &$form_state) {
- $form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], 1, module_invoke_all('uc_add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
- }
- /**
- * Form to add the $node product to the cart.
- *
- * @param $node
- * A product node.
- *
- * @see uc_product_forms()
- * @see uc_product_add_to_cart_form_submit()
- * @ingroup forms
- */
- function uc_product_add_to_cart_form($form, &$form_state, $node) {
- $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
- if ($node->default_qty > 0 && variable_get('uc_product_add_to_cart_qty', FALSE)) {
- $form['qty'] = array(
- '#type' => 'uc_quantity',
- '#title' => t('Quantity'),
- '#default_value' => $node->default_qty,
- );
- }
- else {
- $form['qty'] = array('#type' => 'hidden', '#value' => $node->default_qty ? $node->default_qty : 1);
- }
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Add to cart'),
- '#id' => 'edit-submit-' . $node->nid,
- '#attributes' => array(
- 'class' => array('node-add-to-cart'),
- ),
- );
- $form['node'] = array(
- '#type' => 'value',
- '#value' => isset($form_state['storage']['variant']) ? $form_state['storage']['variant'] : $node,
- );
- uc_form_alter($form, $form_state, __FUNCTION__);
- return $form;
- }
- /**
- * Form validation handler for uc_product_add_to_cart_form().
- */
- function uc_product_add_to_cart_form_validate($form, &$form_state) {
- $form_state['storage']['variant'] = uc_product_load_variant($form_state['values']['nid'], module_invoke_all('uc_add_to_cart_data', $form_state['values']));
- }
- /**
- * Form submission handler for uc_product_add_to_cart_form().
- *
- * @see uc_product_add_to_cart_form()
- */
- function uc_product_add_to_cart_form_submit($form, &$form_state) {
- $form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], $form_state['values']['qty'], module_invoke_all('uc_add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
- }
- /**
- * Returns an array of product node types.
- */
- function uc_product_types() {
- return module_invoke_all('uc_product_types');
- }
- /**
- * Returns an associative array of product node type names keyed by ID.
- */
- function uc_product_type_names() {
- $names = array();
- // Get all the node meta data.
- $node_info = module_invoke_all('node_info');
- // Loop through each product node type.
- foreach (uc_product_types() as $type) {
- $names[$type] = $node_info[$type]['name'];
- }
- return $names;
- }
- /**
- * Determines whether or not a given node or node type is a product.
- *
- * @param $node
- * Either a full node object/array, a node ID, or a node type.
- *
- * @return
- * TRUE or FALSE indicating whether or not a node type is a product node type.
- */
- function uc_product_is_product($node) {
- // Load the node object if we received an integer as an argument.
- if (is_numeric($node)) {
- $node = node_load($node);
- }
- // Determine the node type based on the data type of $node.
- if (is_object($node)) {
- $type = $node->type;
- }
- elseif (is_array($node)) {
- $type = $node['type'];
- }
- elseif (is_string($node)) {
- $type = $node;
- }
- // If no node type was found, go ahead and return FALSE.
- if (!$type) {
- return FALSE;
- }
- // Return TRUE or FALSE depending on whether or not the node type is in the
- // product types array.
- return in_array($type, uc_product_types());
- }
- /**
- * Determines whether or not a given form array is a product node form.
- *
- * @param $form
- * The form array to examine.
- *
- * @return
- * TRUE or FALSE indicating whether or not the form is a product node form.
- */
- function uc_product_is_product_form($form) {
- return !empty($form['#node_edit_form']) && uc_product_is_product($form['#node']);
- }
- /**
- * Gets all models of a product (node).
- *
- * Gathers any modules' models on this node, then add the node's SKU and the
- * optional 'Any' option.
- *
- * @param $nid
- * The node ID of the product.
- * @param $add_blank
- * String to use for the initial blank entry. If not desired, set to NULL
- * or FALSE. Make sure to localize the string first. Defaults to '- Any -'.
- *
- * @return
- * An associative array of model numbers. The key for '- Any -' is the empty
- * string.
- */
- function uc_product_get_models($nid, $add_blank = TRUE) {
- // Get any modules' SKUs on this node.
- $models = module_invoke_all('uc_product_models', $nid);
- // Add the base SKU of the node.
- $models[] = db_query('SELECT model FROM {uc_products} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
- // Now we map the SKUs to the keys, for form handling, etc.
- $models = drupal_map_assoc($models);
- // Sort the SKUs.
- asort($models);
- // And finally, we prepend 'Any' so it's the first option.
- if (!empty($add_blank) || $add_blank === '') {
- if ($add_blank === TRUE) {
- $add_blank = t('- Any -');
- }
- return array('' => $add_blank) + $models;
- }
- return $models;
- }
- /**
- * Gets the cost of a product node.
- *
- * @param $node_id
- * nid of the selected node
- *
- * @return
- * float - cost
- */
- function uc_product_get_cost($node_id) {
- $product = node_load($node_id);
- return $product->cost;
- }
- /**
- * Returns a product node's first attached image.
- *
- * @param $node_id
- * The node's id.
- * @param $style
- * The image style used to format the image. 'uc_product' by default.
- *
- * @return
- * A renderable array of the first product image, linked to the
- * product node, or an empty array if no image is available.
- */
- function uc_product_get_picture($nid, $style = 'uc_product') {
- $product = node_load($nid);
- if (!$product) {
- return array();
- }
- $field_name = variable_get('uc_image_' . $product->type, 'uc_product_image');
- $output = array();
- if ($field_name && isset($product->$field_name)) {
- $elements = field_view_field('node', $product, $field_name, array(
- 'label' => 'hidden',
- 'type' => 'image',
- 'settings' => array(
- 'image_link' => 'content',
- 'image_style' => $style,
- ),
- ));
- // Extract the part of the render array we need.
- $output = isset($elements[0]) ? $elements[0] : array();
- if (isset($elements['#access'])) {
- $output['#access'] = $elements['#access'];
- }
- }
- return $output;
- }
- /**
- * Finds the JavaScript image display module to use on product pages.
- */
- function uc_product_get_image_widget() {
- static $got_widget = FALSE, $image_widget = array();
- // Get the current image widget, if any.
- if (!$got_widget) {
- // Invoke hook to find widgets.
- $image_widgets = uc_store_get_image_widgets();
- // Find widget preference, if any.
- $widget_name = variable_get('uc_product_image_widget', NULL);
- if ($widget_name != NULL) {
- // Widget to use has been set in admin menu.
- $image_widget = $image_widgets[$widget_name];
- }
- else {
- // Widget to use has not been chosen, so default to first element of
- // array, if any.
- $keys = array_keys($image_widgets);
- if ($keys) {
- $image_widget = $image_widgets[$keys[0]];
- variable_set('uc_product_image_widget', $keys[0]);
- }
- }
- $got_widget = TRUE;
- }
- return $image_widget;
- }
- /**
- * Returns HTML for the product description.
- *
- * Modules adding information use hook_uc_product_description() and modules
- * wanting to alter the output before rendering can do so by implementing
- * hook_uc_product_description_alter(). By default, all descriptions supplied
- * by modules via hook_uc_product_description() are concatenated together.
- *
- * NOTE: hook_uc_product_description() supercedes the deprecated
- * hook_cart_item_description().
- *
- * @param $product
- * Product.
- *
- * @return
- * HTML rendered product description.
- */
- function uc_product_get_description($product) {
- // Run through implementations of hook_uc_product_description().
- $description = module_invoke_all('uc_product_description', $product);
- // Now allow alterations via hook_uc_product_description_alter().
- drupal_alter('uc_product_description', $description, $product);
- return drupal_render($description);
- }
- /**
- * Load a product class or all classes.
- */
- function uc_product_class_load($class_id = NULL, $reset = FALSE) {
- static $classes;
- if (!isset($classes) || $reset) {
- // Load classes from database.
- $classes = array();
- $result = db_query("SELECT * FROM {uc_product_classes}");
- while ($class = $result->fetchObject()) {
- $class->locked = FALSE;
- $classes[$class->pcid] = $class;
- }
- // Merge any module-defined classes.
- foreach (module_invoke_all('uc_product_default_classes') as $pcid => $class) {
- // The default class can be overridden by a module if needed,
- // but is not treated as a real class elsewhere.
- if ($pcid == 'product') {
- continue;
- }
- $class += array(
- 'pcid' => $pcid,
- 'name' => $pcid,
- 'description' => '',
- );
- if (isset($classes[$pcid])) {
- // Merge defaults with the existing database info.
- $classes[$pcid] = (object) array_merge($class, (array) $classes[$pcid]);
- }
- else {
- // Ensure the class info is saved in the database.
- drupal_write_record('uc_product_classes', $class);
- $classes[$pcid] = (object) $class;
- }
- // Module-defined classes cannot be deleted.
- $classes[$pcid]->locked = TRUE;
- }
- }
- return is_null($class_id) ? $classes : $classes[$class_id];
- }
- /**
- * Returns data for a product feature, given a feature ID and array key.
- *
- * @param $fid
- * The string ID of the product feature you want to get data from.
- * @param $key
- * The key in the product feature array you want: title, callback, delete,
- * settings.
- *
- * @return
- * The value of the key you specify.
- */
- function uc_product_feature_data($fid, $key) {
- static $features;
- if (empty($features)) {
- foreach (module_invoke_all('uc_product_feature') as $feature) {
- $features[$feature['id']] = $feature;
- }
- }
- return $features[$fid][$key];
- }
- /**
- * Returns a form array with some default hidden values and submit button.
- *
- * @param $form
- * The form array you wish to add the elements to.
- *
- * @return
- * The form array with elements added for the nid, pfid, submit button, and
- * cancel link.
- *
- * @ingroup forms
- */
- function uc_product_feature_form($form, &$form_state, $node, $feature) {
- $form['nid'] = array(
- '#type' => 'hidden',
- '#value' => $node->nid,
- );
- // Forms to add a feature are only given an empty array.
- if (!empty($feature)) {
- $form['pfid'] = array(
- '#type' => 'hidden',
- '#value' => $feature['pfid'],
- );
- }
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save feature'),
- );
- $form['actions']['cancel'] = array(
- '#markup' => l(t('Cancel'), 'node/' . $node->nid . '/edit/features'),
- );
- return $form;
- }
- /**
- * Saves a product feature to a product node.
- *
- * @param $data
- * An array consisting of the following keys:
- * - pfid: (optional) When editing an existing product feature, the numeric
- * ID of the feature.
- * - nid: The numeric ID of the product node.
- * - fid: The string ID of the feature type.
- * - description: The string describing the feature for the overview table.
- */
- function uc_product_feature_save(&$data) {
- if (empty($data['nid']) && arg(0) == 'node' && intval(arg(1)) > 0) {
- $data['nid'] = intval(arg(1));
- }
- if (empty($data['pfid'])) {
- if (arg(0) == 'node' && arg(3) == 'features' && intval(arg(5)) > 0) {
- $data['pfid'] = intval(arg(5));
- }
- }
- // First attempt to update an existing row.
- $result = drupal_write_record('uc_product_features', $data, !empty($data['pfid']) ? 'pfid' : array());
- // Otherwise insert this feature as a new row.
- if ($result == SAVED_NEW) {
- drupal_set_message(t('The product feature has been added.'));
- }
- elseif ($result == SAVED_UPDATED) {
- drupal_set_message(t('The product feature has been updated.'));
- }
- else {
- drupal_set_message(t('The product feature was unable to be saved.'));
- }
- return 'node/' . $data['nid'] . '/edit/features';
- }
- /**
- * Loads all product feature for a node.
- *
- * @param $nid
- * The product node ID.
- *
- * @return
- * The array of all product features object.
- */
- function uc_product_feature_load_multiple($nid) {
- $features = db_query("SELECT * FROM {uc_product_features} WHERE nid = :nid ORDER BY pfid ASC", array(':nid' => $nid))->fetchAllAssoc('pfid');
- return $features;
- }
- /**
- * Loads a product feature object.
- *
- * @todo: should return an object instead of array.
- *
- * @param $pfid
- * The product feature ID.
- * @param $fid
- * Optional. Specify a specific feature id.
- *
- * @return
- * The product feature array.
- */
- function uc_product_feature_load($pfid) {
- $feature = db_query("SELECT * FROM {uc_product_features} WHERE pfid = :pfid", array(':pfid' => $pfid))->fetchAssoc();
- return $feature;
- }
- /**
- * Deletes a product feature object.
- *
- * @param $pfid
- * The product feature ID.
- *
- * @return
- * The product feature object.
- */
- function uc_product_feature_delete($pfid) {
- $feature = uc_product_feature_load($pfid);
- // Call the delete function for this product feature if it exists.
- $func = uc_product_feature_data($feature['fid'], 'delete');
- if (function_exists($func)) {
- $func($feature);
- }
- db_delete('uc_product_features')
- ->condition('pfid', $pfid)
- ->execute();
- return SAVED_DELETED;
- }
- /**
- * Creates a file field with an image field widget, and attach it to products.
- *
- * This field is used by default on the product page, as well as on the cart
- * and catalog pages to represent the products they list. Instances are added
- * to new product classes, and other node types that claim product-ness should
- * call this function for themselves.
- *
- * @param $type
- * The content type to which the image field is to be attached. This may be a
- * a single type as a string, or an array of types. If NULL, all product
- * types get an instance of the field.
- */
- function uc_product_add_default_image_field($type = NULL) {
- $field = field_info_field('uc_product_image');
- // Set up field if it doesn't exist.
- if (!$field) {
- $field = array(
- 'field_name' => 'uc_product_image',
- 'type' => 'image',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- );
- field_create_field($field);
- // Initialize this because field_info_field() would have set it.
- $field['bundles'] = array();
- }
- $label = t('Image');
- $new_instance = array(
- 'entity_type' => 'node',
- 'label' => $label,
- 'weight' => -2,
- 'widget' => array(
- 'type' => 'image_image',
- ),
- 'display' => array(
- 'full' => array(
- 'label' => 'hidden',
- 'type' => 'uc_product_image',
- ),
- 'teaser' => array(
- 'label' => 'hidden',
- 'type' => 'uc_product_image',
- ),
- ),
- );
- if ($type) {
- // Accept single or multiple types as input.
- $types = (array) $type;
- }
- else {
- $types = uc_product_types();
- }
- foreach ($types as $type) {
- $new_instance['bundle'] = $type;
- $field_name = variable_get('uc_image_' . $type, '');
- if (empty($field_name)) {
- $field_name = 'uc_product_image';
- }
- $new_instance['field_name'] = $field_name;
- $instance = field_info_instance('node', $field_name, $type);
- // Only add the instance if it doesn't exist. Don't overwrite any changes.
- if (!$instance) {
- field_create_instance($new_instance);
- variable_set('uc_image_' . $type, $field_name);
- }
- }
- }
- /**
- * Implements hook_image_default_styles().
- */
- function uc_product_image_default_styles() {
- $styles = array();
- $styles['uc_product'] = array(
- 'effects' => array(
- array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => '250',
- 'height' => '250',
- 'upscale' => 0,
- ),
- 'weight' => '0',
- ),
- ),
- );
- $styles['uc_product_full'] = array(
- 'effects' => array(),
- );
- $styles['uc_product_list'] = array(
- 'effects' => array(
- array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => '100',
- 'height' => '100',
- 'upscale' => 0,
- ),
- 'weight' => '0',
- ),
- ),
- );
- $styles['uc_thumbnail'] = array(
- 'effects' => array(
- array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => '35',
- 'height' => '35',
- 'upscale' => 0,
- ),
- 'weight' => '0',
- ),
- ),
- );
- return $styles;
- }
- /**
- * Implements hook_views_api().
- */
- function uc_product_views_api() {
- return array(
- 'api' => '2.0',
- 'path' => drupal_get_path('module', 'uc_product') . '/views',
- );
- }
- /**
- * Implements hook_features_api().
- */
- function uc_product_features_api() {
- return array(
- 'uc_product_classes' => array(
- 'name' => t('Ubercart product classes'),
- 'feature_source' => TRUE,
- 'default_hook' => 'uc_product_default_classes',
- 'file' => drupal_get_path('module', 'uc_product') . '/uc_product.features.inc',
- ),
- );
- }
- /**
- * Implements hook_action_info().
- */
- function uc_product_action_info() {
- return array(
- 'uc_product_action_add_to_cart' => array(
- 'label' => t('Add to cart'),
- 'type' => 'node',
- 'configurable' => FALSE,
- 'triggers' => array('any'),
- ),
- );
- }
- /**
- * Action implementation: adds a product to the cart.
- */
- function uc_product_action_add_to_cart($product, $context = array()) {
- uc_cart_add_item($product->nid, 1, NULL, NULL, TRUE, FALSE, TRUE);
- }
|