You are on page 1of 6

<?

php
//todo: campi: immagine (verificare se si pu caricare da link), categories
/**
* Implements hook_install().
*/
function bibliotita_install() {
//Create content type for the book
if (!in_array('bibliotita_book', node_type_get_names())){
$type = array(
'type' => 'bibliotita_book',
'name' => t('Book'),
'description' => t('A book'),
'base' => 'node_content',
'custom' => 1,
'modified' => 1,
'locked' => 0,
'has_title' => false,
);
$type = node_type_set_defaults($type);
node_type_save($type);
}
//Create field isbn
if(!field_info_field('field_isbn')){
$field_isbn = array(
'field_name' => 'field_isbn',
'type' => 'text',
);
field_create_field($field_isbn);
}
//Create field title
if(!field_info_field('field_title')){
$field_title = array(
'field_name' => 'field_title',
'type' => 'text'
);
field_create_field($field_title);
}
//Create field author
if(!field_info_field('field_author')){
$field_author = array(
'field_name' => 'field_author',
'cardinality' => -1,
'type' => 'text'
);
field_create_field($field_author);
}
//Create field description
if(!field_info_field('field_description')){
$field_description = array(
'field_name' => 'field_description',
'type' => 'text'
);

field_create_field($field_description);
}
//Create field publisher
if(!field_info_field('field_publisher')){
$field_publisher = array(
'field_name' => 'field_publisher',
'type' => 'text'
);
field_create_field($field_publisher);
}
//Create field published date
if(!field_info_field('field_published_date')){
$field_published_date = array(
'field_name' => 'field_published_date',
'type' => 'text'
);
field_create_field($field_published_date);
}

$vocabularies = taxonomy_vocabulary_get_names();
//dpm($vocabularies);
$pos = FALSE;
foreach($vocabularies as $vocab){
//dpm('entro foreach:' . $vocab->name);
if ($vocab->machine_name == 'book_status'){
$pos = $vocab->vid;
}
}
/*
$pos = array_search('book_status', $vocabularies);
*/
//dpm($pos);
if ($pos !== FALSE) {
$vid = $pos;
//dpm('vocabolario gi esistente');
}else{
//Create Vocabulary status
$myVocabulary = array(
'name' => t('Status'),
'machine_name' => 'book_status',
'description' => t('The possible status of a book'),
'hierarchy' => 1,
'module' => 'bibliotita',
'weight' => 1
);
// argument must be an object
$myVocabulary = (object) $myVocabulary;
taxonomy_vocabulary_save($myVocabulary);
// horray, we have a vid now
$vid = $myVocabulary->vid;
$term = new stdClass();
$term->vid = $vid;
$term->name = t('Disponibile');
taxonomy_term_save($term);

$term = new stdClass();


$term->vid = $vid;
$term->name = t('Prenotato');
taxonomy_term_save($term);
$term = new stdClass();
$term->vid = $vid;
$term->name = t('In prestito');
taxonomy_term_save($term);
}
//Create field Status
if(!field_info_field('field_status')){
$field_status = array(
'field_name' => 'field_status',
'type' => 'taxonomy_term_reference',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'book_status',
'parent' => 0,
),
),
),
);
field_create_field($field_status);
}
//Instance field isbn
$instance_isbn = array(
'field_name' => 'field_isbn',
'entity_type' => 'node',
'label' => 'ISBN',
'bundle' => 'bibliotita_book',
'required' => TRUE,
'widget' => array('type' => 'textfield',),
);
field_create_instance($instance_isbn);
//Instance field title
$instance_title = array(
'field_name' => 'field_title',
'entity_type' => 'node',
'label' => t('Title'),
'bundle' => 'bibliotita_book',
'required' => FALSE,
'widget' => array('type' => 'textfield',),
);
field_create_instance($instance_title);
//Instance field author
$instance_author = array(
'field_name' => 'field_author',
'entity_type' => 'node',
'label' => t('Author'),
'bundle' => 'bibliotita_book',
'required' => FALSE,
'widget' => array('type' => 'textfield',),
);

field_create_instance($instance_author);
//Instance field description
$instance_description = array(
'field_name' => 'field_description',
'entity_type' => 'node',
'label' => t('Description'),
'bundle' => 'bibliotita_book',
'required' => FALSE,
'widget' => array('type' => 'textfield',),
);
field_create_instance($instance_description);
//Instance field publisher
$instance_publisher = array(
'field_name' => 'field_publisher',
'entity_type' => 'node',
'label' => t('Publisher'),
'bundle' => 'bibliotita_book',
'required' => FALSE,
'widget' => array('type' => 'textfield',),
);
field_create_instance($instance_publisher);
//Instance field published date
$instance_published_date = array(
'field_name' => 'field_published_date',
'entity_type' => 'node',
'label' => t('Published Date'),
'bundle' => 'bibliotita_book',
'required' => FALSE,
'widget' => array('type' => 'textfield',),
);
field_create_instance($instance_published_date);
//Instance field status
$instance_status = array(
'field_name' => 'field_status',
'entity_type' => 'node',
'label' => t('Status'),
'bundle' => 'bibliotita_book',
'required' => FALSE,
'widget' => array('type' => 'options_select',),
);
field_create_instance($instance_status);
//Create content type for book loan
if (!in_array('bibliotita_book_loan', node_type_get_names())){
$type = array(
'type' => 'bibliotita_book_loan',
'name' => t('Book Loan'),
'description' => t('A book loan'),
'base' => 'node_content',
'custom' => 1,
'modified' => 1,
'locked' => 0,
'has_title' => false,
);
$type = node_type_set_defaults($type);

node_type_save($type);
}

//Create field Borrower


if(!field_info_field('field_borrower')){
$field_borrower = array(
'field_name' => 'field_borrower',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'user',
),
);
field_create_field($field_borrower);
}
//Instance field borrower
$instance_borrower = array(
'field_name' => 'field_borrower',
'entity_type' => 'node',
'label' => t('Borrower'),
'bundle' => 'bibliotita_book_loan',
'required' => FALSE,
'widget' => array('type' => 'options_select',),
);
field_create_instance($instance_borrower);
//Create field Book_loaned
if(!field_info_field('field_book_loaned')){
$field_book_loaned = array(
'field_name' => 'field_book_loaned',
'type' => 'entityreference',
'cardinality' => 1,
'settings' => array(
'target_type' => 'node',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array('bibliotita_book')
),
),
);
field_create_field($field_book_loaned);
}
//Instance field book loaned
$instance_book_loaned = array(
'field_name' => 'field_book_loaned',
'entity_type' => 'node',
'label' => t('Book loaned'),
'bundle' => 'bibliotita_book_loan',
'required' => FALSE,
'widget' => array('type' => 'options_select',),
);
field_create_instance($instance_book_loaned);

//ultima parentesi graffa


}

/**
* Implements hook_uninstall().
*/
function bibliotita_uninstall() {
db_delete('node')
-> condition('type', 'bibliotita_book', '=')
-> execute();
node_type_delete('bibliotita_book');
db_delete('node')
-> condition('type', 'bibliotita_book_loan', '=')
-> execute();
node_type_delete('bibliotita_book_loan');
}

You might also like