178c178,280
< }
\ No newline at end of file
---
> }
> 
> /** Add region visibility options on nodes **/
> function region_visibility_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
>   switch ($op) {
>     /** Update page records when updating an existing node **/
>     case 'update':
>     /** Create page records on node creation **/
>     case 'insert':
>       global $theme_key;
>       $current_theme_records = _region_visibility_current_theme_records();
>       foreach ($node->region_visibility_onnode as $key => $value) {
>         // Save new settings only if region checkbox is checked
>         if ($value) {
>           // Cfr. visibility_api.module
>           $record_name = $theme_key .':'. $key;
>           $record = $current_theme_records[$record_name];
>           // $record['package_key'] = $key; // is it necessary?
>           $record['roles'] = array_keys(array_filter($record['roles']));
>           // Exclude PHP from this logic
>           if ($record['visibility'] < 2) {
>             $path = 'node/'. $node->nid;
>             $pages = explode("\r\n", $record['pages']);
>             // Avoid duplicates
>             if (!in_array($path, $pages)) {
>               $pages[] = $path;
>             }
>             $record['pages'] = implode("\r\n", $pages);
>             // Save
>             $record['pages'] = trim($record['pages']);
>             visibility_api_record_save($record);
>           }
>         }
>       }
>       return;
>     break;
>     case 'prepare':
>     break;
>   }
> }
> 
> function region_visibility_form_alter(&$form, $form_state, $form_id) {
>   if (_region_visibility_onnode_is_enabled($form, $form_id)) {
>     global $theme_key;
>     $values = _region_visibility_onnode_values($form, $form_id);
>     /** Do not display 0 (value not set): this value is saved on database but not on view **/
>     $form['region_visibility'] = array(
>       '#type' => 'fieldset',
>       '#title' => t('Region visibility'),
>       '#collapsible' => TRUE,
>       '#collapsed' => empty($values),
>       '#access' => user_access('administer region visibility'),
>       '#weight' => 20,
>     );
>     $form['region_visibility']['region_visibility_onnode'] = array(
>       '#type' => 'checkboxes',
>       '#title' => t('Suppress / enable regions'),
>       '#default_value' => $values,
>       '#options' => system_region_list($theme_key),
>       '#description' => t('Specify regions to hide / show.'),
>     );
>   }
> }
> 
> /** Display on node/add or node//edit **/
> function _region_visibility_onnode_is_enabled(&$form, $form_id) {
>   if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
>     return TRUE;
>   }
> }
> 
> /** What regions are disabled for this node? **/
> function _region_visibility_onnode_values(&$form, $form_id) {
>   global $theme_key;
>   $options = array();
>   foreach (_region_visibility_current_theme_records() as $key => $record) {
>     list($theme, $region) = explode(':', $key);
>     $path = 'node/'. $form['#node']->nid;
>     $pages = explode("\r\n", $record['pages']);
>     if (in_array($path, $pages)) {
>       $options[$region] = $region;
>     }
>     else {
>       $options[$region] = 0;
>     }
>   }
>   return $options;
> }
> 
> /** Get current theme Visibility API records **/
> function _region_visibility_current_theme_records() {
>   global $theme_key;
>   $records = visibility_api_record_load_all('region_visibility');
>   $current_theme_records = array();
>   foreach ($records as $key => $record) {
>     /** Read only current theme **/
>     if (strpos($key, $theme_key .':') === 0) {
>       $current_theme_records[$key] = $record;
>     }
>   }
>   return $current_theme_records;
> }
> 
