You are here

function uc_product_enable in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_product/uc_product.module \uc_product_enable()
  2. 7.3 uc_product/uc_product.install \uc_product_enable()

Implementation of hook_enable().

Set up default imagefield and imagecache settings.

File

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

Code

function uc_product_enable() {
  $node_types = node_get_types('types');
  $product_classes = array(
    'product',
  );
  $result = db_query("SELECT pcid, name, description FROM {uc_product_classes}");
  while ($product_class = db_fetch_object($result)) {
    $product_classes[] = $product_class->pcid;
  }
  foreach ($node_types as $type => $info) {
    if ($info->module == 'node' && in_array($type, $product_classes)) {
      $info->module = 'uc_product';
      $info->custom = 0;
      node_type_save($info);
    }
  }
  if (module_exists('imagefield')) {
    $result = db_query("SELECT field_name FROM {node_field} WHERE field_name = 'field_image_cache' AND type = 'image'");
    if (!db_num_rows($result)) {
      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:{}');
    }
    $image_path = file_create_path();
    $widget_settings = array(
      'max_resolution' => '0',
      'image_path' => $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) {
      $result = db_query("SELECT * FROM {node_field_instance} WHERE field_name = 'field_image_cache' and type_name = '%s'", $type);
      if (!db_num_rows($result)) {
        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':
          $result = db_query("SELECT relname FROM pg_class WHERE relname = '{content_field_image_cache}'");
          if (!db_num_rows($result)) {
            db_query('CREATE TABLE {content_field_image_cache} (
              "vid" int_unsigned NOT NULL default \'0\',
              "delta" int_unsigned NOT NULL default \'0\',
              "nid" int_unsigned NOT NULL default \'0\',
              "field_image_cache_fid" integer NOT NULL default \'0\',
              "field_image_cache_title" varchar(255) NOT NULL,
              "field_image_cache_alt" varchar(255) NOT NULL,
              PRIMARY KEY  ("vid","delta")
            );');
          }
          break;
      }
    }
    content_clear_type_cache();
  }
  if (module_exists('imagecache')) {
    $presets = array(
      'product' => 0,
      'product_list' => 0,
      'uc_thumbnail' => 0,
    );
    $result = db_query("SELECT * FROM {imagecache_preset} WHERE presetname IN ('" . implode("','", array_keys($presets)) . "')");
    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);
      }
    }
    $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;
        }
      }
    }
    cache_clear_all('imagecache:presets', 'cache');
  }
}