<commit_before><?php
class DistributionProfileConfigureAction extends KalturaAdminConsolePlugin
{
	protected $client;
	
	public function __construct()
	{
		$this->action = 'configDistributionProfile';
	}
	
	/**
	 * @return string - absolute file path of the phtml template
	 */
	public function getTemplatePath()
	{
		return realpath(dirname(__FILE__));
	}
	
	public function getRequiredPermissions()
	{
		return array(Kaltura_Client_Enum_PermissionName::SYSTEM_ADMIN_CONTENT_DISTRIBUTION_MODIFY);
	}
	
	public function doAction(Zend_Controller_Action $action)
	{
		$action->getHelper('layout')->disableLayout();
		$this->client = Infra_ClientHelper::getClient();
		$contentDistributionPlugin = Kaltura_Client_ContentDistribution_Plugin::get($this->client);
		$request = $action->getRequest();
		
		$profileId = $this->_getParam('profile_id');
		$providerType = null;
		$partnerId = null;
		$distributionProfile = null;
		
		$action->view->errMessage = null;
		$action->view->form = '';
		
		try
		{
			if ($profileId)
			{
				$distributionProfile = $contentDistributionPlugin->distributionProfile->get($profileId);
				$providerType = $distributionProfile->providerType;
				$partnerId = $distributionProfile->partnerId;
			}
			else
			{
				$providerType = $this->_getParam('provider_type');
				$partnerId = $this->_getParam('partner_id');
			}
			
			$form = null;
			$profileClass = 'Kaltura_Client_ContentDistribution_Type_DistributionProfile';
			
			if($providerType == Kaltura_Client_ContentDistribution_Enum_DistributionProviderType::GENERIC)
			{
				$form = new Form_GenericProviderProfileConfiguration($partnerId, $providerType);
				$profileClass = 'Kaltura_Client_ContentDistribution_Type_GenericDistributionProfile';
			}
			elseif($providerType == Kaltura_Client_ContentDistribution_Enum_DistributionProviderType::SYNDICATION)
			{
				$form = new Form_SyndicationProviderProfileConfiguration($partnerId, $providerType);
				$profileClass = 'Kaltura_Client_ContentDistribution_Type_SyndicationDistributionProfile';
			}
			else
			{
				$form = KalturaPluginManager::loadObject('Form_ProviderProfileConfiguration', $providerType, array($partnerId, $providerType));
				$profileClass = KalturaPluginManager::getObjectClass($profileClass, $providerType);
			}
			KalturaLog::debug("profile class [$profileClass]");
			
			if(!$form)
			{
				$action->view->errMessage = "Profile form not found for provider [$providerType]";
				return;
			}
			
			$form->setAction($action->view->url(array('controller' => 'plugin', 'action' => 'DistributionProfileConfigureAction')));
			
			$pager = new Kaltura_Client_Type_FilterPager();
			$pager->pageSize = 100;
			
			Infra_ClientHelper::impersonate($partnerId);
			$flavorParamsResponse = $this->client->flavorParams->listAction(null, $pager);
			Infra_ClientHelper::unimpersonate();
			
			if($profileId)
			{
				if ($request->isPost())
				{
					if ($form->isValid($request->getPost()))
					{
						$form->populate($request->getPost());
						$distributionProfile = $form->getObject($profileClass, $request->getPost());
						$form->resetUnUpdatebleAttributes($distributionProfile);
						$distributionProfile = $contentDistributionPlugin->distributionProfile->update($profileId, $distributionProfile);
						$form->saveProviderAdditionalObjects($distributionProfile);
						$form->setAttrib('class', 'valid');
						$action->view->formValid = true;
					}
					else
					{
						$form->populate($request->getPost());
						$distributionProfile = $form->getObject($profileClass, $request->getPost());
						$optionalFlavorParamsIds = array();
						$requiredFlavorParamsIds = array();
						if(!is_null($distributionProfile->optionalFlavorParamsIds) && strlen($distributionProfile->optionalFlavorParamsIds))
							$optionalFlavorParamsIds = explode(',', $distributionProfile->optionalFlavorParamsIds);
						if(!is_null($distributionProfile->requiredFlavorParamsIds) && strlen($distributionProfile->requiredFlavorParamsIds))
							$requiredFlavorParamsIds = explode(',', $distributionProfile->requiredFlavorParamsIds);
							
						$form->addFlavorParamsFields($flavorParamsResponse, $optionalFlavorParamsIds, $requiredFlavorParamsIds);
						
						if(is_array($distributionProfile->requiredThumbDimensions))
							foreach($distributionProfile->requiredThumbDimensions as $dimensions)
								$form->addThumbDimensions($dimensions, true);
								
						if(is_array($distributionProfile->optionalThumbDimensions))
							foreach($distributionProfile->optionalThumbDimensions as $dimensions)
								$form->addThumbDimensions($dimensions, false);
							
						$form->addThumbDimensionsForm();
					}
				}
				else
				{
					$form->populateFromObject($distributionProfile);
					
					$optionalFlavorParamsIds = array();
					$requiredFlavorParamsIds = array();
					if(!is_null($distributionProfile->optionalFlavorParamsIds) && strlen($distributionProfile->optionalFlavorParamsIds))
						$optionalFlavorParamsIds = explode(',', $distributionProfile->optionalFlavorParamsIds);
					if(!is_null($distributionProfile->requiredFlavorParamsIds) && strlen($distributionProfile->requiredFlavorParamsIds))
						$requiredFlavorParamsIds = explode(',', $distributionProfile->requiredFlavorParamsIds);
						
					$form->addFlavorParamsFields($flavorParamsResponse, $optionalFlavorParamsIds, $requiredFlavorParamsIds);
					
					if(is_array($distributionProfile->requiredThumbDimensions))
						foreach($distributionProfile->requiredThumbDimensions as $dimensions)
							$form->addThumbDimensions($dimensions, true);
							
					if(is_array($distributionProfile->optionalThumbDimensions))
						foreach($distributionProfile->optionalThumbDimensions as $dimensions)
							$form->addThumbDimensions($dimensions, false);
						
					$form->addThumbDimensionsForm();
				}
				$action->view->form = $form;
			}
			else
			{
				if ($request->isPost())
				{
					$form->populate($request->getPost());
					$distributionProfile = $form->getObject($profileClass, $request->getPost());
					
					if(!$distributionProfile->partnerId)
						$distributionProfile->partnerId = 0;
					Infra_ClientHelper::impersonate($distributionProfile->partnerId);
					$distributionProfile->partnerId = null;
					$distributionProfile = $contentDistributionPlugin->distributionProfile->add($distributionProfile);
					Infra_ClientHelper::unimpersonate();
					$form->saveProviderAdditionalObjects($distributionProfile);
					$form->setAttrib('class', 'valid');
					$action->view->formValid = true;
				}
				else 
				{
					$form->addFlavorParamsFields($flavorParamsResponse);
					$form->addThumbDimensionsForm();
				}
				$action->view->form = $form;
			}
		}
		catch(Exception $e)
		{
			KalturaLog::err($e->getMessage() . "\n" . $e->getTraceAsString());
			$action->view->errMessage = $e->getMessage();
			$optionalFlavorParamsIds = array();
			$requiredFlavorParamsIds = array();
			if(!is_null($distributionProfile->optionalFlavorParamsIds) && strlen($distributionProfile->optionalFlavorParamsIds))
				$optionalFlavorParamsIds = explode(',', $distributionProfile->optionalFlavorParamsIds);
			if(!is_null($distributionProfile->requiredFlavorParamsIds) && strlen($distributionProfile->requiredFlavorParamsIds))
				$requiredFlavorParamsIds = explode(',', $distributionProfile->requiredFlavorParamsIds);
				
			$form->addFlavorParamsFields($flavorParamsResponse, $optionalFlavorParamsIds, $requiredFlavorParamsIds);
			
			if(is_array($distributionProfile->requiredThumbDimensions))
				foreach($distributionProfile->requiredThumbDimensions as $dimensions)
					$form->addThumbDimensions($dimensions, true);
					
			if(is_array($distributionProfile->optionalThumbDimensions))
				foreach($distributionProfile->optionalThumbDimensions as $dimensions)
					$form->addThumbDimensions($dimensions, false);
					
			$form->addThumbDimensionsForm();
		}<commit_msg>qnd: fixed error when distribution ptofile is not set<commit_after><?php
class DistributionProfileConfigureAction extends KalturaAdminConsolePlugin
{
	protected $client;
	
	public function __construct()
	{
		$this->action = 'configDistributionProfile';
	}
	
	/**
	 * @return string - absolute file path of the phtml template
	 */
	public function getTemplatePath()
	{
		return realpath(dirname(__FILE__));
	}
	
	public function getRequiredPermissions()
	{
		return array(Kaltura_Client_Enum_PermissionName::SYSTEM_ADMIN_CONTENT_DISTRIBUTION_MODIFY);
	}
	
	public function doAction(Zend_Controller_Action $action)
	{
		$action->getHelper('layout')->disableLayout();
		$this->client = Infra_ClientHelper::getClient();
		$contentDistributionPlugin = Kaltura_Client_ContentDistribution_Plugin::get($this->client);
		$request = $action->getRequest();
		
		$profileId = $this->_getParam('profile_id');
		$providerType = null;
		$partnerId = null;
		$distributionProfile = null;
		
		$action->view->errMessage = null;
		$action->view->form = '';
		
		try
		{
			if ($profileId)
			{
				$distributionProfile = $contentDistributionPlugin->distributionProfile->get($profileId);
				$providerType = $distributionProfile->providerType;
				$partnerId = $distributionProfile->partnerId;
			}
			else
			{
				$providerType = $this->_getParam('provider_type');
				$partnerId = $this->_getParam('partner_id');
			}
			
			$form = null;
			$profileClass = 'Kaltura_Client_ContentDistribution_Type_DistributionProfile';
			
			if($providerType == Kaltura_Client_ContentDistribution_Enum_DistributionProviderType::GENERIC)
			{
				$form = new Form_GenericProviderProfileConfiguration($partnerId, $providerType);
				$profileClass = 'Kaltura_Client_ContentDistribution_Type_GenericDistributionProfile';
			}
			elseif($providerType == Kaltura_Client_ContentDistribution_Enum_DistributionProviderType::SYNDICATION)
			{
				$form = new Form_SyndicationProviderProfileConfiguration($partnerId, $providerType);
				$profileClass = 'Kaltura_Client_ContentDistribution_Type_SyndicationDistributionProfile';
			}
			else
			{
				$form = KalturaPluginManager::loadObject('Form_ProviderProfileConfiguration', $providerType, array($partnerId, $providerType));
				$profileClass = KalturaPluginManager::getObjectClass($profileClass, $providerType);
			}
			KalturaLog::debug("profile class [$profileClass]");
			
			if(!$form)
			{
				$action->view->errMessage = "Profile form not found for provider [$providerType]";
				return;
			}
			
			$form->setAction($action->view->url(array('controller' => 'plugin', 'action' => 'DistributionProfileConfigureAction')));
			
			$pager = new Kaltura_Client_Type_FilterPager();
			$pager->pageSize = 100;
			
			Infra_ClientHelper::impersonate($partnerId);
			$flavorParamsResponse = $this->client->flavorParams->listAction(null, $pager);
			Infra_ClientHelper::unimpersonate();
			
			if($profileId)
			{
				if ($request->isPost())
				{
					if ($form->isValid($request->getPost()))
					{
						$form->populate($request->getPost());
						$distributionProfile = $form->getObject($profileClass, $request->getPost());
						$form->resetUnUpdatebleAttributes($distributionProfile);
						$distributionProfile = $contentDistributionPlugin->distributionProfile->update($profileId, $distributionProfile);
						$form->saveProviderAdditionalObjects($distributionProfile);
						$form->setAttrib('class', 'valid');
						$action->view->formValid = true;
					}
					else
					{
						$form->populate($request->getPost());
						$distributionProfile = $form->getObject($profileClass, $request->getPost());
						$optionalFlavorParamsIds = array();
						$requiredFlavorParamsIds = array();
						if(!is_null($distributionProfile->optionalFlavorParamsIds) && strlen($distributionProfile->optionalFlavorParamsIds))
							$optionalFlavorParamsIds = explode(',', $distributionProfile->optionalFlavorParamsIds);
						if(!is_null($distributionProfile->requiredFlavorParamsIds) && strlen($distributionProfile->requiredFlavorParamsIds))
							$requiredFlavorParamsIds = explode(',', $distributionProfile->requiredFlavorParamsIds);
							
						$form->addFlavorParamsFields($flavorParamsResponse, $optionalFlavorParamsIds, $requiredFlavorParamsIds);
						
						if(is_array($distributionProfile->requiredThumbDimensions))
							foreach($distributionProfile->requiredThumbDimensions as $dimensions)
								$form->addThumbDimensions($dimensions, true);
								
						if(is_array($distributionProfile->optionalThumbDimensions))
							foreach($distributionProfile->optionalThumbDimensions as $dimensions)
								$form->addThumbDimensions($dimensions, false);
							
						$form->addThumbDimensionsForm();
					}
				}
				else
				{
					$form->populateFromObject($distributionProfile);
					
					$optionalFlavorParamsIds = array();
					$requiredFlavorParamsIds = array();
					if(!is_null($distributionProfile->optionalFlavorParamsIds) && strlen($distributionProfile->optionalFlavorParamsIds))
						$optionalFlavorParamsIds = explode(',', $distributionProfile->optionalFlavorParamsIds);
					if(!is_null($distributionProfile->requiredFlavorParamsIds) && strlen($distributionProfile->requiredFlavorParamsIds))
						$requiredFlavorParamsIds = explode(',', $distributionProfile->requiredFlavorParamsIds);
						
					$form->addFlavorParamsFields($flavorParamsResponse, $optionalFlavorParamsIds, $requiredFlavorParamsIds);
					
					if(is_array($distributionProfile->requiredThumbDimensions))
						foreach($distributionProfile->requiredThumbDimensions as $dimensions)
							$form->addThumbDimensions($dimensions, true);
							
					if(is_array($distributionProfile->optionalThumbDimensions))
						foreach($distributionProfile->optionalThumbDimensions as $dimensions)
							$form->addThumbDimensions($dimensions, false);
						
					$form->addThumbDimensionsForm();
				}
				$action->view->form = $form;
			}
			else
			{
				if ($request->isPost())
				{
					$form->populate($request->getPost());
					$distributionProfile = $form->getObject($profileClass, $request->getPost());
					
					if(!$distributionProfile->partnerId)
						$distributionProfile->partnerId = 0;
					Infra_ClientHelper::impersonate($distributionProfile->partnerId);
					$distributionProfile->partnerId = null;
					$distributionProfile = $contentDistributionPlugin->distributionProfile->add($distributionProfile);
					Infra_ClientHelper::unimpersonate();
					$form->saveProviderAdditionalObjects($distributionProfile);
					$form->setAttrib('class', 'valid');
					$action->view->formValid = true;
				}
				else 
				{
					$form->addFlavorParamsFields($flavorParamsResponse);
					$form->addThumbDimensionsForm();
				}
				$action->view->form = $form;
			}
		}
		catch(Exception $e)
		{
			KalturaLog::err($e->getMessage() . "\n" . $e->getTraceAsString());
			$action->view->errMessage = $e->getMessage();
			$optionalFlavorParamsIds = array();
			$requiredFlavorParamsIds = array();
			
			if (isset($distributionProfile))
			{
				if(!is_null($distributionProfile->optionalFlavorParamsIds) && strlen($distributionProfile->optionalFlavorParamsIds))
					$optionalFlavorParamsIds = explode(',', $distributionProfile->optionalFlavorParamsIds);
				if(!is_null($distributionProfile->requiredFlavorParamsIds) && strlen($distributionProfile->requiredFlavorParamsIds))
					$requiredFlavorParamsIds = explode(',', $distributionProfile->requiredFlavorParamsIds);
					
				$form->addFlavorParamsFields($flavorParamsResponse, $optionalFlavorParamsIds, $requiredFlavorParamsIds);
				
				if(is_array($distributionProfile->requiredThumbDimensions))
					foreach($distributionProfile->requiredThumbDimensions as $dimensions)
						$form->addThumbDimensions($dimensions, true);
						
				if(is_array($distributionProfile->optionalThumbDimensions))
					foreach($distributionProfile->optionalThumbDimensions as $dimensions)
						$form->addThumbDimensions($dimensions, false);
						
				$form->addThumbDimensionsForm();
			}
		}
{'fraction_non_alphanumeric': 0.12893910860012556, 'fraction_numerical': 0.0005021971123666039, 'mean_word_length': 5.1178955453149, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 233, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535227', 'n_tokens_mistral': 5737, 'n_tokens_neox': 4385, 'n_words': 556}