You are here

classified.install in Classified Ads 7.3

Same filename and directory in other branches
  1. 6.3 classified.install

Install file for the Classified Ads module.

@copyright (c) 2010-2011 Ouest Systemes Informatiques (OSInet)

@license General Public License version 2 or later

New code implementing a feature set derived from the ed_classified module.

---- Information about ed_classified ----

Michael Curry, Exodus Development, Inc. exodusdev@gmail.com for more information, please visit: http://exodusdev.com/drupal/modules/ed_classified.module

Copyright (c) 2006, 2007 Exodus Development, Inc. All Rights Reserved.

Licensed under the terms of the GNU Public License (GPL) version 2. Please see LICENSE.txt for license terms. Possession and use of this code signifies acceptance of license terms.

---- /ed-classified ----

File

classified.install
View source
<?php

/**
 * @file
 * Install file for the Classified Ads module.
 *
 * @copyright (c) 2010-2011 Ouest Systemes Informatiques (OSInet)
 *
 * @license General Public License version 2 or later
 *
 * New code implementing a feature set derived from the ed_classified module.
 *
 * ---- Information about ed_classified ----
 *
 * Michael Curry, Exodus Development, Inc.
 * exodusdev@gmail.com
 * for more information, please visit:
 *   http://exodusdev.com/drupal/modules/ed_classified.module
 *
 * Copyright (c) 2006, 2007 Exodus Development, Inc.  All Rights Reserved.
 *
 * Licensed under the terms of the GNU Public License (GPL) version 2.  Please
 * see LICENSE.txt for license terms.  Possession and use of this code signifies
 * acceptance of license terms.
 *
 * ---- /ed-classified ----
 */

/**
 * Implements hook_install().
 */
function classified_install() {
  $t = get_t();

  // Define default values.
  foreach (_classified_get_vars() as $name => $default) {
    variable_set($name, $default);
  }

  // Create module vocabulary.
  $vocabulary = (object) array(
    'module' => 'classified',
    'machine_name' => 'classified_categories',
    'name' => $t('Classified Ads categories'),
    'description' => $t('This vocabulary is the one used by the Classified Ads module to classify ads. Do not remove it: it is managed by the Classified Ads module, not by the taxonomy module.'),
    'help' => $t('This term will appear on Classified Ads pages'),
    'multiple' => 0,
    'required' => 1,
    'hierarchy' => 1,
    'relations' => 0,
    'weight' => -10,
    'nodes' => array(
      'classified' => 1,
    ),
  );
  taxonomy_vocabulary_save($vocabulary);
  $vid = $vocabulary->vid;
  variable_set('classified-vid', $vid);
  drupal_set_message($t('New vocabulary @vid created for Classified Ads', array(
    '@vid' => $vid,
  )));

  // Create terms in module vocabulary.
  $terms = array(
    $t('For sale'),
    $t('Wanted'),
  );
  foreach ($terms as $name) {
    $term = (object) array(
      'name' => $name,
      'vid' => $vid,
      'description' => '',
      'format' => filter_default_format(),
    );
    taxonomy_term_save($term);
  }
  drupal_set_message($t('@count default ad classifications inserted', array(
    '@count' => count($terms),
  )));

  // Create fields: a body and a category.
  // Start by getting up-to-date content type information.
  node_types_rebuild();
  $types = node_type_get_types();
  node_add_body_field($types['classified'], $t('Ad Body'));
  $field_name = _classified_get('field-category');
  $field = array(
    'field_name' => $field_name,
    'type' => 'taxonomy_term_reference',
    'cardinality' => 1,
    'translatable' => FALSE,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name,
          'parent' => '0',
        ),
      ),
    ),
  );
  field_create_field($field);
  $instance = array(
    'entity_type' => 'node',
    'field_name' => $field_name,
    'bundle' => 'classified',
    'label' => 'Ad Category',
    'description' => 'The category of your Classified Ad',
    'required' => TRUE,
    'widget' => array(
      'type' => 'options_select',
      'weight' => 0,
    ),
  );
  field_create_instance($instance);

  // Adding the flush below causes Simpletest to fail outside the tests.
  // drupal_flush_all_caches();
}

/**
 * Implements hook_requirements().
 */
