php include dirname __FILE__ Basecamp class php user sg api amocrm com

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
include dirname(__FILE__).'/Basecamp.class.php';
$user="sg+api@amocrm.com";
$passwd="";
$acc=2325915;
$projects=array(3407726,3837904);
$basecamp = new Basecamp('amocrmTeam Sync (support@amocrm.com)');
$basecamp->setDebug(!true);
$basecamp->setServerAuthentication($user,$passwd);
//$r=$basecamp->getAccounts();
$r=$basecamp->setAccount('https://basecamp.com/2325915/api/v1');
//$r=$basecamp->getProjects();
$lists=array();
foreach ($projects as $proj){
$r=$basecamp->getTodoLists(false,$proj);
foreach($r as $lo){
$lists[$proj][$lo->id]="{$proj}|".$lo->name;
}
}
$f=dirname(__FILE__).'/config.jsn';
$s=@json_decode(@file_get_contents($f),1);
if (empty($s)) $s=array();
if (!isset($argv)){
header('Content-type: text/html; charset=utf8');
if ($_POST){
$d=array();
foreach ($_POST as $k=>$v){
$k=intval($k);
if ($k<1 OR empty($v)) continue;
$d[$k]=$v;
}
file_put_contents($f,json_encode($d));
echo "OK";
} else {
echo "<form method='POST' action='./index.php'>";
foreach ($lists as $proj)
foreach ($proj as $i=>$n){
$val=(isset($s[$i]))?$s[$i]:'';
echo htmlspecialchars($n).": <input type='text' name='{$i}' value='{$val}'><br>\n";
}
echo "<input type='submit' value='Save'></form>";
}
die;
}
$tasks=array();
foreach ($lists as $proj=>$projd)
foreach (array_keys($projd) as $v){
$r=$basecamp->getTodoList($proj,$v);
foreach ($r->todos->remaining as $to){
//var_dump($r->todos); break;
$nn=getnewname($v,$to->content);
if (empty($nn)) continue;
$tasks[$to->id]=array($proj,$nn);
}
// break;
}
//$tasks=array_filter($tasks,'is_string');
//var_dump($tasks);
foreach ($tasks as $id=>$nn){
echo "Setting {$id} (p{$nn[0]}): {$nn[1]}\n";
$r=$basecamp->updateTodo($nn[0],$id,$nn[1]);
// var_dump($r);
// break;
}
function getnewname($l,$name){
global $s;
$counter=(int)@file_get_contents(dirname(__FILE__).'/counter.jsn');
$counter++;
if (empty($name)) return NULL;
if (preg_match('/^\[[A-Z-]*\d+\]/',$name)) return NULL;
file_put_contents(dirname(__FILE__).'/counter.jsn',$counter);
if (isset($s[$l])) $pfx=$s[$l];
else $pfx='';
return "[{$pfx}{$counter}] ".$name;
}