You are here

function uc_product_image_defaults in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_product/uc_product.admin.inc \uc_product_image_defaults()
  2. 7.3 uc_product/uc_product.admin.inc \uc_product_image_defaults()

Set up imagefield and imagecache for products.

Parameters

$checks A bitmap describing the state of the image modules. Four: checks are made to fill this bitmap: an imagefield exists, an imagefield is attached to product, the imagecache presets exist, and each preset has at least one action.

See also

uc_product_store_status

1 string reference to 'uc_product_image_defaults'
uc_product_menu in uc_product/uc_product.module
Implementation of hook_menu().

File

uc_product/uc_product.module, line 1580
The product module for Ubercart.

Code

function uc_product_image_defaults($checks) {
  $field_check = $checks & 8;
  $product_field_check = $checks & 4;
  $preset_check = $checks & 2;
  $action_check = $checks & 1;
  $presets = array(
    'product',
    'product_list',
    'uc_thumbnail',
  );
  if (module_exists('uc_catalog')) {
    $presets[] = 'uc_category';
  }
  if (module_exists('uc_cart')) {
    $presets[] = 'cart';
  }
  if (module_exists('uc_manufacturer')) {
    $presets[] = 'manufacturer';
  }
  $presets = drupal_map_assoc($presets);
  if (!$field_check) {
    db_query("INSERT INTO {node_field} (field_name, type, global_settings, required, multiple, db_storage) VALUES ('field_image_cache', 'image', '%s', 0, 1, 0)", 'a:0:{}');
    content_clear_type_cache();
  }
  if (!$product_field_check) {
    $widget_settings = array(
      'max_resolution' => '0',
      'image_path' => '',
      'custom_alt' => 1,
      'custom_title' => 1,
      'teaser_preset' => null,
      'body_preset' => null,
    );
    $display_settings = array(
      'label' => array(
        'format' => 'hidden',
      ),
      'teaser' => array(
        'format' => 'hidden',
      ),
      'full' => array(
        'format' => 'hidden',
      ),
    );
    foreach (module_invoke_all('product_types') as $type) {
      db_query("INSERT INTO {node_field_instance} VALUES ('field_image_cache', '%s', -2, 'Image', 'image', '%s', '%s', '')", $type, serialize($widget_settings), serialize($display_settings));
    }
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        db_query("CREATE TABLE IF NOT EXISTS {content_field_image_cache} (\n          `vid` int(10) unsigned NOT NULL default '0',\n          `delta` int(10) unsigned NOT NULL default '0',\n          `nid` int(10) unsigned NOT NULL default '0',\n          `field_image_cache_fid` int(11) NOT NULL default '0',\n          `field_image_cache_title` varchar(255) NOT NULL default '',\n          `field_image_cache_alt` varchar(255) NOT NULL default '',\n          PRIMARY KEY  (`vid`,`delta`)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
        break;
      case 'pgsql':
        db_query("CREATE TABLE {content_field_image_cache} (\n          vid int_unsigned NOT NULL default '0',\n          delta int_unsigned NOT NULL default '0',\n          nid int_unsigned NOT NULL default '0',\n          field_image_cache_fid integer NOT NULL default '0',\n          field_image_cache_title varchar(255) NOT NULL default '',\n          field_image_cache_alt varchar(255) NOT NULL,\n          PRIMARY KEY (vid,delta)\n        );");
        break;
    }
    content_clear_type_cache();
  }
  if (!$preset_check) {
    $result = db_query("SELECT * FROM {imagecache_preset} WHERE presetname IN ('" . implode("','", $presets) . "')");
    $preset_keys = array();
    foreach ($presets as $preset) {
      $preset_keys[$preset] = 0;
    }
    $presets = $preset_keys;
    while ($preset = db_fetch_array($result)) {
      $presets[$preset['presetname']] = $preset['presetid'];
    }

    //drupal_set_message('<pre>'. print_r($presets, true) .'</pre>');
    foreach ($presets as $name => $id) {
      if ($id == 0) {
        $id = db_next_id('{imagecache_preset}_presetid');
        db_query("INSERT INTO {imagecache_preset} (presetid, presetname) VALUES (%d, '%s')", $id, $name);
      }
    }
  }
  if (!$action_check) {
    $result = db_query("SELECT ia.actionid, ip.presetid, ip.presetname FROM {imagecache_preset} AS ip LEFT JOIN {imagecache_action} AS ia ON ip.presetid = ia.presetid WHERE ip.presetname IN ('" . implode("','", array_keys($presets)) . "')");
    $presets = array();
    while ($preset = db_fetch_array($result)) {
      if (is_null($preset['actionid'])) {
        switch ($preset['presetname']) {
          case 'product':
            db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, 0, '%s')", db_next_id('{imagecache_action}_actionid'), $preset['presetid'], 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:3:"100";s:6:"height";s:3:"100";}');
            break;
          case 'product_list':
            db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, 0, '%s')", db_next_id('{imagecache_action}_actionid'), $preset['presetid'], 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:3:"100";s:6:"height";s:3:"100";}');
            break;
          case 'uc_thumbnail':
            db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, 0, '%s')", db_next_id('{imagecache_action}_actionid'), $preset['presetid'], 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:2:"35";s:6:"height";s:2:"35";}');
            break;
          case 'uc_category':
            db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, 0, '%s')", db_next_id('{imagecache_action}_actionid'), $preset['presetid'], 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:2:"96";s:6:"height";s:2:"96";}');
            break;
          case 'cart':
            db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, 0, '%s')", db_next_id('{imagecache_action}_actionid'), $preset['presetid'], 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:2:"50";s:6:"height";s:2:"50";}');
            break;
          case 'manufacturer':
            db_query("INSERT INTO {imagecache_action} (actionid, presetid, weight, data) VALUES (%d, %d, 0, '%s')", db_next_id('{imagecache_action}_actionid'), $preset['presetid'], 'a:4:{s:8:"function";s:5:"scale";s:3:"fit";s:6:"inside";s:5:"width";s:2:"80";s:6:"height";s:2:"80";}');
            break;
        }
      }
    }
  }
  cache_clear_all('imagecache:presets', 'cache');
  drupal_goto('admin/store');
}