function classified_requirements($phase) {
  $requirements = array();
  $t = get_t();
  if (module_exists('ed_classified')) {
    $requirements['ed_classified'] = array(
      'title' => $t('Classified is incompatible with ed_classified'),
      'value' => $t('Module conflict'),
      'description' => $t('The "Classified" module is a fork/rewrite of ed_classified and is incompatible with it.'),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  if (!module_exists('statistics')) {
    $requirements['statistics'] = array(
      'title' => $t('Classified is enhanced by Statistics'),
      'value' => NULL,
      'description' => $t('The "Classified" module can only provide its "popular" block if statistics.module is enabled'),
      'severity' => REQUIREMENT_INFO,
    );
  }
  elseif (variable_get('statistics_count_content_views', 0) == 0) {
    $requirements['statistics'] = array(
      'title' => $t('Classified "popular ads" block needs content views counted'),
      'value' => NULL,
      'description' => $t('The "Popular Ads" block can only provide valida data if content views are counted. <a href="!link">Enable "Count content views"</a>.', array(
        '!link' => url('admin/config/system/statistics'),
      )),
      'severity' => REQUIREMENT_INFO,
    );
  }
  if ($phase == 'runtime') {
    $vid = _classified_get('vid');
    $vocabulary = taxonomy_vocabulary_load($vid);
    switch ($vocabulary->hierarchy) {
      case 0:
        $requirements['hierarchy'] = array(
          'title' => $t('Classified Ads vocabulary'),
          'value' => $t('No hierarchy'),
          'description' => $t('All ad categories are at the same level. In most cases, you should set up a hierarchy of terms to classify ads.'),
          'severity' => REQUIREMENT_WARNING,
        );
        break;
      case 1:

        // All OK: nothing to add.
        $requirements['hierarchy'] = array(
          'title' => $t('Classified Ads vocabulary'),
          'value' => $t('The terms in the Classified Ads vocabulary are set up in a tree hierarchy.'),
          'severity' => REQUIREMENT_OK,
        );
        break;
      default:
        $requirements['hierarchy'] = array(
          'title' => $t('Classified Ads vocabulary'),
          'value' => $t('Maximum parents per term: @count', array(
            '@count' => $vocabulary->hierarchy,
          )),
          'description' => $t('The Classified Ads module does not support non-tree term hierarchies. Make sure each term has at most one parent.'),
          'severity' => REQUIREMENT_ERROR,
        );
        break;
    }
  }
  return $requirements;
}

/**
 * Implements hook_schema().
 *
 * Non-translation of schema data became standard with Drupal 6.9.
 * See: @link http://drupal.org/node/332123 @endlink.
 */
function classified_schema() {
  $schema['classified_node'] = array(
    'description' => 'Stores the extra information for the classified node-related type.',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Node nid of related node.',
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Node vid (version id).',
      ),
      'expires' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Expiration timestamp',
      ),
      'notify' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Timestamp of latest notification',
      ),
    ),
    'primary key' => array(
      'nid',
      'vid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 *
 * - remove vocabulary and its terms,
 * - remove fields,
 * - remove nodes,
 * - remove module variables.
 *
 * Permissions/role mapping removal is handled by core in D7:
 * @link http://drupal.org/node/306027 @endlink
 */
function classified_uninstall() {
  drupal_load('module', 'classified');
  $vid = _classified_get('vid');
  $t = get_t();
  if ($vid) {

    // In some broken situations, the vocabulary may be already deleted, so we
    // avoid choking on its failed deletion.
    $vocabulary = taxonomy_vocabulary_load($vid);
    if ($vocabulary !== FALSE) {
      taxonomy_vocabulary_delete($vid);
      drupal_set_message($t('Removed Classified Ads vocabulary and terms.'));
    }
    else {
      drupal_set_message($t('Classified Ads vocabulary was already removed.'));
    }
  }
  else {
    drupal_set_message($t('Could not find a Classified Ads vocabulary to remove.'), 'warning');
  }

  // Delete our fields.
  field_delete_instance(array(
    'field_name' => 'body',
    'entity_type' => 'node',
    'bundle' => 'classified',
  ), TRUE);
  field_delete_instance(array(
    'field_name' => _classified_get('field-category'),
    'entity_type' => 'node',
    'bundle' => 'classified',
  ), TRUE);
  drupal_set_message($t('Removed Classified Ads fields.'));

  // Delete our nodes.
  $q = new EntityFieldQuery();
  $light_nodes = $q
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'classified')
    ->execute();
  if (isset($light_nodes['node']) && !empty($light_nodes['node'])) {
    $nids = array_keys($light_nodes['node']);
    node_delete_multiple($nids);
    drupal_set_message($t('Removed Classified Ads nodes.'));
  }
  else {
    drupal_set_message($t('Could not find nodes to remove.'));
  }

  // Delete our variables.
  foreach (array_keys(_classified_get_vars()) as $var_name) {
    variable_del($var_name);
  }
  drupal_set_message($t('Removed Classified Ads variables.'));
  cache_clear_all('classified:overview', 'cache');
  field_cron();
  drupal_flush_all_caches();
}

/**
 * Implements hook_update_N().
 *
 * - define an initial schema version.
 */
function classified_update_7300() {

  // Nothing to do: just let core register us as a 7.3 version.
}

/**
 * Implements hook_update_last_removed().
 */
function classified_update_last_removed() {
  return 6100;
}