<?php

/**
 * Ahead WebSoft Technologies Pvt Ltd.
 * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
 * @license    https://www.socialengine.com/policies
 */

class Core_View_Helper_CustomFieldValueLoop extends Fields_View_Helper_FieldAbstract
{

	public function customFieldValueLoop($subject, $contentShow = true, $labelBold = true, $fieldClass = 'custom_field', $labelClass = 'custom_field_label', $valueClass = 'custom_field_value')
	{
		$view = $this->view;
		$view->addHelperPath(APPLICATION_PATH . '/application/modules/Fields/View/Helper', 'Fields_View_Helper');
		$partialStructure = Engine_Api::_()->core()->getFieldsStructurePartial($subject);
		if (empty($partialStructure))
			return '';

		if (!($subject instanceof Core_Model_Item_Abstract) || !$subject->getIdentity())
			return '';

		$notice = '';
		$viewer = Engine_Api::_()->user()->getViewer();
		$usePrivacy = ($subject instanceof User_Model_User);
		if ($usePrivacy) {
			$relationship = 'everyone';
			if ($viewer && $viewer->getIdentity()) {
				if ($viewer->getIdentity() == $subject->getIdentity())
					$relationship = 'self';
				else if ($viewer->membership()->isMember($subject, true))
					$relationship = 'friends';
				else
					$relationship = 'registered';
			}
		}

		// Generate
		$content = '';
		$lastContents = '';
		$lastHeadingTitle = null;

		$front = Zend_Controller_Front::getInstance();
		$module = $front->getRequest()->getModuleName();
		$action = $front->getRequest()->getActionName();
		$controller = $front->getRequest()->getControllerName();

		$show_hidden = true;

		$flag = 0;
		foreach ($partialStructure as $map) {

			// Get field meta object
			$field = $map->getChild();
			$value = $field->getValue($subject);
			if (!$field || $field->type == 'profile_type')
				continue;
			if (!$field->display && !$show_hidden)
				continue;
			$isHidden = !$field->display;

			// Get first value object for reference
			$firstValue = $value;
			if (is_array($value) && !empty($value)) {
				$firstValue = $value[0];
			}

			// Evaluate privacy
			if ($usePrivacy && !empty($firstValue->privacy) && $relationship != 'self') {
				if ($firstValue->privacy == 'self' && $relationship != 'self') {
					$isHidden = true; //continue;
				} else if ($firstValue->privacy == 'friends' && ($relationship != 'friends' && $relationship != 'self')) {
					$isHidden = true; //continue;
				} else if ($firstValue->privacy == 'registered' && $relationship == 'everyone') {
					$isHidden = true; //continue;
				}
			}

			// Render
			if ($field->type == 'heading') {
				// Heading
				if (!empty($lastContents)) {
					$content .= $this->_buildLastContents($lastContents, $lastHeadingTitle);
					$lastContents = '';
				}
				if ($contentShow) {
					$lastHeadingTitle = $this->view->translate($field->label);
				}
			} else {
				// Normal fields
				$tmp = $this->getFieldValueString($field, $value, $subject, $map, $partialStructure);
				if (!empty($firstValue->value) && !empty($tmp)) {

					if (!$isHidden || $show_hidden) {
						$icon = "";
						if (isset($field->icon) && $field->icon)
							$icon = '<i class="' . $field->icon . '"></i>';
						$label = $icon . $this->view->translate($field->label);
						if ($labelBold) {
							$lastContents .= <<<EOF
							<div class='{$fieldClass}' data-field-id={$field->field_id}>{$notice}<div class='{$labelClass}'><b>{$label}</b>: </div><div class='{$valueClass} '>{$tmp}</div></div>
						EOF;
						} else {
							$lastContents .= <<<EOF
							<div class='{$fieldClass}' data-field-id={$field->field_id}>{$notice}<div class='{$labelClass}'>{$label}: </div><div class='{$valueClass}'>{$tmp}</div></div>
						EOF;
						}
						$flag++;
					}
				}
			}
		}

		if (!empty($lastContents)) {
			$content .= $this->_buildLastContents($lastContents, $lastHeadingTitle);
		}

		return $content;
	}

	public function getFieldValueString($field, $value, $subject, $map = null, $partialStructure = null)
	{
		if ((!is_object($value) || !isset($value->value)) && !is_array($value)) {
			return null;
		}

		$helperName = Engine_Api::_()->fields()->getFieldInfo($field->type, 'helper');
		if (!$helperName) {
			return null;
		}

		$helper = $this->view->getHelper($helperName);
		if (!$helper) {
			return null;
		}

		$helper->structure = $partialStructure;
		$helper->map = $map;
		$helper->field = $field;
		$helper->subject = $subject;
		$tmp = $helper->$helperName($subject, $field, $value);
		unset($helper->structure);
		unset($helper->map);
		unset($helper->field);
		unset($helper->subject);

		return $tmp;
	}

	protected function _buildLastContents($content, $title)
	{
		if (!$title) {
			return $content;
		}
		return <<<EOF
          <h4><span class='customfield_title'>{$title}: </span></h4>{$content}
EOF;
	}
}
