#!/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::CategoryManager::ParentCategory;

# load modules
use strict;

use IT::Globals;

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

sub createFieldHtml {
  my $tableNameWithPrefix = shift;
  my $fieldName           = shift;
  my $placeholders        = shift;
  my $template            = "admin/menus/_customEditFields.html";

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

  # load data
  my $records       = &IT::ArtMan::CustomMenus::Category::getHashOfCategoryRecords();
  my $sortedRecords = &IT::ArtMan::CustomMenus::Category::getSortedFilteredCategoryArray({});

  # generate category options
  my $categoryNum    = $placeholders->{'num'};
  my $parentNum;
  if    ($FORM->{'parentNum'})            { $parentNum = $FORM->{'parentNum'}; }
  elsif (exists $records->{$categoryNum}) { $parentNum = $records->{$categoryNum}{'parentNum'}; }

  my $invalidParents = &IT::ArtMan::CustomMenus::Category::getInvalidParentsForThisCategory($categoryNum);
  foreach my $category (@$sortedRecords) {
    my $num             = $category->{'num'};
    my $isCurrentParent = $num == $parentNum;
    my $isInvalidParent = $invalidParents->{$num};
    my $invalidReason   = $invalidParents->{$num}; # circularReference or exceedsMaxDepth

    my $cellPlaceholders = {};
    $cellPlaceholders->{'_breadcrumbText'} = &IT::ArtMan::CustomMenus::Category::generateTextBreadcrumb($num);
    $cellPlaceholders->{'_optionValue'}    = $isInvalidParent ? $invalidReason               : $num;
    $cellPlaceholders->{'_class'}          = $isInvalidParent ? ' class="unavailableOption"' : '';
    $cellPlaceholders->{'_selected'}       = $isCurrentParent ? ' selected="selected"'       : '';

    $placeholders->{'categoryManagerParentCategoryOptions'} .= &templateCell($template, 'categoryManagerParentCategoryOption', $cellPlaceholders);
  }

  # generate field html
  my $html = &templateCell($template, 'categoryManagerParentCategory', $placeholders);

  return $html;
}

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

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

  # set record value
  my $recordValue = $FORM->{$fieldName};

  return $recordValue;
}

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

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

  $placeholderValue = $recordFieldValue;

  return $placeholderValue;
}

# ------------------------------------------------------------------------------
# Function    : errorCheckingForUserInput
# Description :
# Usage       : &IT::ArtMan::CustomFields::CategoryManager::ParentCategory::errorCheckingForUserInput()
# ------------------------------------------------------------------------------

sub errorCheckingForUserInput {
  my($fieldLabelOrName, $fieldValue, $fieldName, $tableName, $recordNum) = @_;
  my $parentNum = $FORM->{'parentNum'};

  # get errors
  my $error;
  if    ($parentNum eq 'exceedsMaxDepth')             { $error = "Selecting that category would exceed the maximum category depth of $SETTINGS->{'maxCategoryDepth'} levels (defined in Admin Menu)."; }
  elsif ($parentNum eq 'circularReference')           { $error = "You can't select the current category or it's children as a parent."; }
  elsif ($parentNum && $parentNum ne int($parentNum)) { $error = "Parent Category value must be a number, not '$parentNum'!"; }

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

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

