#!/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::ArticleEditor::SecondaryCategory;

# load modules
use strict;

use IT::Globals;

use vars qw($MAXIMUM_CATEGORIES_ALLOWED_PER_ARTICLE);

$MAXIMUM_CATEGORIES_ALLOWED_PER_ARTICLE = 100;

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

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

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

  return $html;
}

# ------------------------------------------------------------------------------
# Function    : getRecordFieldFromFormInput
# Description :
# Usage       : my $recordFieldValue = &IT::ArtMan::CustomFields::ArticleEditor::MultiCategoryList::getRecordFieldFromFormInput($fieldName)
# ------------------------------------------------------------------------------

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

  my $recordValue = &IT::ArtMan::CustomFields::ArticleEditor::PrimaryCategory::getRecordFieldFromFormInput($fieldName);

  return $recordValue;
}

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

sub getPlaceholderFromRecordField {
  my $fieldValue = shift;
  my $record     = shift;

  # define placeholder
  my $placeholderValue = $fieldValue;

  return $placeholderValue;
}

# ------------------------------------------------------------------------------
# Function    : errorCheckingForUserInput
# Description :
# Usage       : &IT::ArtMan::CustomFields::ArticleEditor::MultiCategoryList::errorCheckingForUserInput()
# ------------------------------------------------------------------------------

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

  # NOTE: secondary category fields aren't required (no error checking)

  # Category Access Rights are enforced in IT::ArtMan::CustomMenus::Article.pm
  # since we need to check them on edit, confirmErase, erase, and save

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

# ------------------------------------------------------------------------------
# Function    : errorCheckingForFieldEditor
# Description :
# Usage       : &IT::ArtMan::CustomFields::ArticleEditor::PrimaryCategory::errorCheckingForFieldEditor()
# ------------------------------------------------------------------------------

sub errorCheckingForFieldEditor {
  my($fieldLabelOrName, $fieldFormInput, $fieldName, $tableName, $recordNum) = @_;
  my @errors;

  # enforce fieldname format
  unless ($FORM->{'newFieldName'} =~ /^category\d+Num$/) { push @errors, "Secondary Category fields must be named 'category###Num'.  (eg: category2Num, category3Num, etc)"; }

  # don't allow attributes: hidden, not editable
  if ($FORM->{'appHiddenField'}) { push @errors, "This is a required field and cannot be made hidden."; }
  if ($FORM->{'appDisplayOnly'}) { push @errors, "This is a required field and cannot be made not editable."; }

  # require field type: int
  my %validDbFieldTypes      = map { $_ => 1 } qw(int);
  my $isValidDbFieldType     = $validDbFieldTypes{ $FORM->{'dbFieldType'} };
  if (!$isValidDbFieldType) {
    my $validDbFieldTypesAsCSV = join ", ", keys %validDbFieldTypes;
    push @errors, "Invalid value for 'Database Options > Stored As.  Valid options: $validDbFieldTypesAsCSV";
  }

  # don't allow secondary categories without primary categories
  my $primaryCategoryFieldnames = &IT::ArtMan::CustomFields::ArticleEditor::PrimaryCategory::getArticleCategoryFieldnames('primary');
  if ( @$primaryCategoryFieldnames == 0 ) {
    push @errors, "You must create a 'Primary Category' field before you can create or update 'Secondary Category' fields.";
  }

  #
  return @errors;
}

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

