You are here

function uc_catalog_taxonomy in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_catalog/uc_catalog.module \uc_catalog_taxonomy()

Implementation of hook_taxonomy().

File

uc_catalog/uc_catalog.module, line 195
Übercart Catalog module.

Code

function uc_catalog_taxonomy($op, $type, $object = null) {
  switch ($type) {
    case 'vocabulary':
      if ($object['vid'] == variable_get('uc_catalog_vid', 0)) {
        switch ($op) {
          case 'delete':
            variable_del('uc_catalog_vid');
            variable_del('uc_catalog_name');
            variable_del('uc_catalog_description');
            break;
          case 'update':
            variable_set('uc_catalog_name', check_plain($object['name']));
            variable_set('uc_catalog_description', filter_xss_admin($object['description']));
            break;
        }
      }
      break;
    case 'term':
      switch ($op) {
        case 'insert':
          if (module_exists('pathauto')) {
            $category = (object) $object;
            if ($category->name) {
              $count = _uc_catalog_pathauto_alias($category, $op);
            }
          }
          $field_name = 'image';
          if ($file = file_check_upload($field_name)) {
            $file->filepath = str_replace('\\', '/', $file->filepath);
            $image_path = file_create_path();
            $file = file_save_upload($field_name, $image_path . '/' . $file->filename);
            if ($file) {
              if (image_get_info($file->filepath)) {
                db_query("INSERT INTO {uc_catalog_images} (fid, tid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", db_next_id('{files}_fid'), $object['tid'], $file->filename, $file->filepath, $file->filemime, $file->filesize);
              }
              else {
                form_set_error($field_name, t('Uploaded file is not a valid image'));
                file_delete($file->filepath);
              }
            }
          }
          break;
        case 'update':
          if (module_exists('pathauto')) {
            $category = (object) $object;
            if ($category->name) {
              $count = _uc_catalog_pathauto_alias($category, $op);
            }
          }
          $field_name = 'image';
          if ($object['remove']) {
            db_query("DELETE FROM {uc_catalog_images} WHERE tid = %d", $object['tid']);
          }
          else {
            if ($file = file_check_upload($field_name)) {
              $file->filepath = str_replace('\\', '/', $file->filepath);
              $file = file_save_upload($field_name, file_create_path() . '/' . $file->filename);
              if ($file) {
                if (image_get_info($file->filepath)) {
                  db_query("DELETE FROM {uc_catalog_images} WHERE tid = %d", $object['tid']);
                  db_query("INSERT INTO {uc_catalog_images} (fid, tid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", db_next_id('{files}_fid'), $object['tid'], $file->filename, $file->filepath, $file->filemime, $file->filesize);
                }
                else {
                  form_set_error($field_name, t('Uploaded file is not a valid image'));
                  file_delete($file->filepath);
                }
              }
            }
          }
          break;
        case 'delete':
          $category = (object) $object;
          if ($file = db_fetch_object(db_query("SELECT fid, filepath FROM {uc_catalog_images} WHERE tid = %d", $category->tid))) {
            file_delete($file->filepath);
            db_query("DELETE FROM {uc_catalog_images} WHERE fid = %d", $file->fid);
          }
          path_set_alias(uc_catalog_path($category));
          break;
      }
      break;
  }
}