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

# load modules
use strict;

use IT::Globals;

use vars qw(@STATUS_OPTIONS);

@STATUS_OPTIONS = (
  {
    label   => "pending (awaiting approval)",
    value   => "pending",
    default => 0,
  },{
    label   => "visible (displayed on website)",
    value   => "visible",
    default => 0,
  },{
    label   => "hidden (not displayed)",
    value   => "hidden",
    default => 0,
  }
);



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

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

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

  # display hidden field for writers
  my $isWriter            = ($CURRENT_USER->{'accessLevel'} <= 2);
  if ($isWriter) {
    my $html = &templateCell($template, 'articleEditorArticleStatusForWriters', $placeholders);
    return $html;
  }

  # generate options
  my $selectedStatus = $placeholders->{'status'};
  foreach my $option (@STATUS_OPTIONS) {
    my $isSelected       = ($option->{'value'} eq $selectedStatus);
    my $cellPlaceholders = {};
    $cellPlaceholders->{'_optionValue'} = $option->{'value'};
    $cellPlaceholders->{'_optionLabel'} = $option->{'label'};
    $cellPlaceholders->{'_selected'}    = $isSelected ? ' selected="selected"' : '';

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

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

  return $html;
}

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

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

  # always return "pending" for writers
  my $isWriter = ($CURRENT_USER->{'accessLevel'} <= 2);
  if ($isWriter) {
    my $recordValue = "pending";
    return $recordValue;
  }

  # for other users check form value
  my $recordValue = $FORM->{$fieldName};

  return $recordValue;
}

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

sub getPlaceholderFromRecordField {
  my $recordFieldValue = shift;

  my $placeholderValue = $recordFieldValue;

  return $placeholderValue;
}

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

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


  # make field required
  unless ($FORM->{'status'}) {
    $error .= "You must select an article status!";
  }

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

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

