Hi guys, I'm still hammering my head with this issue. I set up a new env in a local VM computer, with Ubuntu and apache2, this is what I got from phpinfo() "PHP Version 5.3.10-1ubuntu3.5" for more details, I'm using phalcon 0.9.1
This is a completed new product on what I'm working on, here is the code for an action.
<?php
namespace Modules\Project\Target;
class Create extends \Classes\General\BasicController
{
public function post()
{
$this->filter->add('under', new \Filters\AlphaUnderscore());
$this->filter->add('interval_choices', new \Filters\Choices(array('i', 'h', 'd', 'w', 'm')));
$id = $this->filter->sanitize($this->request->getPost('id'), 'under');
$projectId = $this->filter->sanitize($this->request->getPost('project_id'), 'under');
$description = $this->request->getPost('description');
$preBuild = $this->request->getPost('prebuild');
$build = $this->request->getPost('build');
$postBuild = $this->request->getPost('postbuild');
if ($id == '') {
$this->jsonError("<b>ID</b> can't be empty.", 'id');
return;
}
if ($projectId == '') {
$this->jsonError("Unable to find the project you are working on, please reload the page and start over.", 'error');
return;
}
//checking if project exist
$project = \Models\Project::findFirst(array(
'conditions' => 'id = ?1',
'bind' => array(1 => $projectId)
));
if ($project == false) {
$this->jsonError("Unable to find the project you are working on, please reload the page and start over.", 'error');
return;
}
//loading needed settings
$setting = \Models\GlobalSetting::findFirst(array(
'conditions' => 'id = ?1',
'bind' => array(1 => 'scripts_path')
));
if ($setting == false) {
$this->jsonError("Unable to find the folder to store the projects, please contact the development team.", 'error');
return;
}
//Creating project folder structure
$this->_createFolderStructure($setting->value, $projectId, $id);
//todo: log the errors creating project folder structure.
//Creating script (xml) files
$this->_createBuildFile($setting->value.'/'.$projectId.'/'.$id.'/scripts/', 'build.xml', $build);
$this->_createBuildFile($setting->value.'/'.$projectId.'/'.$id.'/scripts/', 'prebuild.xml', $preBuild);
$this->_createBuildFile($setting->value.'/'.$projectId.'/'.$id.'/scripts/', 'postbuild.xml', $postBuild);
//todo: log the errors creating build files.
//checking if target exist
$targetModel = \Models\Target::findFirst(array(
'conditions' => 'id = ?1',
'bind' => array(1 => $id)
));
if ($targetModel != false) {
$this->jsonError("Target ID already exist, please choose another one.", 'error');
return;
}
$manager = new \Phalcon\Mvc\Model\Transaction\Manager();
$manager->collectTransactions();
var_dump($manager);
$transaction = $manager->get();
$targetModel = new \Models\Target();
$targetModel->setTransaction($transaction);
$targetModel->id = $id;
$targetModel->description = $description;
$targetModel->project_id = $projectId;
$result = $targetModel->save();
//TODO: collect the errors and log it
if ($result == false) {
$transaction->rollback("Unable to save the Target, please try again the operation.");
unset($transaction, $manager);
//$this->jsonError("Unable to save the Target, please try again the operation.", 'error');
return;
}
//Checking for triggers
$triggers = $this->request->getPost('triggers');
if ($triggers == 'commits') {
$svnUrl = $this->request->getPost('svn_url');
$svnUsername = $this->request->getPost('svn_username');
$svnPassword = $this->request->getPost('svn_password');
//Checking SVN connection
$svn = new \Classes\SourceControl\Svn();
try {
$svn->checkConnection($svnUrl, $svnUsername, $svnPassword);
} catch (\Exception $e) {
$this->jsonError($e->getMessage(), 'error');
return;
}
//Checking if SVN connection is already defined
$svnModel = \Models\Def\SourceControl\Svn::findFirst(array(
'conditions' => 'url = ?1 AND username = ?2 ',
'bind' => array(1 => $svnUrl, 2 => $svnUsername)
));
if ($svnModel == false) {
$svnModel = new \Models\Def\SourceControl\Svn();
}
//Storing the SVN connection params only if is new or password was updated
if ($svnModel->password != $svnPassword) {
$svnModel->setTransaction($transaction);
$svnModel->url = $svnUrl;
$svnModel->username = $svnUsername;
$svnModel->password = $svnPassword;
$result = $svnModel->save();
if ($result == false) {
$transaction->rollback("Unable to save the SVN definition, please try again the operation.");
unset($transaction, $manager);
return;
}
}
//Trigger interval values
$svnCheckEvery = $this->filter->sanitize($this->request->getPost('svn_check_every'), 'int');
$svnCheckInterval = $this->filter->sanitize($this->request->getPost('svn_check_interval'), 'interval_choices');
if ($svnCheckEvery == 0) {
$svnCheckEvery = 5;
$svnCheckInterval= 'i';
}
if ($svnCheckInterval == false) {
$svnCheckInterval= 'i';
}
switch ($svnCheckInterval) {
case 'i': //Minutes
$svnCheckInterval = 'minutes';
break;
case 'h': //Hours
$svnCheckInterval = 'hours';
break;
case 'd': //Days
$svnCheckInterval = 'days';
break;
case 'w': //Weeks
$svnCheckInterval = 'week';
break;
case 'm': //Months
$svnCheckInterval = 'month';
break;
}
$triggerModel = new \Models\Target\Trigger();
$triggerModel->setTransaction($transaction);
$triggerModel->type = 'commits';
$triggerModel->code = 'svn::id='.$svnModel->id;
$triggerModel->execute_every = " + ".$svnCheckEvery." ".$svnCheckInterval;
$triggerModel->next_execution = date('Y-m-d H:i:s', strtotime(" + ".$svnCheckEvery." ".$svnCheckInterval));
$triggerModel->target_id = $id;
$result = $triggerModel->save();
if ($result == false) {
$transaction->rollback("Unable to save create the trigger for the target, please try again.");
unset($transaction, $manager);
return;
}
}
$transaction->commit();
unset($transaction, $manager);
$this->jsonOk();
}
private function _createFolderStructure($path, $projectId, $targetId)
{
if (is_dir($path.'/'.$projectId.'/'.$targetId.'/scripts/') == false) {
if (@mkdir($path.'/'.$projectId.'/'.$targetId.'/scripts/', 0777, true) == false) {
$this->jsonError("Unable to create the project folder structure, please contact the development team.", 'error');
return;
}
}
if (is_dir($path.'/'.$projectId.'/'.$targetId.'/builds/') == false) {
if (@mkdir($path.'/'.$projectId.'/'.$targetId.'/builds/', 0777, true) == false) {
$this->jsonError("Unable to create the project folder structure, please contact the development team.", 'error');
return;
}
}
if (is_dir($path.'/'.$projectId.'/'.$targetId.'/temp/source/') == false) {
if (@mkdir($path.'/'.$projectId.'/'.$targetId.'/temp/source', 0777, true) == false) {
$this->jsonError("Unable to create the project folder structure, please contact the development team.", 'error');
return;
}
}
}
private function _createBuildFile($path, $file, $content)
{
if (is_dir($path) == false) {
$this->jsonError("Unable to find the Script directory for the project, please contact the development team.", 'error');
return;
}
$hdl = fopen($path.$file, 'w');
if ($hdl == false) {
$this->jsonError("Unable to create the '.$file.' file in the script directory, please contact the development team.", 'error');
return;
}
fwrite($hdl, $content);
fclose($hdl);
}
}