178c178,297
< }
\ 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
>         // Cfr. visibility_api.module
>         $record_name = $theme_key .':'. $key;
>         $record = $current_theme_records[$record_name];
>         $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 when insert
>           if (!in_array($path, $pages) && $value) {
>             $pages[] = $path;
>           }
>           elseif (in_array($path, $pages) && !$value) {
>             // delete if unchecked
>             foreach ($pages as $pkey => $page) {
>               if ($page == $path) {
>                 unset($pages[$pkey]);
>               }
>             }
>           }
>           $record['pages'] = implode("\r\n", $pages);
>           // Save
>           $record['pages'] = trim($record['pages']);
>           $package = $record['package'];
>           // clear cache
>           cache_clear_all('visibility_api:'. $record['package'], 'cache', TRUE);
>           // save values
>           visibility_api_record_save($record);
>         }
>       }
>       return;
>     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);
>     $collapsed = TRUE;
>     foreach ($values as $value) {
>       if (empty($value)) {
>         continue;
>       }
>       else {
>         $collapsed = FALSE;
>         break;
>       }
>     }
>     /** 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' => $collapsed,
>       '#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;
> }
> 
