<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class User_Form_Signup_Photo extends Engine_Form
{
  public function init()
  {
		$translate = Zend_Registry::get('Zend_Translate');
    $settings = Engine_Api::_()->getApi('settings', 'core');
  
    // Init form
    $this->setTitle('Add Your Photo');

    $this
      ->setAttrib('enctype', 'multipart/form-data')
      ->setAttrib('id', 'SignupForm');

    $this->addElement('Image', 'current', array(
      'label' => 'Current Photo',
      'ignore' => true,
      'decorators' => array(array('ViewScript', array(
        'viewScript' => '_formSignupImage.tpl',
        'class'      => 'form element'
      )))
    ));
    Engine_Form::addDefaultDecorators($this->current);

    $this->addElement('File', 'Filedata', array(
      'label' => 'Choose New Photo',
      'multiFile' => 1,
      'validators' => array(
        array('Count', false, 1),
        array('Extension', false, 'jpg,png,gif,jpeg,webp'),
      ),
      'accept' => 'image/*',
      'data-function' => 'uploadSignupPhoto',
			'style' => 'display:none;'
    ));

		if( $settings->getSetting('user.signup.photo', 0) == 0 && $settings->getSetting('user.signup.enableavatar', 1)) {

			$this->addElement('Dummy', 'avatar', array(
        'content' => '<p class="_sep">or</p>'
      ));

			//Get the signup profile type from session
			$genderLabel = Engine_Api::_()->user()->getSignupGenderBeforeCreate();

			$images = !empty($genderLabel) ? Engine_Api::_()->getDbtable('avatars', 'user')->getAvatars(array('enabled' => 1, 'fetchAll' => 1, 'gender' => strtolower($genderLabel))) : Engine_Api::_()->getDbtable('avatars', 'user')->getAvatars(array('enabled' => 1, 'fetchAll' => 1));

			$options = array();
			foreach ($images as $image) {
				$photo = Engine_Api::_()->storage()->get($image->file_id, '');
				if($photo) {
					$options[$image->file_id] = '<img src="'.$photo->getPhotoUrl().'" alt="" />';
				}
			}

			if( $settings->getSetting('user.signup.photo', 0) == 1 ) { 
				$this->addElement('Radio', 'image_id', array(
					'multiOptions' => $options,
					'escape' => false,
					'onclick' => "showAvatarButton();",
				));
			} else {
				if(engine_count($images) > 0) {
					$label = $translate->translate("Choose Avatar");
					$this->addElement('Dummy', 'avatar', array(
						'content' => '<div class="avatar_element"><p class="_sep">or</p> <p class="user_avatar_upload_btn signupavatar_link"><a class="ajaxsmoothbox" href="'.Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'user', 'controller' => 'signup', 'action' => 'choose-avatar'), 'user_extended', true).'"><i class="fas fa-user-circle"></i><span>'.$label.'</span></a></p></div>'
					));
					$this->addElement('Hidden', 'image_id', array(
						'order' => 500
					));
				}
			}
		}
  
    $this->addElement('Hash', 'token');

    $this->addElement('Hidden', 'coordinates', array(
      'order' => 1
    ));
    $this->addElement('Hidden', 'uploadPhoto', array(
      'order' => 2
    ));
    $this->addElement('Hidden', 'nextStep', array(
      'order' => 3
    ));
    $this->addElement('Hidden', 'skip', array(
     'order' => 4
    ));
    
    // Element: done
    if( $settings->getSetting('user.signup.photo', 0) == 0 ) {
    $this->addElement('Button', 'done', array(
      'label' => 'Save Photo',
      'type' => 'submit',
      'onclick' => 'javascript:finishForm();',
      'decorators' => array(
        'ViewHelper',
      ),
    ));
    }

    // Element: skip  
    if( $settings->getSetting('user.signup.photo', 0) == 0 ) {
      $this->addElement('Cancel', 'skip-link', array(
        'label' => 'skip',
        //'prependText' => ' or ',
        'link' => true,
        'href' => 'javascript:void(0);',
        'onclick' => 'skipForm(); return false;',
        'decorators' => array(
          'ViewHelper',
        ),
      ));
    }
  }
}
