<commit_before><?php

class dmAdminBaseGeneratedModuleActions extends dmAdminBaseActions
{
	public function preExecute()
	{
		parent::preExecute();
			
		$this->configuration = new $this->configurationClass();
		$this->configuration->setAction($this);

		$this->dispatcher->notify(new sfEvent($this, 'admin.pre_execute', array('configuration' => $this->configuration)));

		$this->helper = new $this->helperClass($this->getDmModule());
		$this->helper->setAction($this);

		$this->security_manager = $this->getDmModule()->getSecurityManager($this);
	}

	public function getCredential()
	{
		$can = $this->getDmModule()->getSecurityManager()->userHasCredentials($this->actionName);

		if(!$can)
		{
			return DmPermission::NEVER_GRANT_ACCESS; //we hope such credentials will never exists
		}
		return $can;
	}


	protected function getRouteArrayForAction($action, $object = null)
	{
		$route = array('sf_route' => $this->getDmModule()->getUnderscore(), 'action' => $action);

		if (null !== $object)
		{
			$route['pk'] = $object->getPrimaryKey();
		}

		if(sfConfig::get('dm_admin_embedded'))
		{
			$route['dm_embed'] = 1;
		}

		return $route;
	}

	/**
	 * @return dmDoctrineRecord
	 */
	public function getObject($relations = array(), $pk = 'pk')
	{
		if(!isset($this->object))
		{
			$_pk = $this->getRequest()->getParameter($pk, false);
			if(!$_pk) {
				$_pk = $this->getRequest()->getGetParameter($pk, false);
				if(!$_pk) {
					$_pk = $this->getRequest()->getPostParameter($pk, false);
				}
			}

			if(!$_pk)
			{
				$id = $this->getDmModule()->getTable()->getIdentifier();
				$_pk = $this->getRequest()->getParameter($id, false);
			}

			if($_pk){
				$this->object = $this->buildObjectQuery($_pk, empty($relations) ? $this->getRelationsAlias() : $relations)->fetchOne();
			}else{
				$this->object = false;
			}
		}
		return $this->object;
	}

	/**
	 * @param dmWebRequest $request
	 */
	protected function getObjectOrForward404(dmWebRequest $request, $relations = array())
	{
		if(!$this->object = $this->getObject($relations))
		{
			$this->forward404(
			sprintf('Unable to find the %s object with the following parameters "%s").', $this->getDmModule()->getModel(), str_replace("\n", '', var_export($request->getParameterHolder()->getAll(), true)))
			);
		}
		return $this->object;
	}

	/**
	 * @return dmModule
	 */
	public function getDmModule()
	{
		if (null !== $this->dmModule)
		{
			return $this->dmModule;
		}

		return $this->dmModule = $this->context->getModuleManager()->getModule($this->getModuleKey());
	}

	public function executeLoremize(dmWebRequest $request)
	{
		try
		{
			$this->getService('table_loremizer')->execute($this->getDmModule()->getTable(), $request->getParameter('nb', 10));

			$this->getUser()->logInfo('Successfully loremized');
		}
		catch(Exception $e)
		{
			$this->getUser()->logError('An error occured during loremization');

			if (sfConfig::get('sf_debug'))
			{
				$this->getUser()->logAlert($e->getMessage());
			}

			if (sfConfig::get('dm_debug'))
			{
				throw $e;
			}
		}

		return $this->redirectBack();
	}

