/**
  * Determine whether to show a grid of existing records or a form for either adding a new record, editing an existing one,
  * or creating a new record from an existing one.
  * @param array $args iform parameters.
  * @param object $node node being shown.
  * @return const The mode [MODE_GRID|MODE_NEW|MODE_EXISTING|MODE_CLONE].
  */
 protected static function getMode($args, $node)
 {
     // Default to mode MODE_GRID or MODE_NEW depending on no_grid parameter
     $mode = isset($args['no_grid']) && $args['no_grid'] ? self::MODE_NEW : self::MODE_GRID;
     self::$loadedSampleId = null;
     self::$loadedOccurrenceId = null;
     self::$availableForGroups = $node->available_for_groups;
     if ($_POST) {
         if (!array_key_exists('website_id', $_POST)) {
             // non Indicia POST, in this case must be the location allocations. add check to ensure we don't corrupt the data by accident
             if (function_exists('iform_loctools_checkaccess') && iform_loctools_checkaccess($node, 'admin') && array_key_exists('mnhnld1', $_POST)) {
                 $locs = array();
                 foreach ($_POST as $key => $value) {
                     $parts = explode(':', $key);
                     if ($parts[0] == 'location' && !in_array($parts[1], $locs)) {
                         $locs[] = $parts[1];
                     }
                 }
                 if (count($locs) > 0) {
                     foreach ($locs as $loc) {
                         iform_loctools_deletelocation($node, $loc);
                     }
                     foreach ($_POST as $key => $value) {
                         $parts = explode(':', $key);
                         if ($parts[0] == 'location' && $value == 1) {
                             iform_loctools_insertlocation($node, $parts[2], $parts[1]);
                         }
                     }
                 }
             }
         } else {
             if (!is_null(data_entry_helper::$entity_to_load)) {
                 // errors with new sample or entity populated with post, so display this data.
                 $mode = self::MODE_EXISTING;
             }
         }
         // else valid save, so go back to gridview: default mode 0
     }
     if (!empty($_GET['sample_id']) && $_GET['sample_id'] != '{sample_id}') {
         $mode = self::MODE_EXISTING;
         self::$loadedSampleId = $_GET['sample_id'];
     }
     if (!empty($_GET['occurrence_id']) && $_GET['occurrence_id'] != '{occurrence_id}') {
         $mode = self::MODE_EXISTING;
         self::$loadedOccurrenceId = $_GET['occurrence_id'];
     }
     if ($mode != self::MODE_EXISTING && array_key_exists('new', $_GET)) {
         $mode = self::MODE_NEW;
         data_entry_helper::$entity_to_load = array();
     }
     if ($mode == self::MODE_EXISTING && array_key_exists('new', $_GET)) {
         $mode = self::MODE_CLONE;
     }
     return $mode;
 }