#!/usr/bin/perl
# ----------------------------------------------------------------------------
# This software is Copyright (C) 2007, All Rights Reserved
# ----------------------------------------------------------------------------
# This program is protected by local and international copyright laws. Any
# use of this program is subject to the the terms of the license agreement
# included as part of this distribution archive. Any other uses are strictly
# prohibited without the written permission of the Vendor and all
# other rights are reserved.
# -----------------------------------------------------------------------------
# Warning : Modifying this program violates your license agreement!
# -----------------------------------------------------------------------------

package IT::ArtMan::CustomFields::AccountManager::CategoryRights;

# load modules
use strict;

use IT::Globals;

# ------------------------------------------------------------------------------
# Function    : createFieldHtml
# Description :
# Usage       : &IT::ArtMan::CustomFields::AccountManager::CategoryRights::createFieldHtml($tableNameWithPrefix, $fieldName, $placeholders)
# ------------------------------------------------------------------------------

sub createFieldHtml {
  my $accountsTableName   = shift;
  my $fieldName           = shift;
  my $placeholders        = shift;
  my $categoryTableName   = $TABLE_PREFIX . "category";
  my $template            = "admin/menus/_customEditFields.html";
  my $checkedHtml         = qq| checked="checked"|;

  $placeholders = { %{$placeholders} };  # copy hash values so we don't modify global

  # load category rights data
  my $recordFieldValue = $placeholders->{'_fieldValue'};
  my($formAccessRights,   $formEmailRights)   = &getCategoryRightsHashesFromFormInput($fieldName);
  my($recordAccessRights, $recordEmailRights) = &getCategoryRightsHashesFromRecordValue($recordFieldValue);

  # generate "All Categories" row
  &IT::App::Menu::rotateBgColorClassGlobalPlaceholder();
  my $accountManagerCategoryRightsRows;
  my $isAllAccessChecked = $formAccessRights->{'all'} || $recordAccessRights->{'all'};
  my $isAllEmailChecked  = $formEmailRights->{'all'} || $recordEmailRights->{'all'};
  my $cellPlaceholders   =  { %$placeholders };
  $cellPlaceholders->{'category.num'}   = "all";
  $cellPlaceholders->{'category.depth'} = "0";
  $cellPlaceholders->{'category.name'}  = "<b>All Categories</b>";
  $cellPlaceholders->{'_accessChecked'} = $isAllAccessChecked ? $checkedHtml : "";
  $cellPlaceholders->{'_emailChecked'}  = $isAllEmailChecked  ? $checkedHtml : "";
  $accountManagerCategoryRightsRows .= &templateCell($template, 'accountManagerCategoryRightsRow', $cellPlaceholders);

  # generate category rows
  my $sortedRecords = &IT::ArtMan::CustomMenus::Category::getSortedFilteredCategoryArray({});
  foreach my $record (@$sortedRecords) {
    my $num = $record->{'num'};
    &IT::App::Menu::rotateBgColorClassGlobalPlaceholder();
    my $cellPlaceholders               = { %$placeholders };

    # add category placeholders
    my $categoryPlaceholders           = &IT::App::getPlaceholdersFromRecordFields($categoryTableName, $record);
    foreach my $name (keys %$categoryPlaceholders) {
      $cellPlaceholders->{"category.$name"} = $categoryPlaceholders->{$name};
    }
    my $isAccessChecked = $formAccessRights->{$num} || $recordAccessRights->{$num};
    my $isEmailChecked  = $formEmailRights->{$num} || $recordEmailRights->{$num};
    $cellPlaceholders->{'_accessChecked'} = $isAccessChecked ? $checkedHtml : '';
    $cellPlaceholders->{'_emailChecked'}  = $isEmailChecked ? $checkedHtml : '';

    $accountManagerCategoryRightsRows .= &templateCell($template, 'accountManagerCategoryRightsRow', $cellPlaceholders);
  }

  # show no matches row if no categories
  $accountManagerCategoryRightsRows ||= &templateCell($template, 'accountManagerCategoryRightsNoRecords', {});

  # create category table
  $placeholders->{'accountManagerCategoryRightsRows'} = $accountManagerCategoryRightsRows;
  my $CategoryRightsFieldHtml = &templateCell($template, 'accountManagerCategoryRights', $placeholders);

  return $CategoryRightsFieldHtml;

}