	/*
	 * When sorting by a localKey column ( ex: categ_id ),
	 * try to sort with foreign's table identifier column ( ex: categ.name )
	 * Also try to sort with the translation table if any
	 */
	protected function tryToSortWithForeignColumn(Doctrine_Query $query, array $sort)
	{
		$table = $this->getDmModule()->getTable();

		if('integer' === dmArray::get($table->getColumnDefinition($sort[0]), 'type'))
		{
			if($table->isI18nColumn($sort[0]))
			{
				$query->addOrderBy(sprintf('%s.%s %s', $query->getJoinAliasForRelationAlias($table->getComponentName(), 'Translation'), $sort[0], $sort[1]));
				// Success, skip default sorting by local column
				return;
			}
			// If the sort column is a local key, try to sort with foreign table
			elseif ($relation = $table->getRelationHolder()->getLocalByColumnName($sort[0]))
			{
				if ($relation instanceof Doctrine_Relation_LocalKey && ($foreignTable = $relation->getTable()) instanceof dmDoctrineTable)
				{
					if (($foreignColumn = $foreignTable->getIdentifierColumnName()) != 'id')
					{
						if (!$joinAlias = $query->getJoinAliasForRelationAlias($table->getComponentName(), $relation->getAlias()))
						{
							$query->leftJoin(sprintf('%s.%s %s', $query->getRootAlias(), $relation->getAlias(), $relation->getAlias()));
							$joinAlias = $relation->getAlias();

							if($foreignTable->isI18nColumn($foreignColumn))
							{
								try{
									$query->withI18n(); //leftJoin(sprintf('%s.%s %s', $joinAlias, 'Translation', $joinAlias.'Translation'));
								}catch(Exception $e){}
							}
						}

						if($foreignTable->isI18nColumn($foreignColumn))
						{
							$query->addOrderBy(sprintf('%s.%s %s', $joinAlias.'Translation', $foreignColumn, $sort[1]));
						}
						else
						{
							$query->addOrderBy(sprintf('%s.%s %s', $joinAlias, $foreignColumn, $sort[1]));
						}
						// Success, skip default sorting by local column
						return;
					}
				}
			}
		}
		elseif($table->isI18nColumn($sort[0]))
		{
			$query->addOrderBy(sprintf('%s.%s %s', $query->getJoinAliasForRelationAlias($table->getComponentName(), 'Translation'), $sort[0], $sort[1]));
			// Success, skip default sorting by local column
			return;
		}

		if($table->hasField($sort[0]))<commit_msg>Fix error with joining foreign keys<commit_after><?php

class dmAdminBaseGeneratedModuleActions extends dmAdminBaseActions
{
	public function preExecute()
	{
		parent::preExecute();
			
		$this->configuration = new $this->configurationClass();
		$this->configuration->setAction($this);

		$this->dispatcher->notify(new sfEvent($this, 'admin.pre_execute', array('configuration' => $this->configuration)));

		$this->helper = new $this->helperClass($this->getDmModule());
		$this->helper->setAction($this);

		$this->security_manager = $this->getDmModule()->getSecurityManager($this);
	}

	public function getCredential()
	{
		$can = $this->getDmModule()->getSecurityManager()->userHasCredentials($this->actionName);

		if(!$can)
		{
			return DmPermission::NEVER_GRANT_ACCESS; //we hope such credentials will never exists
		}
		return $can;
	}


	protected function getRouteArrayForAction($action, $object = null)
	{
		$route = array('sf_route' => $this->getDmModule()->getUnderscore(), 'action' => $action);

		if (null !== $object)
		{
			$route['pk'] = $object->getPrimaryKey();
		}

		if(sfConfig::get('dm_admin_embedded'))
		{
			$route['dm_embed'] = 1;
		}

		return $route;
	}

	/**
	 * @return dmDoctrineRecord
	 */
	public function getObject($relations = array(), $pk = 'pk')
	{
		if(!isset($this->object))
		{
			$_pk = $this->getRequest()->getParameter($pk, false);
			if(!$_pk) {
				$_pk = $this->getRequest()->getGetParameter($pk, false);
				if(!$_pk) {
					$_pk = $this->getRequest()->getPostParameter($pk, false);
				}
			}

			if(!$_pk)
			{
				$id = $this->getDmModule()->getTable()->getIdentifier();
				$_pk = $this->getRequest()->getParameter($id, false);
			}

			if($_pk){
				$this->object = $this->buildObjectQuery($_pk, empty($relations) ? $this->getRelationsAlias() : $relations)->fetchOne();
			}else{
				$this->object = false;
			}
		}
		return $this->object;
	}

	/**
	 * @param dmWebRequest $request
	 */
	protected function getObjectOrForward404(dmWebRequest $request, $relations = array())
	{
		if(!$this->object = $this->getObject($relations))
		{
			$this->forward404(
			sprintf('Unable to find the %s object with the following parameters "%s").', $this->getDmModule()->getModel(), str_replace("\n", '', var_export($request->getParameterHolder()->getAll(), true)))
			);
		}
		return $this->object;
	}

	/**
	 * @return dmModule
	 */
	public function getDmModule()
	{
		if (null !== $this->dmModule)
		{
			return $this->dmModule;
		}

		return $this->dmModule = $this->context->getModuleManager()->getModule($this->getModuleKey());
	}

	public function executeLoremize(dmWebRequest $request)
	{
		try
		{
			$this->getService('table_loremizer')->execute($this->getDmModule()->getTable(), $request->getParameter('nb', 10));

			$this->getUser()->logInfo('Successfully loremized');
		}
		catch(Exception $e)
		{
			$this->getUser()->logError('An error occured during loremization');

			if (sfConfig::get('sf_debug'))
			{
				$this->getUser()->logAlert($e->getMessage());
			}

			if (sfConfig::get('dm_debug'))
			{
				throw $e;
			}
		}

		return $this->redirectBack();
	}

	/*
	 * When sorting by a localKey column ( ex: categ_id ),
	 * try to sort with foreign's table identifier column ( ex: categ.name )
	 * Also try to sort with the translation table if any
	 */
	protected function tryToSortWithForeignColumn(Doctrine_Query $query, array $sort)
	{
		$table = $this->getDmModule()->getTable();

		if('integer' === dmArray::get($table->getColumnDefinition($sort[0]), 'type'))
		{
			if($table->isI18nColumn($sort[0]))
			{
				$query->addOrderBy(sprintf('%s.%s %s', $query->getJoinAliasForRelationAlias($table->getComponentName(), 'Translation'), $sort[0], $sort[1]));
				// Success, skip default sorting by local column
				return;
			}
			// If the sort column is a local key, try to sort with foreign table
			elseif ($relation = $table->getRelationHolder()->getLocalByColumnName($sort[0]))
			{
				if ($relation instanceof Doctrine_Relation_LocalKey && ($foreignTable = $relation->getTable()) instanceof dmDoctrineTable)
				{
					if (($foreignColumn = $foreignTable->getIdentifierColumnName()) != 'id')
					{
						if (!$joinAlias = $query->getJoinAliasForRelationAlias($table->getComponentName(), $relation->getAlias()))
						{
							if($foreignTable->isI18nColumn($foreignColumn))
							{
								try{
									$query->withI18n(); //leftJoin(sprintf('%s.%s %s', $joinAlias, 'Translation', $joinAlias.'Translation'));
								}catch(Exception $e){}
								
								$joinAlias = $query->getRootAlias();
							}
							else
							{
								$query->leftJoin(sprintf('%s.%s %s', $query->getRootAlias(), $relation->getAlias(), $relation->getAlias()));
								$joinAlias = $relation->getAlias();
							}
						}

						if($foreignTable->isI18nColumn($foreignColumn))
						{
							$query->addOrderBy(sprintf('%s.%s %s', $joinAlias.'Translation', $foreignColumn, $sort[1]));
						}
						else
						{
							$query->addOrderBy(sprintf('%s.%s %s', $joinAlias, $foreignColumn, $sort[1]));
						}
						// Success, skip default sorting by local column
						return;
					}
				}
			}
		}
		elseif($table->isI18nColumn($sort[0]))
		{
			$query->addOrderBy(sprintf('%s.%s %s', $query->getJoinAliasForRelationAlias($table->getComponentName(), 'Translation'), $sort[0], $sort[1]));
			// Success, skip default sorting by local column
			return;
		}

		if($table->hasField($sort[0]))
{'fraction_non_alphanumeric': 0.18783093358104969, 'fraction_numerical': 0.00557361820715281, 'mean_word_length': 3.984259259259259, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 204, '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': '4044816', 'n_tokens_mistral': 4223, 'n_tokens_neox': 3451, 'n_words': 619}