# ------------------------------------------------------------------------------
# Function    : getRecordFieldFromFormInput
# Description : return record value for category rights data
# Usage       : my $recordFieldValue = &IT::ArtMan::CustomFields::AccountManager::CategoryRights::getRecordFieldFromFormInput($fieldName)
# ------------------------------------------------------------------------------

sub getRecordFieldFromFormInput {
  my $fieldName = shift || die "no fieldname specified!";

  # get rights from form input
  my($categoryAccessRights, $categoryEmailRights) = &getCategoryRightsHashesFromFormInput($fieldName);

  # set record value
  my $recordValue;
  $recordValue .= join ',', keys %$categoryAccessRights;
  $recordValue .= "\t";
  $recordValue .= join ',', keys %$categoryEmailRights;

  return $recordValue;
}

# ------------------------------------------------------------------------------
# Function    : getPlaceholderFromRecordField
# Description :
# Usage       : my $placeholderValue = &IT::ArtMan::CustomFields::AccountManager::CategoryRights::getPlaceholderFromRecordField($recordFieldValue)
# ------------------------------------------------------------------------------

sub getPlaceholderFromRecordField {
  my $recordFieldValue = shift;
  my $placeholderValue;

  $placeholderValue = $recordFieldValue;

  return $placeholderValue;
}

# ------------------------------------------------------------------------------
# Function    : errorCheckingForUserInput
# Description :
# Usage       : &IT::ArtMan::CustomFields::AccountManager::CategoryRights::errorCheckingForUserInput()
# ------------------------------------------------------------------------------

sub errorCheckingForUserInput {
  my($fieldLabelOrName, $fieldValue, $fieldName, $tableName, $recordNum) = @_;
  my $error;

#  my($categoryAccessRights, $categoryEmailRights) = &getCategoryRightsHashesFromFormInput($fieldName);

  # return errors
  if ($error) { return $error; }
  else        { return; } # return empty list so nothing is added to errors array
}

# ------------------------------------------------------------------------------
# Function    : getCategoryRightsHashesFromFormInput
# Description : return hash of access and email rights
# Usage       : my($accessRights, $emailRights) = &IT::ArtMan::CustomFields::AccountManager::CategoryRights::getCategoryRightsHashesFromFormInput($fieldName);
# ------------------------------------------------------------------------------

sub getCategoryRightsHashesFromFormInput {
  my $fieldName = shift || die "no fieldname specified!";

  my %accessRights = map { $_ => 1 } @{ $FORM_ARRAYS->{"$fieldName.access"} };
  my %emailRights  = map { $_ => 1 } @{ $FORM_ARRAYS->{"$fieldName.email"} };

  return(\%accessRights, \%emailRights);
}

# ------------------------------------------------------------------------------
# Function    : getCategoryRightsHashesFromRecordValue
# Description : return hash of access and email rights
# Usage       : my($accessRights, $emailRights) = &IT::ArtMan::CustomFields::AccountManager::CategoryRights::getCategoryRightsHashesFromRecordValue($recordFieldValue);
# ------------------------------------------------------------------------------

sub getCategoryRightsHashesFromRecordValue {
  my $recordFieldValue = shift;

  # get rights
  my($accessRights, $emailRights) = split /\t/, $recordFieldValue;
  my %accessRights = map { $_ => 1 } split /,/, $accessRights;
  my %emailRights  = map { $_ => 1 } split /,/, $emailRights;

  return(\%accessRights, \%emailRights);
}

# ----------------------------------------------------------------------------
# end of file
1;

